I have a rails project running that defines the standard production:, :development and :test DB-connections in config/database.yml
In addition I have a quiz_developm
In addition to running a migration in a different environment, I also want the schemas in separate files. You can do this from the command line:
RAILS_ENV=quiz_development SCHEMA=db/schema_quiz_development.rb rake db:migrate
But I like the custom rake task approach so I can type this instead:
rake db:with[quiz_development, db:migrate]
Here's the rake task:
namespace :db do
desc "Run :task against :database"
task :with, [:database,:task] => [:environment] do |t, args|
puts "Applying #{args.task} to #{args.database}"
ENV['SCHEMA'] ||= "#{Rails.root}/db/schema_#{args.database}.rb"
begin
oldRailsEnv = Rails.env
Rails.env = args.database
ActiveRecord::Base.establish_connection(args.database)
Rake::Task[args.task].invoke
ensure
Rails.env = oldRailsEnv
end
end
end