Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] global.seeds.rb prereq file support #70

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 0.5.0
* `db/global.seeds.rb` will always be run before all other seeds
41 changes: 14 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
============
Expand Down Expand Up @@ -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
Expand All @@ -196,6 +182,7 @@ git log | grep Author | sort | uniq
* pivotal-cloudplanner
* vkill
* Aleksey Ivanov
* Stefan Wrobel

Note on Patches/Pull Request
============================
Expand Down
12 changes: 12 additions & 0 deletions lib/seedbank/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ 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

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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming/MemoizedInstanceVariableName: Memoized variable @_seedbank_global does not match method name global_seeds_file. Use @global_seeds_file instead.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

def seeds_root
Pathname.new Seedbank.seeds_root
end
Expand Down Expand Up @@ -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'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end

def runner
@_seedbank_runner ||= Seedbank::Runner.new
end
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/seed.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,15 +24,15 @@ 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}."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [147/132]

task environment => ['db:seed:common'] + environment_dependencies

override_dependency << "db:seed:#{environment}" if defined?(Rails) && Rails.env == environment
end
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}.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [140/132]

ENVIRONMENT is the current Rails.env.)
override_seed_task seed: override_dependency
end
Empty file added test/dummy/db/global.seeds.rb
Empty file.
6 changes: 3 additions & 3 deletions test/lib/tasks/seed_rake_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

['db', 'seed', File.basename(seed_file, '.seeds.rb')].join(':')
end

Expand Down