Skip to content

Instantly share code, notes, and snippets.

@jellybob
Created January 31, 2012 17:24
Show Gist options
  • Save jellybob/1711703 to your computer and use it in GitHub Desktop.
Save jellybob/1711703 to your computer and use it in GitHub Desktop.
Preparing multiple MySQL databases with Rails 2.3
namespace :db do
namespace :test do
task :prepare => :environment do
abcs = ActiveRecord::Base.configurations
[ "", "_data_warehouse" ].each do |db|
# Dump the development database
config = abcs["development#{db}"]
cmd = "mysqldump -u#{config["username"]} -p#{config["password"]} #{config["database"]} --no-data > #{RAILS_ROOT}/db/development#{db}_structure.sql"
`#{cmd}`
# Recreate an empty test database
config = abcs["test#{db}"]
ActiveRecord::Base.establish_connection("test#{db}")
ActiveRecord::Base.connection.recreate_database(config["database"], config)
# Import the dump
cmd = "mysql -u#{config["username"]} -p#{config["password"]} #{config["database"]} < #{RAILS_ROOT}/db/development#{db}_structure.sql"
`#{cmd}`
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment