Skip to content

Instantly share code, notes, and snippets.

@mattwynne
Created February 28, 2009 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwynne/71918 to your computer and use it in GitHub Desktop.
Save mattwynne/71918 to your computer and use it in GitHub Desktop.
namespace :db do
namespace :features do
def checking_for_config
if ActiveRecord::Base.configurations["features"]
yield
else
puts "Unable to prepare features database - it's not defined in your configuration. Skipping. Preparing test database instead."
Rake::Task['db:test:prepare'].invoke
end
end
desc "Clones the development database into the features environment"
task :prepare => [:environment, 'db:abort_if_pending_migrations'] do
Rake::Task['db:features:prepare!'].invoke
end
desc "Clones the development database into the features environment without checking for pending migrations."
task :prepare! => [:environment, 'db:structure:dump'] do
checking_for_config do
ActiveRecord::Base.establish_connection(:features)
ActiveRecord::Base.connection.recreate_database(ActiveRecord::Base.configurations["features"]["database"])
ActiveRecord::Base.establish_connection(:features)
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
ActiveRecord::Base.connection.execute(table)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment