Skip to content
This repository has been archived by the owner on Mar 31, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from simplifi/merging_upstream_changes
Browse files Browse the repository at this point in the history
Merging upstream changes
  • Loading branch information
mcmoyer committed Oct 11, 2016
2 parents 7a90d96 + 7140eb0 commit e4ea0f9
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -5,4 +5,4 @@
Gemfile.lock
.bundle
.yardoc
doc
doc
39 changes: 39 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,42 @@
Version 2.3.6
-------------

Features:

* Rails 5.0 support

Version 2.3.5
-------------

Features:

* Rails 4.2 support

Version 2.3.3
-------------

Features:

* Capistrano v3 support (by @shishi)

Version 2.3.2
-------------

Features:

* Documentation improvements (by @george, @kenips, @joshuapinter)
* Fix documentation of seed_once method (by weedySeaDragon)
* Allow to seed data with an id < 1 (by @SamSaffron, @aserafin)
* Seeds work on postgresql when there is no primary key or if primary key has no sequence assigned (by @aserafin)

Version 2.3.1
-------------

Features:

* Rails 4.1 support added.
* Capistrano task included. (by @linjunpop)

Version 2.3.0
-------------

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,3 +1,3 @@
source 'http://rubygems.org'
gem 'rails', ">= 3.1", "< 4.1"
gem 'rails', ">= 3.1", "< 5.1"
gemspec
26 changes: 14 additions & 12 deletions README.md
Expand Up @@ -39,16 +39,9 @@ Basic Example
Installation
------------

### Rails 4.0
### Rails 3.1, 3.2, 4.0, 4.1, 4.2, 5.0

The current latest version isn't compatible with Rails 4.0.
You will have to use the HEAD of this repo.

gem 'seed-fu', github: 'mbleigh/seed-fu'

### Rails 3.1

Just add `gem 'seed-fu', '~> 2.1.0'` to your `Gemfile`
Just add `gem 'seed-fu', '~> 2.3'` to your `Gemfile`

Seed Fu depends on Active Record, but doesn't have to be used with a full Rails app. Simply load and require the `seed-fu` gem and you're set.

Expand Down Expand Up @@ -96,8 +89,8 @@ Where to put seed files

By default, seed files are looked for in the following locations:

* `Rails.root/db/fixtures` and `Rails.root/db/fixtures/Rails.env` in a Rails app
* `db/fixtures` when loaded without Rails
* `#{Rails.root}/db/fixtures` and `#{Rails.root}/db/fixtures/#{Rails.env}` in a Rails app
* `./db/fixtures` when loaded without Rails

You can change these defaults by modifying the `SeedFu.fixture_paths` array.

Expand Down Expand Up @@ -146,7 +139,7 @@ If you need to programmatically generate seed files, for example to convert a CS
Capistrano deployment
---------------------

SeedFu has included Capistrano [deploy script](lib/seed-fu/capistrano.rb), you just need require that
SeedFu has included Capistrano [deploy script](lib/seed-fu/capistrano.rb), you just need require that
in `config/deploy.rb`:

```ruby
Expand All @@ -156,6 +149,15 @@ require 'seed-fu/capistrano'
after 'deploy:update_code', 'db:seed_fu'
```

If you use Capistrano3, you should require another file.

```ruby
require 'seed-fu/capistrano3'

# Trigger the task before publishing
before 'deploy:publishing', 'db:seed_fu'
```

Bugs / Feature requests
-----------------------

Expand Down
8 changes: 8 additions & 0 deletions Rakefile
@@ -0,0 +1,8 @@
require "bundler/gem_tasks"
require "rake/testtask"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new :spec do |spec|
spec.pattern = 'spec/**/*_spec.rb'
end

2 changes: 1 addition & 1 deletion lib/seed-fu/active_record_extension.rb
Expand Up @@ -38,7 +38,7 @@ def seed(*args, &block)
# @example
# Person.seed(:id, :id => 1, :name => "Jon") # => Record created
# Person.seed(:id, :id => 1, :name => "Bob") # => Name changed
# Person.seed(:id, :id => 1, :name => "Harry") # => Name *not* changed
# Person.seed_once(:id, :id => 1, :name => "Harry") # => Name *not* changed
def seed_once(*args, &block)
constraints, data = parse_seed_fu_args(args, block)
SeedFu::Seeder.new(self, constraints, data, :insert_only => true).seed
Expand Down
1 change: 1 addition & 0 deletions lib/seed-fu/capistrano3.rb
@@ -0,0 +1 @@
load File.expand_path('../../tasks/seed_fu_capistrano3.rake', __FILE__)
20 changes: 10 additions & 10 deletions lib/seed-fu/seeder.rb
Expand Up @@ -71,12 +71,12 @@ def seed_record(data)
else
record.assign_attributes(data)
end
record.save(:validate => false) || raise(ActiveRecord::RecordNotSaved)
record.save(:validate => false) || raise(ActiveRecord::RecordNotSaved, 'Record not saved!')
record
end

def find_or_initialize_record(data)
@model_class.unscoped.where(constraint_conditions(data)).first ||
@model_class.unscoped.where(constraint_conditions(data)).take ||
@model_class.new
end

Expand All @@ -85,15 +85,15 @@ def constraint_conditions(data)
end

def update_id_sequence
if @model_class.connection.adapter_name == "PostgreSQL"
if @model_class.connection.adapter_name == "PostgreSQL" or @model_class.connection.adapter_name == "PostGIS"
return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?

quoted_id = @model_class.connection.quote_column_name(@model_class.primary_key)
quoted_sequence = "'" + @model_class.sequence_name + "'"
@model_class.connection.execute(
"SELECT pg_catalog.setval(" +
"#{quoted_sequence}," +
"(SELECT MAX(#{quoted_id}) FROM #{@model_class.quoted_table_name}) + 1" +
");"
)
sequence = @model_class.sequence_name

@model_class.connection.execute <<-EOS
SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{@model_class.quoted_table_name}), false)
EOS
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/seed-fu/version.rb
@@ -1,4 +1,4 @@
module SeedFu
# The current version of Seed Fu
VERSION = '2.3.0'
VERSION = '2.3.6'
end
6 changes: 3 additions & 3 deletions lib/tasks/seed_fu.rake
Expand Up @@ -16,13 +16,13 @@ namespace :db do
Examples:
# default, to load all seed files for the current environment
rake db:seed
rake db:seed_fu
# to load seed files matching orders or customers
rake db:seed FILTER=orders,customers
rake db:seed_fu FILTER=orders,customers
# to load files from RAILS_ROOT/features/fixtures
rake db:seed FIXTURE_PATH=features/fixtures
rake db:seed_fu FIXTURE_PATH=features/fixtures
EOS
task :seed_fu => :environment do
if ENV["FILTER"]
Expand Down
12 changes: 12 additions & 0 deletions lib/tasks/seed_fu_capistrano3.rake
@@ -0,0 +1,12 @@
namespace :db do
desc 'Load seed data into database'
task :seed_fu do
on roles(:db) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :bundle, :exec, :rake, 'db:seed_fu'
end
end
end
end
end
12 changes: 6 additions & 6 deletions seed-fu.gemspec
Expand Up @@ -8,20 +8,20 @@ Gem::Specification.new do |s|
s.name = "seed-fu"
s.version = SeedFu::VERSION
s.platform = Gem::Platform::RUBY
s.licenses = ['MIT']
s.authors = ["Michael Bleigh", "Jon Leighton"]
s.email = ["michael@intridea.com", "j@jonathanleighton.com"]
s.homepage = "http://github.com/mbleigh/seed-fu"
s.summary = "Easily manage seed data in your Active Record application"
s.description = "Seed Fu is an attempt to once and for all solve the problem of inserting and maintaining seed data in a database. It uses a variety of techniques gathered from various places around the web and combines them to create what is hopefully the most robust seed data system around."

s.add_dependency "activerecord", [">= 3.1", "< 4.1"]
s.add_dependency "activesupport", [">= 3.1", "< 4.1"]
s.add_dependency "activerecord", [">= 3.1"]
s.add_dependency "activesupport", [">= 3.1"]

s.add_development_dependency "rspec", "~> 2.0"
s.add_development_dependency "pg"
s.add_development_dependency "mysql2"
s.add_development_dependency "sqlite3"
s.add_development_dependency "protected_attributes"
s.add_development_dependency "pg", '~> 0'
s.add_development_dependency "mysql2", '~> 0'
s.add_development_dependency "sqlite3", '~> 0'

