diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..092283f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +## 0.5.0 +* `db/global.seeds.rb` will always be run before all other seeds diff --git a/README.md b/README.md index 5ec5065..587b4f8 100644 --- a/README.md +++ b/README.md @@ -25,28 +25,29 @@ Seedbank seeds follow this structure; db/seeds/ bar.seeds.rb + global.seeds.rb development/ users.seeds.rb foo.seeds.rb This would generate the following Rake tasks - rake db:seed # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env. - rake db:seed:bar # Load the seed data from db/seeds/bar.seeds.rb - rake db:seed:common # Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb. - rake db:seed:development # Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/development/*.seeds.rb. - rake db:seed:development:users # Load the seed data from db/seeds/development/users.seeds.rb - rake db:seed:original # Load the seed data from db/seeds.rb + rake db:seed # Load the seed data from db/global.seeds.rb, db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the current environment in Rails.env. + rake db:seed:bar # Load the seed data from db/global.seeds.rb, db/seeds/bar.seeds.rb + rake db:seed:common # Load the seed data from db/global.seeds.rb, db/seeds.rb and db/seeds/*.seeds.rb. + rake db:seed:development # Load the seed data from db/global.seeds.rb, db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/development/*.seeds.rb. + rake db:seed:development:users # Load the seed data from db/global.seeds.rb, db/seeds/development/users.seeds.rb + rake db:seed:original # Load the seed data from db/global.seeds.rb, db/seeds.rb Therefore, assuming `RAILS_ENV` is not set or it is "development": $ rake db:seed -will load the seeds in `db/seeds.rb`, `db/seeds/bar.seeds.rb`, `db/seeds/foo.seeds.rb` and `db/seeds/development/users.seeds.rb`. Whereas, setting the `RAILS_ENV` variable, like so: +will load the seeds in `db/global.seeds.rb`, `db/seeds.rb`, `db/seeds/bar.seeds.rb`, `db/seeds/foo.seeds.rb` and `db/seeds/development/users.seeds.rb`. Whereas, setting the `RAILS_ENV` variable, like so: $ RAILS_ENV=production rake db:seed -will load the seeds in `db/seeds.rb`, `db/seeds/bar.seeds.rb` and `db/seeds/foo.seeds.rb`. +will load the seeds in `db/global.seeds.rb`, `db/seeds.rb`, `db/seeds/bar.seeds.rb` and `db/seeds/foo.seeds.rb`. Installation ============ @@ -148,35 +149,20 @@ end ### Defining and using methods As seed files are evaluated within a single runner in dependency order, any methods defined earlier in the run will be available across dependent tasks. I -recommend keeping method definitions in the seed file that uses them. Alternatively if you have many common methods, put them into a module and extend the -runner with the module. +recommend keeping method definitions in the seed file that uses them. Alternatively if you have many common methods, you can put them into `db/global.seeds.rb`. -db/seeds/support.rb +db/global.seeds.rb ```ruby -module Support - def notify(filename) - puts "Seeding: #{filename}" - end +def notify(filename) + puts "Seeding: #{filename}" end ``` db/seeds/common.seeds.rb ```ruby -require_relative 'support' -extend Support - notify(__FILE__) ``` -To keep this dry you could make the seeds dependent on a support seed that extends the runner. - -db/seeds/users.seeds.rb -```ruby -after :common do - notify(__FILE__) -end -``` - Contributors ============ ```shell @@ -196,6 +182,7 @@ git log | grep Author | sort | uniq * pivotal-cloudplanner * vkill * Aleksey Ivanov +* Stefan Wrobel Note on Patches/Pull Request ============================ diff --git a/lib/seedbank/dsl.rb b/lib/seedbank/dsl.rb index 3e8b778..30b698b 100644 --- a/lib/seedbank/dsl.rb +++ b/lib/seedbank/dsl.rb @@ -36,6 +36,7 @@ def define_seed_task(seed_file, *args) task.add_description "Load the seed data from #{relative_file}" add_environment_dependency(task) + add_global_dependency(task) task.name end @@ -43,6 +44,10 @@ def original_seeds_file @_seedbank_original ||= existent(Pathname.new('../seeds.rb').expand_path(seeds_root)) end + def global_seeds_file + @_seedbank_global ||= existent(Pathname.new('../global.seeds.rb').expand_path(seeds_root)) + end + def seeds_root Pathname.new Seedbank.seeds_root end @@ -70,6 +75,13 @@ def add_environment_dependency(task) end end + def add_global_dependency(task) + if global_seeds_file + define_seed_task(global_seeds_file) + task.enhance(['db:seed_global']) + end + end + def runner @_seedbank_runner ||= Seedbank::Runner.new end diff --git a/lib/tasks/seed.rake b/lib/tasks/seed.rake index 1cea8cb..f658b15 100644 --- a/lib/tasks/seed.rake +++ b/lib/tasks/seed.rake @@ -14,7 +14,7 @@ namespace :db do common_dependencies.unshift('db:seed:original') end - desc "Load the seed data from db/seeds.rb and db/seeds/#{Seedbank.matcher}." + desc "Load the seed data from db/global.seeds.rb, db/seeds.rb and db/seeds/#{Seedbank.matcher}." task 'common' => common_dependencies # Glob through the directories under seeds_path and create a task for each adding it to the dependency list. @@ -24,7 +24,7 @@ namespace :db do environment_dependencies = seed_tasks_matching(environment, Seedbank.matcher) - desc "Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/#{environment}/#{Seedbank.matcher}." + desc "Load the seed data from db/global.seeds.rb, db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/#{environment}/#{Seedbank.matcher}." task environment => ['db:seed:common'] + environment_dependencies override_dependency << "db:seed:#{environment}" if defined?(Rails) && Rails.env == environment @@ -32,7 +32,7 @@ namespace :db do end # Override db:seed to run all the common and environments seeds plus the original db:seed. - desc %(Load the seed data from db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/ENVIRONMENT/#{Seedbank.matcher}. + desc %(Load the seed data from db/global.seeds.rb, db/seeds.rb, db/seeds/#{Seedbank.matcher} and db/seeds/ENVIRONMENT/#{Seedbank.matcher}. ENVIRONMENT is the current Rails.env.) override_seed_task seed: override_dependency end diff --git a/test/dummy/db/global.seeds.rb b/test/dummy/db/global.seeds.rb new file mode 100644 index 0000000..e69de29 diff --git a/test/lib/tasks/seed_rake_test.rb b/test/lib/tasks/seed_rake_test.rb index 2b21f18..be782cd 100644 --- a/test/lib/tasks/seed_rake_test.rb +++ b/test/lib/tasks/seed_rake_test.rb @@ -21,7 +21,7 @@ def self.glob_dummy_seeds it 'creates all the seed tasks' do seeds = %w[db:seed:circular1 db:seed:circular2 db:seed:common db:seed:dependency db:seed:dependency2 db:seed:dependent db:seed:dependent_on_nested db:seed:dependent_on_several db:seed:development - db:seed:development:users db:seed:no_block db:seed:original db:seed:reference_memos db:seed:with_block_memo db:seed:with_inline_memo] + db:seed:development:users db:seed:global db:seed:no_block db:seed:original db:seed:reference_memos db:seed:with_block_memo db:seed:with_inline_memo] subject.map(&:to_s).must_equal seeds end @@ -35,7 +35,7 @@ def self.glob_dummy_seeds subject { Rake.application.lookup(['db', 'seed', seed].join(':')) } it 'is dependent on db:abort_if_pending_migrations' do - subject.prerequisites.must_equal %w[db:abort_if_pending_migrations] + subject.prerequisites.must_equal %w[db:abort_if_pending_migrations db:seed:global] end end end @@ -66,7 +66,7 @@ def setup end it 'is dependent on only the common seeds' do - prerequisite_seeds = self.class.glob_dummy_seeds.sort.map do |seed_file| + prerequisite_seeds = ['db:seed:global'] + self.class.glob_dummy_seeds.sort.map do |seed_file| ['db', 'seed', File.basename(seed_file, '.seeds.rb')].join(':') end