Created
January 31, 2012 17:24
-
-
Save jellybob/1711703 to your computer and use it in GitHub Desktop.
Preparing multiple MySQL databases with Rails 2.3
This file contains hidden or 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
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