s.files = Dir.glob("{lib}/**/*") + %w(LICENSE README.md CHANGELOG.md)
s.require_path = 'lib'
Expand Down
44 changes: 40 additions & 4 deletions spec/seeder_spec.rb
@@ -1,6 +1,34 @@
require 'spec_helper'

describe SeedFu::Seeder do

it "should work with negative seeds" do
SeededModel.seed(:id) do |s|
s.id = 10
s.login = "bob2"
s.first_name = "Bob2"
s.last_name = "Bobson2"
s.title = "Peaon2"
end

SeededModel.seed(:id) do |s|
s.id = -2
s.login = "bob"
s.first_name = "Bob"
s.last_name = "Bobson"
s.title = "Peon"
end

bob = SeededModel.find_by_id(-2)
bob.first_name.should == "Bob"
bob.last_name.should == "Bobson"

if ENV['DB'] == 'postgresql'
next_id = SeededModel.connection.execute("select nextval('seeded_models_id_seq')")
next_id[0]['nextval'].to_i.should == 11
end
end

it "should create a model if one doesn't exist" do
SeededModel.seed(:id) do |s|
s.id = 5
Expand Down Expand Up @@ -132,23 +160,31 @@
end

it "should require that all constraints are defined" do
lambda { SeededModel.seed(:doesnt_exist, :title => "Bla") }.should raise_error(ArgumentError)
expect { SeededModel.seed(:doesnt_exist, :title => "Bla") }.to raise_error(ArgumentError)
end

it "should not perform validation" do
lambda { SeededModel.seed(:id => 1) }.should_not raise_error(ActiveRecord::RecordInvalid)
expect { SeededModel.seed(:id => 1) }.not_to raise_error()
end

if ENV["DB"] == "postgresql"
it "should update the primary key sequence after a records have been seeded" do
id = SeededModel.connection.select_value("SELECT currval('seeded_models_id_seq')").to_i + 1
SeededModel.seed(:title => "Foo", :id => id)

lambda { SeededModel.create!(:title => "Bla") }.should_not raise_error
expect { SeededModel.create!(:title => "Bla") }.not_to raise_error
end

it "should not raise error when there is no primary key specified" do
expect { SeededModelNoPrimaryKey.seed(:id => "Id") }.not_to raise_error
end

it "should not raise error when there is primary key without sequence" do
expect { SeededModelNoSequence.seed(:id => "Id") }.not_to raise_error
end
end

it "should raise an ActiveRecord::RecordNotSaved exception if any records fail to save" do
lambda { SeededModel.seed(:fail_to_save => true, :title => "Foo") }.should raise_error(ActiveRecord::RecordNotSaved)
expect { SeededModel.seed(:fail_to_save => true, :title => "Foo") }.to raise_error(ActiveRecord::RecordNotSaved)
end
end
21 changes: 19 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -2,7 +2,6 @@
require 'bundler/setup'
require 'seed-fu'
require 'logger'
require 'protected_attributes'

SeedFu.quiet = true

Expand All @@ -20,17 +19,35 @@
t.column :title, :string
t.column :is_deleted, :boolean, default: false
end

create_table :seeded_model_no_primary_keys, :id => false, :force => true do |t|
t.column :id, :string
end

create_table :seeded_model_no_sequences, :id => false, :force => true do |t|
t.column :id, :string
end

execute("ALTER TABLE seeded_model_no_sequences ADD PRIMARY KEY (id)") if ENV['DB'] == 'postgresql'
end

class SeededModel < ActiveRecord::Base
validates_presence_of :title
attr_protected :first_name
attr_protected :first_name if self.respond_to?(:protected_attributes)
attr_accessor :fail_to_save
default_scope { where(:is_deleted => false) }

before_save { false if fail_to_save }
end

class SeededModelNoPrimaryKey < ActiveRecord::Base

end

class SeededModelNoSequence < ActiveRecord::Base

end

RSpec.configure do |config|
config.before do
SeededModel.delete_all
Expand Down

0 comments on commit e4ea0f9

Please sign in to comment.