Skip to content

Instantly share code, notes, and snippets.

@dsueiro
Forked from lukemelia/databases.rake
Created May 23, 2011 20:46
Show Gist options
  • Save dsueiro/987568 to your computer and use it in GitHub Desktop.
Save dsueiro/987568 to your computer and use it in GitHub Desktop.
Our replacement for schema.rb dump/load
namespace :db do
namespace :structure do
desc "Dump the database structure to a SQL file"
task :dump => :load_config do
config = ActiveRecord::Base.configurations[Rails.env]
command = "mysqldump -u #{config["username"]} #{config["database"]} --no-data --skip-comments --skip-add-drop-table > db/#{Rails.env}_structure.sql"
puts "Running: #{command}"
system command
command = "mysqldump -u #{config["username"]} #{config["database"]} --skip-extended-insert --no-create-info --skip-comments --tables schema_migrations >> db/#{Rails.env}_structure.sql"
puts "Running: #{command}"
system command
end
end
end
namespace :db do
namespace :test do
desc "Load the test database from a SQL file"
task :prepare => "db:load_config" do
config = ActiveRecord::Base.configurations["test"]
command = "mysqladmin -u #{config["username"]} drop -f #{config["database"]}"
puts "Running: #{command}"
system command
command = "mysqladmin -u #{config["username"]} create #{config["database"]}"
puts "Running: #{command}"
system command
command = "mysql -u #{config["username"]} #{config["database"]} < db/development_structure.sql"
puts "Running: #{command}"
system command
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment