diff options
| author | Zog | 2018-02-26 17:14:24 +0100 | 
|---|---|---|
| committer | Zog | 2018-02-26 17:14:24 +0100 | 
| commit | 66e034743620172d93158ddc94b49033ee3ee206 (patch) | |
| tree | 34580b602311fe4ec8d16c7710e5d4622df9b8c7 | |
| parent | 206bf218e1b84dbe53683ec2e3f983cd30f7ded1 (diff) | |
| download | chouette-core-66e034743620172d93158ddc94b49033ee3ee206.tar.bz2 | |
Refs #6026; Use SEED_ENV in seedbank, fallback on Rails.env
| -rw-r--r-- | lib/tasks/seeds.rake | 44 | 
1 files changed, 44 insertions, 0 deletions
| diff --git a/lib/tasks/seeds.rake b/lib/tasks/seeds.rake new file mode 100644 index 000000000..8e5bbda26 --- /dev/null +++ b/lib/tasks/seeds.rake @@ -0,0 +1,44 @@ +namespace :db do + +  include Seedbank::DSL + +  base_dependencies   = ['db:seed:original'] +  override_dependency = ['db:seed:common'] + +  namespace :seed do +    # Create seed tasks for all the seeds in seeds_path and add them to the dependency +    # list along with the original db/seeds.rb. +    common_dependencies = glob_seed_files_matching('*.seeds.rb').sort.map { |seed_file| seed_task_from_file(seed_file) } + +    desc "Load the seed data from db/seeds.rb and db/seeds/*.seeds.rb." +    task 'common' => base_dependencies + common_dependencies + +    seeds_environment = ENV.fetch("SEED_ENV", Rails.env) + +    # Glob through the directories under seeds_path and create a task for each adding it to the dependency list. +    # Then create a task for the environment +    glob_seed_files_matching('/*/').each do |directory| +      environment = File.basename(directory) + +      environment_dependencies = glob_seed_files_matching(environment, '*.seeds.rb').sort.map { |seed_file| seed_task_from_file(seed_file) } + +      desc "Load the seed data from db/seeds.rb, db/seeds/*.seeds.rb and db/seeds/#{environment}/*.seeds.rb." +      task environment => ['db:seed:common'] + environment_dependencies + +      override_dependency << "db:seed:#{environment}" if defined?(Rails) && seeds_environment == environment +    end + +    if Rails.version > '4' +      original_seeds_file = Rails.application.paths["db/seeds.rb"].existent.first +    elsif Rails.version > '3.1' +      original_seeds_file = Rails.application.paths["db/seeds"].existent.first +    else +      original_seeds_file = Rails.root.join("db","seeds").children.first.to_s +    end +    define_seed_task original_seeds_file, :original if original_seeds_file +  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/*.seeds.rb and db/seeds/ENVIRONMENT/*.seeds.rb. ENVIRONMENT is the e,nv var SEED_ENV or the current environment in Rails.env.' +  override_seed_task :seed => override_dependency +end | 
