Skip to content

Instantly share code, notes, and snippets.

@joxxoxo
Created May 23, 2013 21:31
Show Gist options
  • Save joxxoxo/5639590 to your computer and use it in GitHub Desktop.
Save joxxoxo/5639590 to your computer and use it in GitHub Desktop.
to load seeds when test db recreated
db_namespace = namespace :db do
namespace :test do
# desc "Recreate the test database from a fresh schema.rb file"
task :clone => %w(db:schema:dump db:test:load_schema db:test:load_required_seeds)
# desc "Recreate the test database from a fresh structure.sql file"
task :clone_structure => [ "db:structure:dump", "db:test:load_structure", "db:test:load_required_seeds"]
desc 'Check for pending migrations and load the test schema, then load seeds'
task :prepare => 'db:abort_if_pending_migrations' do
unless ActiveRecord::Base.configurations.blank?
# Default behavior
db_namespace[{ :sql => 'test:clone_structure', :ruby => 'test:load' }[ActiveRecord::Base.schema_format]].invoke
#
# added seeds load
db_namespace['test:load_required_seeds'].invoke
# end of added behavior
end
end
# desc "Recreate the test database from the current schema"
task :load => 'db:test:purge' do
# Default behavior
case ActiveRecord::Base.schema_format
when :ruby
db_namespace["test:load_schema"].invoke
when :sql
db_namespace["test:load_structure"].invoke
end
#
# added seeds load
db_namespace['test:load_required_seeds'].invoke
# end of added behavior
end
desc 'Loads required seeds'
task :load_required_seeds do
rails_env_was = Rails.env
Rails.env = 'test'
begin
require "factory_girl_rails"
puts 'Loading seeds for test in DB...'
require Rails.root.join('spec/test_specific_seeds')
puts 'Seeds loaded'
rescue => e
puts "Seeds not loaded: #{e}\n#{e.backtrace * "\n"}"
ensure
Rails.env = rails_env_was
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment