Created
October 7, 2011 07:37
-
-
Save abstraktor/1269699 to your computer and use it in GitHub Desktop.
rake task to dump and load sqlseeds in Rails
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 :sqlseed do | |
desc "drop database and reload seeds" | |
task :load do | |
# is this what im looking for? | |
# FileList['db/seed.sql'] | |
conf = Rails.configuration.database_configuration[Rails.env] | |
system "mysql -u#{conf['username']} #{conf['database']} <<ENDMYSQL | |
drop database if exists #{conf['database']}; | |
create database #{conf['database']} character set utf8 collate utf8_general_ci; | |
use #{conf['database']}; | |
\nENDMYSQL" | |
system("mysql -u#{conf['username']} #{conf['database']} < db/seed.sql"); | |
end | |
desc "dump database to db/seed.sql" | |
task :dump do | |
conf = Rails.configuration.database_configuration[Rails.env] | |
system("mysqldump -u#{conf['username']} #{conf['database']} > db/seed.sql"); | |
end | |
desc "clone from specified environment into development, test, db/seed.sql and db/schema.rb" | |
task :cascade_edit => :dump do | |
#dump schema | |
Rake::Task["db:schema:dump"].invoke | |
#load into development database | |
Rails.env = "development" | |
Rake::Task["db:sqlseed:load"].reenable | |
Rake::Task["db:sqlseed:load"].invoke | |
#load into test database | |
Rails.env = "test" | |
Rake::Task["db:sqlseed:load"].reenable | |
Rake::Task["db:sqlseed:load"].invoke | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment