Skip to content

Instantly share code, notes, and snippets.

@abstraktor
Created October 7, 2011 07:37
Show Gist options
  • Save abstraktor/1269699 to your computer and use it in GitHub Desktop.
Save abstraktor/1269699 to your computer and use it in GitHub Desktop.
rake task to dump and load sqlseeds in Rails
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