revo-seed-fu 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +66 -0
- data/Rakefile +6 -0
- data/init.rb +1 -0
- data/lib/seed-fu.rb +90 -0
- data/rails/init.rb +1 -0
- data/revo-seed-fu.gemspec +30 -0
- data/spec/schema.rb +8 -0
- data/spec/seed_fu_spec.rb +73 -0
- data/spec/spec_helper.rb +8 -0
- data/tasks/seed_fu_tasks.rake +44 -0
- metadata +75 -0
data/README
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
Seed Fu
|
2
|
+
=======
|
3
|
+
|
4
|
+
Seed Fu is an attempt to once and for all solve the problem of inserting and
|
5
|
+
maintaining seed data in a database. It uses a variety of techniques gathered
|
6
|
+
from various places around the web and combines them to create what is
|
7
|
+
hopefully the most robust seed data system around.
|
8
|
+
|
9
|
+
If you have suggestions or come across problems, please report them on
|
10
|
+
the Lighthouse project for Seed Fu:
|
11
|
+
http://mbleigh.lighthouseapp.com/projects/10223-seed-fu/overview
|
12
|
+
|
13
|
+
Usage
|
14
|
+
=======
|
15
|
+
|
16
|
+
Seed data is taken from the db/fixtures directory. Simply make descriptive .rb
|
17
|
+
files in that directory (they can be named anything, but users.rb for the User
|
18
|
+
model seed data makes sense, etc.). These scripts will be run whenever the
|
19
|
+
db:seed rake task is called. You can put arbitrary Ruby code in these files,
|
20
|
+
but remember that it will be executed every time the rake task is called, so
|
21
|
+
it needs to be runnable multiple times on the same database.
|
22
|
+
|
23
|
+
You can also have environment-specific seed data placed in
|
24
|
+
db/fixtures/ENVIRONMENT that will only be loaded if that is the current
|
25
|
+
environment.
|
26
|
+
|
27
|
+
Let's say we want to populate a few default users. In db/fixtures/users.rb we
|
28
|
+
write the following code:
|
29
|
+
|
30
|
+
User.seed(:login, :email) do |s|
|
31
|
+
s.login = "bob"
|
32
|
+
s.email = "bob@bobson.com"
|
33
|
+
s.first_name = "Bob"
|
34
|
+
s.last_name = "Bobson"
|
35
|
+
end
|
36
|
+
|
37
|
+
User.seed(:login, :email) do |s|
|
38
|
+
s.login = "bob"
|
39
|
+
s.email = "bob@stevenson.com"
|
40
|
+
s.first_name = "Bob"
|
41
|
+
s.last_name = "Stevenson"
|
42
|
+
end
|
43
|
+
|
44
|
+
That's all you have to do! You will now have two users created in the system
|
45
|
+
and you can change their first and last names in the users.rb file and it will
|
46
|
+
be updated as such.
|
47
|
+
|
48
|
+
The arguments passed to the <ActiveRecord>.seed method are the constraining
|
49
|
+
attributes: these must remain unchanged between db:seed calls to avoid data
|
50
|
+
duplication. By default, seed data will constrain to the id like so:
|
51
|
+
|
52
|
+
Category.seed do |s|
|
53
|
+
s.id = 1
|
54
|
+
s.name = "Buttons"
|
55
|
+
end
|
56
|
+
|
57
|
+
Category.seed do |s|
|
58
|
+
s.id = 2
|
59
|
+
s.name = "Bobbins"
|
60
|
+
s.parent_id = 1
|
61
|
+
end
|
62
|
+
|
63
|
+
Note that any constraints that are passed in must be present in the subsequent
|
64
|
+
seed data setting.
|
65
|
+
|
66
|
+
Copyright (c) 2008 Michael Bleigh, released under the MIT license
|
data/Rakefile
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
data/lib/seed-fu.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
module SeedFu
|
2
|
+
class Seeder
|
3
|
+
def self.plant(model_class, *constraints, &block)
|
4
|
+
constraints = [:id] if constraints.empty?
|
5
|
+
seed = Seeder.new(model_class)
|
6
|
+
insert_only = constraints.last.is_a? TrueClass
|
7
|
+
constraints.delete_at(*constraints.length-1) if (constraints.last.is_a? TrueClass or constraints.last.is_a? FalseClass)
|
8
|
+
seed.set_constraints(*constraints)
|
9
|
+
yield seed
|
10
|
+
seed.plant!(insert_only)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(model_class)
|
14
|
+
@model_class = model_class
|
15
|
+
@constraints = [:id]
|
16
|
+
@data = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_constraints(*constraints)
|
20
|
+
raise "You must set at least one constraint." if constraints.empty?
|
21
|
+
@constraints = []
|
22
|
+
constraints.each do |constraint|
|
23
|
+
raise "Your constraint `#{constraint}` is not a column in #{@model_class}. Valid columns are `#{@model_class.column_names.join("`, `")}`." unless @model_class.column_names.include?(constraint.to_s)
|
24
|
+
@constraints << constraint.to_sym
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def set_attribute(name, value)
|
29
|
+
@data[name.to_sym] = value
|
30
|
+
end
|
31
|
+
|
32
|
+
def plant! insert_only=false
|
33
|
+
record = get
|
34
|
+
return if !record.new_record? and insert_only
|
35
|
+
@data.each do |k, v|
|
36
|
+
record.send("#{k}=", v)
|
37
|
+
end
|
38
|
+
record.save!
|
39
|
+
puts " - #{@model_class} #{condition_hash.inspect}"
|
40
|
+
record
|
41
|
+
end
|
42
|
+
|
43
|
+
def method_missing(method_name, *args) #:nodoc:
|
44
|
+
if (match = method_name.to_s.match(/(.*)=$/)) && args.size == 1
|
45
|
+
set_attribute(match[1], args.first)
|
46
|
+
else
|
47
|
+
super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def get
|
54
|
+
records = @model_class.find(:all, :conditions => condition_hash)
|
55
|
+
raise "Given constraints yielded multiple records." unless records.size < 2
|
56
|
+
if records.any?
|
57
|
+
return records.first
|
58
|
+
else
|
59
|
+
return @model_class.new
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def condition_hash
|
64
|
+
@data.reject{|a,v| !@constraints.include?(a)}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
class ActiveRecord::Base
|
71
|
+
# Creates a single record of seed data for use
|
72
|
+
# with the db:seed rake task.
|
73
|
+
#
|
74
|
+
# === Parameters
|
75
|
+
# constraints :: Immutable reference attributes. Defaults to :id
|
76
|
+
def self.seed(*constraints, &block)
|
77
|
+
SeedFu::Seeder.plant(self, *constraints, &block)
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.seed_many(*constraints)
|
81
|
+
seeds = constraints.pop
|
82
|
+
seeds.each do |seed_data|
|
83
|
+
seed(*constraints) do |s|
|
84
|
+
seed_data.each_pair do |k,v|
|
85
|
+
s.send "#{k}=", v
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'seed-fu'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'revo-seed-fu'
|
3
|
+
s.version = '1.0.0'
|
4
|
+
s.date = '2008-08-16'
|
5
|
+
|
6
|
+
s.summary = "Allows easier database seeding of tables."
|
7
|
+
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."
|
8
|
+
|
9
|
+
s.authors = ["Michael Bleigh","Revo Pty. Ltd."]
|
10
|
+
s.email = "michael@intridea.com"
|
11
|
+
s.homepage = 'http://github.com/mbleigh/seed-fu'
|
12
|
+
|
13
|
+
s.has_rdoc = true
|
14
|
+
s.rdoc_options = ["--main", "README"]
|
15
|
+
s.extra_rdoc_files = ["README"]
|
16
|
+
|
17
|
+
s.add_dependency 'rails', ['>= 2.1']
|
18
|
+
|
19
|
+
s.files = ["README",
|
20
|
+
"Rakefile",
|
21
|
+
"init.rb",
|
22
|
+
"lib/seed-fu.rb",
|
23
|
+
"rails/init.rb",
|
24
|
+
"revo-seed-fu.gemspec",
|
25
|
+
"spec/schema.rb",
|
26
|
+
"spec/seed_fu_spec.rb",
|
27
|
+
"spec/spec_helper.rb",
|
28
|
+
"tasks/seed_fu_tasks.rake"]
|
29
|
+
|
30
|
+
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
4
|
+
|
5
|
+
describe SeedFu::Seeder do
|
6
|
+
it "should create a model if one doesn't exist" do
|
7
|
+
SeededModel.seed(:id) do |s|
|
8
|
+
s.id = 1
|
9
|
+
s.login = "bob"
|
10
|
+
s.first_name = "Bob"
|
11
|
+
s.last_name = "Bobson"
|
12
|
+
s.title = "Peon"
|
13
|
+
end
|
14
|
+
|
15
|
+
bob = SeededModel.find_by_id(1)
|
16
|
+
bob.first_name.should == "Bob"
|
17
|
+
bob.last_name.should == "Bobson"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be able to handle multiple constraints" do
|
21
|
+
SeededModel.seed(:title, :login) do |s|
|
22
|
+
s.login = "bob"
|
23
|
+
s.title = "Peon"
|
24
|
+
s.first_name = "Bob"
|
25
|
+
end
|
26
|
+
|
27
|
+
SeededModel.count.should == 1
|
28
|
+
|
29
|
+
SeededModel.seed(:title, :login) do |s|
|
30
|
+
s.login = "frank"
|
31
|
+
s.title = "Peon"
|
32
|
+
s.first_name = "Frank"
|
33
|
+
end
|
34
|
+
|
35
|
+
SeededModel.count.should == 2
|
36
|
+
|
37
|
+
SeededModel.find_by_login("bob").first_name.should == "Bob"
|
38
|
+
SeededModel.seed(:title, :login) do |s|
|
39
|
+
s.login = "bob"
|
40
|
+
s.title = "Peon"
|
41
|
+
s.first_name = "Steve"
|
42
|
+
end
|
43
|
+
SeededModel.find_by_login("bob").first_name.should == "Steve"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be able to create models from an array of seed attributes" do
|
47
|
+
SeededModel.seed_many(:title, :login, [
|
48
|
+
{:login => "bob", :title => "Peon", :first_name => "Steve"},
|
49
|
+
{:login => "frank", :title => "Peasant", :first_name => "Francis"},
|
50
|
+
{:login => "harry", :title => "Noble", :first_name => "Harry"}
|
51
|
+
])
|
52
|
+
|
53
|
+
SeededModel.find_by_login("bob").first_name.should == "Steve"
|
54
|
+
SeededModel.find_by_login("frank").first_name.should == "Francis"
|
55
|
+
SeededModel.find_by_login("harry").first_name.should == "Harry"
|
56
|
+
end
|
57
|
+
|
58
|
+
#it "should raise an error if constraints are not unique" do
|
59
|
+
# SeededModel.create(:login => "bob", :first_name => "Bob", :title => "Peon")
|
60
|
+
# SeededModel.create(:login => "bob", :first_name => "Robert", :title => "Manager")
|
61
|
+
#
|
62
|
+
# SeededModel.seed(:login) do |s|
|
63
|
+
# s.login = "bob"
|
64
|
+
# s.title = "Overlord"
|
65
|
+
# end
|
66
|
+
#end
|
67
|
+
|
68
|
+
it "should default to an id constraint"
|
69
|
+
it "should update, not create, if constraints are met"
|
70
|
+
it "should require that all constraints are defined"
|
71
|
+
it "should raise an error if validation fails"
|
72
|
+
it "should retain fields that aren't specifically altered in the seeding"
|
73
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
namespace :db do
|
2
|
+
desc <<-EOS
|
3
|
+
Loads seed data for the current environment. It will look for
|
4
|
+
ruby seed files in <RAILS_ROOT>/db/fixtures/ and
|
5
|
+
<RAILS_ROOT>/db/fixtures/<RAILS_ENV>/.
|
6
|
+
|
7
|
+
By default it will load any ruby files found. You can filter the files
|
8
|
+
loaded by passing in the SEED environment variable with a comma-delimited
|
9
|
+
list of patterns to include. Any files not matching the pattern will
|
10
|
+
not be loaded.
|
11
|
+
|
12
|
+
You can also change the directory where seed files are looked for
|
13
|
+
with the FIXTURE_PATH environment variable.
|
14
|
+
|
15
|
+
Examples:
|
16
|
+
# default, to load all seed files for the current environment
|
17
|
+
rake db:seed
|
18
|
+
|
19
|
+
# to load seed files matching orders or customers
|
20
|
+
rake db:seed SEED=orders,customers
|
21
|
+
|
22
|
+
# to load files from RAILS_ROOT/features/fixtures
|
23
|
+
rake db:seed FIXTURE_PATH=features/fixtures
|
24
|
+
EOS
|
25
|
+
task :seed => :environment do
|
26
|
+
fixture_path = ENV["FIXTURE_PATH"] ? ENV["FIXTURE_PATH"] : "db/fixtures"
|
27
|
+
|
28
|
+
global_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, '*.rb')].sort
|
29
|
+
env_specific_seed_files = Dir[File.join(RAILS_ROOT, fixture_path, RAILS_ENV, '*.rb')]
|
30
|
+
potential_seed_files = (global_seed_files + env_specific_seed_files).uniq
|
31
|
+
|
32
|
+
if ENV["SEED"]
|
33
|
+
filter = ENV["SEED"].gsub(/,/, "|")
|
34
|
+
potential_seed_files.reject!{ |file| true unless file =~ /#{filter}/ }
|
35
|
+
puts "\n == Filtering seed files against regexp: #{filter}"
|
36
|
+
end
|
37
|
+
|
38
|
+
potential_seed_files.each do |file|
|
39
|
+
pretty_name = file.sub("#{RAILS_ROOT}/", "")
|
40
|
+
puts "\n== Seed from #{pretty_name} "
|
41
|
+
load file
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: revo-seed-fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Bleigh
|
8
|
+
- Revo Pty. Ltd.
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-08-16 00:00:00 +10:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rails
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "2.1"
|
25
|
+
version:
|
26
|
+
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.
|
27
|
+
email: michael@intridea.com
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README
|
34
|
+
files:
|
35
|
+
- README
|
36
|
+
- Rakefile
|
37
|
+
- init.rb
|
38
|
+
- lib/seed-fu.rb
|
39
|
+
- rails/init.rb
|
40
|
+
- revo-seed-fu.gemspec
|
41
|
+
- spec/schema.rb
|
42
|
+
- spec/seed_fu_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
- tasks/seed_fu_tasks.rake
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/mbleigh/seed-fu
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --main
|
52
|
+
- README
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.5
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Allows easier database seeding of tables.
|
74
|
+
test_files: []
|
75
|
+
|