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

Merging upstream changes #1

Merged
merged 46 commits into from Oct 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
63487f5
fix seed-fu for postgresql when there is no primary key defined or pr…
aserafin Aug 14, 2013
2716391
removed not valid gitignore entries, fixed typo in test, moved if int…
aserafin Aug 14, 2013
502c8b8
BUGFIX: if you seed data that has an id < 1 seed-fu attempts to reset…
SamSaffron Sep 6, 2013
29be328
add rake file
SamSaffron Dec 14, 2013
9437fb2
use rspec
SamSaffron Dec 14, 2013
35dbef4
fix specs in Rails 4
SamSaffron Dec 14, 2013
f4ffaf3
Failing test in DB=postgresql
SamSaffron Dec 14, 2013
2148ee9
rails 4.1 support
Apr 9, 2014
cbd56b6
Merge pull request #67 from igas/rails41
aserafin Apr 11, 2014
01dea6f
bump version to 2.3.1
aserafin Apr 14, 2014
1a4fb7d
update `rake db:seed_fu` description (minor)
unpublishedworks Apr 14, 2014
a047d65
Merge pull request #69 from george/patch-3
aserafin Apr 14, 2014
5198074
Update README and CHANGELOG
kenips Apr 14, 2014
a47f53c
Add Capistrano3 support
shishi Apr 16, 2014
b726d28
Made location of seed data more clear.
joshuapinter May 10, 2014
2f63925
Merge pull request #53 from aserafin/postgresql-no-primary-key
aserafin Jun 16, 2014
9f72689
Merge pull request #70 from kenips/bug/readme_rails41
aserafin Jun 16, 2014
c5266a4
adding primary key after table creation is postgresql specific
aserafin Jun 16, 2014
b61632f
Merge pull request #73 from joshuapinter/patch-1
aserafin Jun 16, 2014
4266e55
Merge pull request #54 from SamSaffron/master
aserafin Jun 16, 2014
cfd6cc9
#65 fix seed_once method documentation
aserafin Jun 16, 2014
f998e94
checking sequence nextval test only for postgresql
aserafin Jun 17, 2014
ed0f001
bump version to 2.3.2
aserafin Jun 17, 2014
a0ccc59
fix rubygems dependency versioning deprecation
aserafin Jun 17, 2014
b62858b
Merge pull request #71 from shishi/add_cap3_support
aserafin Jul 15, 2014
192bf48
bump version to 2.3.2
aserafin Jul 15, 2014
0d958e8
Update dependencies for Rails 4.2
Dec 22, 2014
38e328b
fix tests for 4.2
Dec 22, 2014
131ccdf
Merge pull request #87 from Nikamura/master
aserafin Dec 24, 2014
92ac270
bump version to 2.3.4
aserafin Dec 24, 2014
25881e3
Remove implicit ordering when searching existing row by constraint
Dec 29, 2014
6e96d63
update dependencies for Rails 4.2.x
y-yagi Mar 4, 2015
ca7f1e6
Merge pull request #92 from y-yagi/rails_4_2_x
aserafin Mar 29, 2015
b5b32f0
bump version to 2.3.5
aserafin Mar 29, 2015
a13ce36
Update CHANGELOG.md
aserafin Mar 29, 2015
264801c
removing rails 4 limitation
Nov 10, 2015
8468169
Merge pull request #99 from cimtico/rails_5
aserafin Mar 18, 2016
2ff039c
Merge pull request #89 from Valve/remove-implicit-ordering
aserafin Mar 18, 2016
f9be330
add MIT license to gemspec
fzipi Mar 22, 2016
8def918
add PostGIS adapter name detect
sickate Apr 18, 2016
154e6b4
Merge pull request #103 from fzipi/add-gemspec-license
aserafin Jul 1, 2016
bbcb75a
allow rails 5 in Gemfile
aserafin Jul 1, 2016
61361bf
bump version to 2.3.6 with Rails 5 support
aserafin Jul 1, 2016
9d393b2
Merge pull request #106 from sickate/master
aserafin Jul 1, 2016
da62844
Merge remote-tracking branch 'upstream/master'
mcmoyer Oct 11, 2016
7140eb0
removing explicit require of protected_attributes
mcmoyer Oct 11, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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