genesis 0.0.1
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/.document +5 -0
- data/.gitignore +5 -0
- data/History.txt +7 -0
- data/LICENSE +20 -0
- data/README.rdoc +113 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/genesis.gemspec +70 -0
- data/lib/genesis.rb +10 -0
- data/lib/genesis/active_record_extensions.rb +62 -0
- data/lib/genesis/create_schema_seeds.rb +15 -0
- data/lib/genesis/schema_seed.rb +4 -0
- data/lib/genesis/seeder.rb +157 -0
- data/rails_generators/genesis/USAGE +0 -0
- data/rails_generators/genesis/genesis_generator.rb +50 -0
- data/rails_generators/genesis/templates/genesis_override.rake +0 -0
- data/rails_generators/genesis/templates/migration.rb +9 -0
- data/rails_generators/prepare_seeding/USAGE +1 -0
- data/rails_generators/prepare_seeding/prepare_seeding_generator.rb +52 -0
- data/rails_generators/prepare_seeding/templates/genesis.rake +23 -0
- data/rails_generators/prepare_seeding/templates/genesis_callbacks.rb +13 -0
- data/spec/genesis_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +98 -0
data/.document
ADDED
data/History.txt
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jason Harrelson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
= genesis
|
2
|
+
|
3
|
+
A data seeding solution for Ruby on Rails providing seeding facilities far more advanced than the current built
|
4
|
+
in Ruby on Rails solution.
|
5
|
+
|
6
|
+
|
7
|
+
== HISTORY
|
8
|
+
|
9
|
+
This gem is a continuation of my db-seed project (http://github.com/midas/db-seed).
|
10
|
+
|
11
|
+
I cannot claim the idea for this plugin or all of the implementation to be entirely mine. I have basically taken the
|
12
|
+
db-populate plugin by Josh Knowles and added some functionality outlined at RailsSpikes and then extended it with my
|
13
|
+
own ideas. That said, genesis does work more like Rails active record migrations than db-populate or the other db
|
14
|
+
seeding tools currently available.
|
15
|
+
|
16
|
+
|
17
|
+
== FEATURES
|
18
|
+
|
19
|
+
* Prepare seeding generator
|
20
|
+
* Genesis seed file generator similar to the Rails migration generator
|
21
|
+
* Seed version task
|
22
|
+
* Seeding task
|
23
|
+
* Database mulligan task (runs db:migrate:reset and then seeds)
|
24
|
+
* Only run seeds from the all and the current environment folder the task is being
|
25
|
+
executed within (ie. all seeds from db/seeds and db/seeds/production when rake
|
26
|
+
db:genesis RAILS_ENV=production)
|
27
|
+
|
28
|
+
|
29
|
+
== TODO
|
30
|
+
|
31
|
+
* Add option to override Rails db:seed task
|
32
|
+
* Add ability to automatically generate the down seed from the up, if possible
|
33
|
+
|
34
|
+
|
35
|
+
== REQUIREMENTS
|
36
|
+
|
37
|
+
* ActiveRecord >= 2.0
|
38
|
+
|
39
|
+
|
40
|
+
== INSTALL
|
41
|
+
|
42
|
+
gem sources -a http://gemcutter.org
|
43
|
+
sudo gem install genesis
|
44
|
+
|
45
|
+
Run the prepare seeding generator to create a lib/tasks/genesis.rake file:
|
46
|
+
|
47
|
+
script/generate prepare_seeding
|
48
|
+
|
49
|
+
Or to specify which environments to create:
|
50
|
+
|
51
|
+
script/generate prepare_seeding env:[development,staging,production]
|
52
|
+
|
53
|
+
Generate a seed file:
|
54
|
+
|
55
|
+
script/generate genesis create_users
|
56
|
+
|
57
|
+
This will generate a seed file for you in the db/seeds directory.
|
58
|
+
|
59
|
+
Generate a seed file in a specific environment folder:
|
60
|
+
|
61
|
+
script/generate genesis create_users production
|
62
|
+
|
63
|
+
This will generate a seed file for you in the db/seeds/production directory.
|
64
|
+
|
65
|
+
Next, simply populate the self.up and self.down methods of the generated seed file. The seed is a normal Ruby class.
|
66
|
+
|
67
|
+
ActiveRecord is extended with the following methods:
|
68
|
+
|
69
|
+
=== create_or_update_by_all
|
70
|
+
|
71
|
+
user = User.create_or_update_by_all( :name => 'John Smith', :number => '012345', :status => 'active' )
|
72
|
+
|
73
|
+
Will try to find a user with the name 'John Smith' and number '012345'. If found, will return, otherwise will create
|
74
|
+
the user and return it.
|
75
|
+
|
76
|
+
=== create_or_update_by_some
|
77
|
+
|
78
|
+
user = User.create_or_update_by_some( :find_by => { :name => 'John Smith', :status => 'active' }, :number => '012345' )
|
79
|
+
|
80
|
+
Will try to find a user with the name 'John Smith' and a status of 'active.' If found will update with other attributes
|
81
|
+
and save returning it. If not found, will create the user with all the attributes (name, status and number) and return it.
|
82
|
+
|
83
|
+
=== create_or_update_by_{attribute}
|
84
|
+
|
85
|
+
user = User.find_or_update_by_name( :name => 'John Smith', :number => '012345', :status => 'active' )
|
86
|
+
|
87
|
+
Will try to find a user with the name 'John Smith.' If found will update with other attributes and save returning it. If
|
88
|
+
not found, will create the user with all the attributes (name and number) and return it.
|
89
|
+
|
90
|
+
|
91
|
+
== Note on Patches/Pull Requests
|
92
|
+
|
93
|
+
* Fork the project.
|
94
|
+
* Make your feature addition or bug fix.
|
95
|
+
* Add tests for it. This is important so I don't break it in a
|
96
|
+
future version unintentionally.
|
97
|
+
* Commit, do not mess with rakefile, version, or history.
|
98
|
+
(if you want to have your own version, that is fine but
|
99
|
+
bump version in a commit by itself I can ignore when I pull)
|
100
|
+
* Send me a pull request. Bonus points for topic branches.
|
101
|
+
|
102
|
+
|
103
|
+
== LICENSE
|
104
|
+
|
105
|
+
Copyright (c) 2009 C. Jason Harrelson (midas)
|
106
|
+
|
107
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
108
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
109
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
110
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
111
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
112
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
113
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "genesis"
|
8
|
+
gem.summary = %Q{A data seeding solution for Ruby on Rails.}
|
9
|
+
gem.description = %Q{A data seeding solution for Ruby on Rails providing seeding facilities far more advanced than the current built in Ruby on Rails solution.}
|
10
|
+
gem.email = "jason@lookforwardenterprises.com"
|
11
|
+
gem.homepage = "http://github.com/midas/genesis"
|
12
|
+
gem.authors = ["C. Jason Harrelson (midas)"]
|
13
|
+
gem.add_dependency "activerecord", ">= 2.0"
|
14
|
+
gem.add_development_dependency "rspec"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
if File.exist?('VERSION')
|
41
|
+
version = File.read('VERSION')
|
42
|
+
else
|
43
|
+
version = ""
|
44
|
+
end
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "genesis #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/genesis.gemspec
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{genesis}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["C. Jason Harrelson (midas)"]
|
12
|
+
s.date = %q{2009-12-13}
|
13
|
+
s.description = %q{A data seeding solution for Ruby on Rails providing seeding facilities far more advanced than the current built in Ruby on Rails solution.}
|
14
|
+
s.email = %q{jason@lookforwardenterprises.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"History.txt",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"genesis.gemspec",
|
28
|
+
"lib/genesis.rb",
|
29
|
+
"lib/genesis/active_record_extensions.rb",
|
30
|
+
"lib/genesis/create_schema_seeds.rb",
|
31
|
+
"lib/genesis/schema_seed.rb",
|
32
|
+
"lib/genesis/seeder.rb",
|
33
|
+
"rails_generators/genesis/USAGE",
|
34
|
+
"rails_generators/genesis/genesis_generator.rb",
|
35
|
+
"rails_generators/genesis/templates/genesis_override.rake",
|
36
|
+
"rails_generators/genesis/templates/migration.rb",
|
37
|
+
"rails_generators/prepare_seeding/USAGE",
|
38
|
+
"rails_generators/prepare_seeding/prepare_seeding_generator.rb",
|
39
|
+
"rails_generators/prepare_seeding/templates/genesis.rake",
|
40
|
+
"rails_generators/prepare_seeding/templates/genesis_callbacks.rb",
|
41
|
+
"spec/genesis_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
s.homepage = %q{http://github.com/midas/genesis}
|
45
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
46
|
+
s.require_paths = ["lib"]
|
47
|
+
s.rubygems_version = %q{1.3.5}
|
48
|
+
s.summary = %q{A data seeding solution for Ruby on Rails.}
|
49
|
+
s.test_files = [
|
50
|
+
"spec/genesis_spec.rb",
|
51
|
+
"spec/spec_helper.rb"
|
52
|
+
]
|
53
|
+
|
54
|
+
if s.respond_to? :specification_version then
|
55
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
56
|
+
s.specification_version = 3
|
57
|
+
|
58
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
59
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.0"])
|
60
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<activerecord>, [">= 2.0"])
|
63
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
64
|
+
end
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<activerecord>, [">= 2.0"])
|
67
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
data/lib/genesis.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'genesis/seeder'
|
5
|
+
|
6
|
+
module Genesis
|
7
|
+
VERSION = '0.0.1'
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveRecord::Base.send :include, Genesis::ActiveRecordExtensions if defined? ActiveRecord::Base
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Genesis
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
def self.included( base )
|
4
|
+
base.extend( ClassMethods )
|
5
|
+
class << ActiveRecord::Base; alias_method_chain :method_missing, :create_or_update; end
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Use all attributes to try and find a record. If found returns the record. Otherwise creates
|
10
|
+
# and returns the record.
|
11
|
+
#
|
12
|
+
def create_or_update_by_all( attrs={} )
|
13
|
+
conditions = attrs
|
14
|
+
record = find( :first, :conditions => conditions ) || self.new
|
15
|
+
record.attributes = attrs
|
16
|
+
record.save!
|
17
|
+
record
|
18
|
+
end
|
19
|
+
|
20
|
+
# Use some attributes (the ones passed in as the find_by hash) to try and find a record. If found
|
21
|
+
# returns the record. Otherwise creates and returns the record with all of the attributes (including
|
22
|
+
# the ones in the find_by hash.)
|
23
|
+
#
|
24
|
+
def create_or_update_by_some( attrs={} )
|
25
|
+
conditions = attrs.delete( :find_by )
|
26
|
+
raise 'You must provide a :find_by hash of attributes to search with, ie. :find_by => {:id => 1}' unless conditions
|
27
|
+
attrs.merge!( conditions )
|
28
|
+
record = find( :first, :conditions => conditions ) || self.new
|
29
|
+
record.attributes = attrs
|
30
|
+
record.save!
|
31
|
+
record
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_or_update( attrs={} )
|
35
|
+
self.create_or_update_by( :id, attrs )
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_or_update_by( field, attrs={} )
|
39
|
+
find_value = attrs[field]
|
40
|
+
conditions = {field => find_value}
|
41
|
+
record = find( :first, :conditions => conditions) || self.new
|
42
|
+
record.attributes = attrs
|
43
|
+
record.save!
|
44
|
+
record
|
45
|
+
end
|
46
|
+
|
47
|
+
# Catches method missing exceptions and tries to write a custom find or update by method that uses the he field name
|
48
|
+
# at the end of the method name as the find conditions. Method must match pattern create_or_update_by_{attribute}
|
49
|
+
#
|
50
|
+
def method_missing_with_create_or_update( method_name, *args )
|
51
|
+
if match = method_name.to_s.match(/create_or_update_by_([a-z0-9_]+)/)
|
52
|
+
field = match[1].to_sym
|
53
|
+
create_or_update_by(field, *args)
|
54
|
+
else
|
55
|
+
method_missing_without_create_or_update(method_name, *args)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
#alias_method_chain :method_missing, :create_or_update
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Genesis
|
2
|
+
class CreateSchemaSeeds < ActiveRecord::Migration
|
3
|
+
def self.up
|
4
|
+
options = {}
|
5
|
+
options.merge!(:guid => false) if defined?( UsesguidMigrations )
|
6
|
+
create_table :schema_seeds, options do |t|
|
7
|
+
t.string :version
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
drop_table :schema_seeds
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
module Genesis
|
2
|
+
class Seeder
|
3
|
+
def self.load_schema_seed_model
|
4
|
+
load( File.join(File.expand_path(File.dirname(__FILE__)), 'schema_seed.rb') )
|
5
|
+
end
|
6
|
+
|
7
|
+
# Verifies that the schema_seeds table exists creating if if it does not exist.
|
8
|
+
#
|
9
|
+
def self.verify_or_create_version_table
|
10
|
+
unless seed_version_table_exists?
|
11
|
+
load( File.join(File.expand_path(File.dirname(__FILE__)), 'create_schema_seeds.rb') )
|
12
|
+
Genesis::CreateSchemaSeeds.up
|
13
|
+
end
|
14
|
+
|
15
|
+
load_schema_seed_model
|
16
|
+
end
|
17
|
+
|
18
|
+
# Deletes all records from the schema_seeds table.
|
19
|
+
#
|
20
|
+
def self.empty_revisions_table
|
21
|
+
if seed_version_table_exists?
|
22
|
+
load_schema_seed_model
|
23
|
+
SchemaSeed.delete_all
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the current seed version from the schema_seeds table.
|
28
|
+
#
|
29
|
+
def self.get_current_version
|
30
|
+
return 'No seed version table exists. Assuming seed version is 0.' unless seed_version_table_exists?
|
31
|
+
load_schema_seed_model
|
32
|
+
determine_current_version
|
33
|
+
return @current_version
|
34
|
+
end
|
35
|
+
|
36
|
+
# Checks if the schema_seeds table exists.
|
37
|
+
#
|
38
|
+
def self.seed_version_table_exists?
|
39
|
+
ActiveRecord::Base.connection.tables.include?( 'schema_seeds' )
|
40
|
+
end
|
41
|
+
|
42
|
+
# Runs the migration process.
|
43
|
+
#
|
44
|
+
def self.run( seeds=[], to_version=nil, ignores=[] )
|
45
|
+
ignores << 'genesis_callbacks.rb'
|
46
|
+
@separator_size = 79
|
47
|
+
determine_current_version
|
48
|
+
map_versions( seeds, ignores )
|
49
|
+
raise 'There are no seeds to execute.' if @versions_map.empty?
|
50
|
+
to_version = determine_to_version( to_version )
|
51
|
+
determine_and_prepare_seed_direction( to_version )
|
52
|
+
if @to_run.empty?
|
53
|
+
puts "The requested seed version (#{to_version}) resulted in no necessary seeding. Data is up to date."
|
54
|
+
return
|
55
|
+
end
|
56
|
+
run_seeds
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def self.determine_to_version( to_version )
|
62
|
+
versions = @versions_map.empty? ? [] : @versions_map.sort
|
63
|
+
return '' if to_version.blank? && versions.blank?
|
64
|
+
return to_version.blank? ? versions[versions.size-1][0] : to_version
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.map_versions( seeds, ignores )
|
68
|
+
@versions_map = {}
|
69
|
+
seeds.each do |seed|
|
70
|
+
parts = File.split( seed )
|
71
|
+
unless ignores.include?( parts[1] )
|
72
|
+
matches = parts[1].match( /(\d*)_(\w*).rb/ )
|
73
|
+
@versions_map[matches[1]] = [matches[2], seed]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.validate_version_existence( to_version )
|
79
|
+
raise "A seed file with the version '#{to_version}' does not exist." unless @versions_map.has_key?( to_version ) || to_version == '0'
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.determine_current_version
|
83
|
+
current_seed = SchemaSeed.find( :last, :order => :version )
|
84
|
+
@current_version = current_seed.nil? ? '' : current_seed.version
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.determine_and_prepare_seed_direction( to_version )
|
88
|
+
validate_version_existence( to_version ) if to_version
|
89
|
+
to_version ||= ''
|
90
|
+
@to_run = []
|
91
|
+
return if @current_version == to_version || (@current_version.empty? && to_version == '0')
|
92
|
+
if to_version > @current_version
|
93
|
+
@versions_map = @versions_map.reject { |version, metadata| version <= @current_version || version > to_version}
|
94
|
+
@to_run = @versions_map.sort
|
95
|
+
@method = :up
|
96
|
+
else
|
97
|
+
@versions_map = @versions_map.reject { |version, metadata| version <= to_version }
|
98
|
+
@to_run = @versions_map.sort { |a, b| b[0] <=> a[0] }
|
99
|
+
@method = :down
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.parse_to_version_and_name( seed_file_name )
|
104
|
+
no_extension_name = File.split( seed_file_name )
|
105
|
+
matches = no_extension_name.match( /(\d*)_(\w*).rb/ )
|
106
|
+
return matches[1], matches[2]
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.run_seeds
|
110
|
+
callbacks = File.join( RAILS_ROOT, 'db', 'seeds', 'genesis_callbacks.rb' )
|
111
|
+
if File.exists?( callbacks )
|
112
|
+
load( callbacks )
|
113
|
+
should_run_callbacks = true
|
114
|
+
callback_method = :"before_#{@method}"
|
115
|
+
GenesisCallbacks.send( callback_method ) if GenesisCallbacks.respond_to?( callback_method )
|
116
|
+
end
|
117
|
+
|
118
|
+
@to_run.each { |version, metadata| self.run_seed( version, metadata ) }
|
119
|
+
|
120
|
+
if should_run_callbacks
|
121
|
+
callback_method = :"after_#{@method}"
|
122
|
+
GenesisCallbacks.send( callback_method ) if GenesisCallbacks.respond_to?( callback_method )
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.run_seed( version, metadata )
|
127
|
+
class_name = metadata[0].camelcase
|
128
|
+
file_name = metadata[1]
|
129
|
+
load( file_name )
|
130
|
+
klass = class_name.constantize
|
131
|
+
start_time = Time.now
|
132
|
+
log_entry_start( class_name )
|
133
|
+
klass.send( @method )
|
134
|
+
if @method == :up
|
135
|
+
SchemaSeed.create!( :version => version )
|
136
|
+
else
|
137
|
+
schema_seed = SchemaSeed.find( :first, :conditions => { :version => version } )
|
138
|
+
schema_seed.destroy unless schema_seed.nil?
|
139
|
+
end
|
140
|
+
log_entry_finish( class_name, Time.now - start_time )
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.log_entry_start( class_name )
|
144
|
+
entry = "== #{class_name}: seeding (#{@method.to_s}) "
|
145
|
+
entry << "="*(@separator_size-entry.length+1) << "\n"
|
146
|
+
puts entry
|
147
|
+
RAILS_DEFAULT_LOGGER.info entry
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.log_entry_finish( class_name, total_time )
|
151
|
+
entry = "== #{class_name}: seeded (#{@method.to_s}) (#{total_time}s) "
|
152
|
+
entry << "="*(@separator_size-entry.length) << "\n\n"
|
153
|
+
puts entry
|
154
|
+
RAILS_DEFAULT_LOGGER.info entry
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
File without changes
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class GenesisGenerator < Rails::Generator::NamedBase
|
2
|
+
attr_accessor :opts
|
3
|
+
attr_accessor :environment
|
4
|
+
attr_accessor :env_dirs
|
5
|
+
attr_accessor :stamp
|
6
|
+
|
7
|
+
def initialize( runtime_args, runtime_options={} )
|
8
|
+
super
|
9
|
+
@opts = {}
|
10
|
+
@env_dirs = []
|
11
|
+
@stamp = DateTime.now.strftime( "%Y%m%d%H%M%S" )
|
12
|
+
parse_args( args )
|
13
|
+
end
|
14
|
+
|
15
|
+
def manifest
|
16
|
+
env_str = @environment.nil? || @environment.empty? ? '' : "#{@environment}/"
|
17
|
+
|
18
|
+
record do |m|
|
19
|
+
m.directory "db/seeds"
|
20
|
+
@env_dirs.each { |env_dir| m.directory "db/seeds/#{env_dirs}" }
|
21
|
+
m.template "migration.rb", "db/seeds/#{env_str}#{@stamp}_#{file_name.underscore}.rb"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def parse_args( arguments )
|
28
|
+
arguments.each do |arg|
|
29
|
+
arg_parts = arg.split( ':' )
|
30
|
+
if arg_parts.size > 1
|
31
|
+
process_keyed_arg( arg_parts )
|
32
|
+
else
|
33
|
+
handle_env_arg( arg )
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def process_keyed_arg( arg_parts )
|
39
|
+
if arg_parts[0] == 'env'
|
40
|
+
handle_env_arg( arg_parts[1] )
|
41
|
+
else
|
42
|
+
opts[arg_parts[0].to_sym] = arg_parts[1]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def handle_env_arg( val )
|
47
|
+
@environment = val
|
48
|
+
@env_dirs = val
|
49
|
+
end
|
50
|
+
end
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
script/generate seed create_users OR script/generate seed create_users production
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class PrepareSeedingGenerator < Rails::Generator::Base
|
2
|
+
attr_accessor :opts
|
3
|
+
attr_accessor :environments
|
4
|
+
|
5
|
+
def initialize( runtime_args, runtime_options = {} )
|
6
|
+
super
|
7
|
+
@opts = {}
|
8
|
+
@environments = []
|
9
|
+
parse_args( args )
|
10
|
+
end
|
11
|
+
|
12
|
+
def manifest
|
13
|
+
record do |m|
|
14
|
+
m.directory 'db/seeds'
|
15
|
+
@environments.each { |env| m.directory "db/seeds/#{env}" }
|
16
|
+
m.file 'genesis.rake', 'lib/tasks/genesis.rake'
|
17
|
+
m.file 'genesis_callbacks.rb', 'db/seeds/genesis_callbacks.rb'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def parse_args( arguments )
|
24
|
+
arguments.each do |arg|
|
25
|
+
arg_parts = arg.split( ':' )
|
26
|
+
if arg_parts[0] == 'env'
|
27
|
+
handle_env_arg( arg_parts[1] )
|
28
|
+
else
|
29
|
+
opts[arg_parts[0].to_sym] = arg_parts[1]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
validate_env_args
|
34
|
+
end
|
35
|
+
|
36
|
+
def handle_env_arg( val )
|
37
|
+
if val.include?( '[' ) && val.include?( ']')
|
38
|
+
val.gsub!( /\[/, '' ).gsub!( /\]/, '' )
|
39
|
+
val.split( ',' ).each { |v| @environments << v.trim.gsub( /,/, '' ) }
|
40
|
+
elsif val.include?( '[' ) || val.include?( ']' )
|
41
|
+
raise 'Error The env option must be formatted without any spaces in the array. ie. env:[development,production]'
|
42
|
+
elsif val.include?( ',' )
|
43
|
+
raise 'Error The env option must be formatted with braces at the beginning and end of the list. ie. env:[development,production]'
|
44
|
+
else
|
45
|
+
@environments << val
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def validate_env_args
|
50
|
+
@environments += %w(development production) if @environments.empty?
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
namespace :db do
|
2
|
+
desc "Loads seed data for the current environment."
|
3
|
+
task :genesis => :environment do
|
4
|
+
Genesis::Seeder.verify_or_create_version_table
|
5
|
+
ignores = %w()
|
6
|
+
seeds = Dir[File.join( RAILS_ROOT, 'db', 'seeds', '*.rb' )] +
|
7
|
+
Dir[File.join( RAILS_ROOT, 'db', 'seeds', RAILS_ENV, '*.rb') ]
|
8
|
+
Genesis::Seeder.run( seeds, ENV['VERSION'] || nil, ignores )
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Drops and recreates all tables along with seeding the database"
|
12
|
+
task :mulligan => :environment do
|
13
|
+
Rake::Task['db:migrate:reset'].invoke
|
14
|
+
Rake::Task['db:genesis'].invoke
|
15
|
+
end
|
16
|
+
|
17
|
+
namespace :genesis do
|
18
|
+
desc "Returns the current seed version from teh schema_seeds table"
|
19
|
+
task :version => :environment do
|
20
|
+
puts "[Genesis Seed Version] #{Genesis::Seeder.get_current_version}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: genesis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- C. Jason Harrelson (midas)
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-13 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "2.0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: A data seeding solution for Ruby on Rails providing seeding facilities far more advanced than the current built in Ruby on Rails solution.
|
36
|
+
email: jason@lookforwardenterprises.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- History.txt
|
48
|
+
- LICENSE
|
49
|
+
- README.rdoc
|
50
|
+
- Rakefile
|
51
|
+
- VERSION
|
52
|
+
- genesis.gemspec
|
53
|
+
- lib/genesis.rb
|
54
|
+
- lib/genesis/active_record_extensions.rb
|
55
|
+
- lib/genesis/create_schema_seeds.rb
|
56
|
+
- lib/genesis/schema_seed.rb
|
57
|
+
- lib/genesis/seeder.rb
|
58
|
+
- rails_generators/genesis/USAGE
|
59
|
+
- rails_generators/genesis/genesis_generator.rb
|
60
|
+
- rails_generators/genesis/templates/genesis_override.rake
|
61
|
+
- rails_generators/genesis/templates/migration.rb
|
62
|
+
- rails_generators/prepare_seeding/USAGE
|
63
|
+
- rails_generators/prepare_seeding/prepare_seeding_generator.rb
|
64
|
+
- rails_generators/prepare_seeding/templates/genesis.rake
|
65
|
+
- rails_generators/prepare_seeding/templates/genesis_callbacks.rb
|
66
|
+
- spec/genesis_spec.rb
|
67
|
+
- spec/spec_helper.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: http://github.com/midas/genesis
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- --charset=UTF-8
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: "0"
|
82
|
+
version:
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: "0"
|
88
|
+
version:
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.3.5
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: A data seeding solution for Ruby on Rails.
|
96
|
+
test_files:
|
97
|
+
- spec/genesis_spec.rb
|
98
|
+
- spec/spec_helper.rb
|