Created
December 5, 2011 19:25
-
-
Save verticonaut/1434888 to your computer and use it in GitHub Desktop.
Seed script differentiating for environments (for yaml and rb files)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pp' | |
puts "Seeding for env '#{Rails.env}'" | |
# disable AR logger | |
old_logger = ActiveRecord::Base.logger | |
ActiveRecord::Base.logger = nil unless $VERBOSE | |
env_seed_file = "#{Rails.root}/db/seeds/#{Rails.env.downcase}/seeds.rb" | |
# first load yaml files that is "base" loading | |
seed_data_files = Dir.glob("#{Rails.root}/db/seeds/all/**/*.yaml")+Dir.glob("#{Rails.root}/db/seeds/#{Rails.env.downcase}/**/*.yaml") | |
seed_data_files.each { |seed_data_file| | |
puts "Seeding #{seed_data_file}" unless %w[test cucumber].include?(Rails.env) | |
YamlDb.load seed_data_file | |
} | |
# then load rb files that depend on yamls and have some logic (sequence currently ignored) | |
seed_rb_files = Dir.glob("#{Rails.root}/db/seeds/all/**/*.rb")+Dir.glob("#{Rails.root}/db/seeds/#{Rails.env.downcase}/**/*.rb") | |
seed_rb_files.each { |seed_rb_file| | |
puts "Seeding #{seed_rb_file}" unless Rails.env == 'cucumber' | |
load(seed_rb_file) | |
} | |
load(env_seed_file) if File.exist?(env_seed_file) | |
# reenable logger | |
ActiveRecord::Base.logger = old_logger if old_logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment