Created
April 21, 2009 08:47
-
-
Save ggonnella/99030 to your computer and use it in GitHub Desktop.
MySQL rake task for rails to copy the content of the production database to the development database. Please note that the current development database will be DROPPED!
This file contains 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 :mysql do | |
desc "Overwrite the whole development db with a copy of the production db" | |
task :init_dev do | |
config = YAML.load(IO.read("config/database.yml")) | |
d = config["development"] | |
p = config["production"] | |
unless d["adapter"] == "mysql" and | |
p["adapter"] == "mysql" | |
raise "This task works only on mysql" | |
end | |
drop_dev_db = "mysql -h #{d["host"]} -u #{d["username"]} "+ | |
"-p#{d["password"]} #{d["database"]} "+ | |
"-e'drop database mafo_d;'" | |
create_dev_db = "mysqladmin create #{d["database"]} -h #{d["host"]} -u #{d["username"]} "+ | |
"-p#{d["password"]} " | |
dump_prod_db = "mysqldump -h #{p["host"]} -u #{p["username"]} "+ | |
"-p#{p["password"]} #{p["database"]} "+ | |
" > /tmp/db_mysql_init_dev.sql" | |
populate_dev_db = "mysql -h #{d["host"]} -u #{d["username"]} "+ | |
"-p#{d["password"]} #{d["database"]} "+ | |
"< /tmp/db_mysql_init_dev.sql" | |
puts drop_dev_db | |
print "(drop the development database) ... " | |
STDOUT.flush | |
`#{drop_dev_db}` | |
puts "done." | |
puts create_dev_db | |
print "(create a new development database) ..." | |
STDOUT.flush | |
`#{create_dev_db}` | |
puts "done." | |
puts dump_prod_db | |
print "(dump the production database) ..." | |
STDOUT.flush | |
`#{dump_prod_db}` | |
puts "done." | |
puts populate_dev_db | |
print "(populate the development database) ..." | |
STDOUT.flush | |
`#{populate_dev_db}` | |
puts "done." | |
`rm /tmp/db_mysql_init_dev.sql` | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also it looks like you left the original database name in the drop db line. Other than that it works like a charm.