Skip to content

Instantly share code, notes, and snippets.

@xaviershay
Created November 4, 2011 19:22
Show Gist options
  • Save xaviershay/1340242 to your computer and use it in GitHub Desktop.
Save xaviershay/1340242 to your computer and use it in GitHub Desktop.
db:structure:load for Rails 3.1
# Adapter from http://pivotallabs.com/users/jdean/blog/articles/1707-using-mysql-foreign-keys-procedures-and-triggers-with-rails
namespace :db do
namespace :structure do |schema|
schema[:dump].abandon
desc "OVERWRITTEN - shell out to mysqldump"
task :dump => :environment do
cmd = "mysqldump #{mysql_options} -d --routines --triggers --skip-comments > db/development_structure.sql"
system cmd
File.open("#{Rails.root}/db/#{Rails.env}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
end
end
namespace :test do |schema|
schema[:clone_structure].abandon
desc "OVERWRITTEN - load the development_structure file using mysql shell"
task :clone_structure => ["db:structure:dump", "db:test:purge"] do
config = ActiveRecord::Base.configurations['test']
cmd = "mysql #{mysql_options} < db/development_structure.sql"
system cmd
end
end
namespace :structure do |schema|
desc "load the development_structure file using mysql shell"
task :load => :environment do
config = ActiveRecord::Base.configurations[Rails.env]
cmd = "mysql #{mysql_options} < db/development_structure.sql"
system cmd
end
end
def mysql_options
config = ActiveRecord::Base.configurations[Rails.env]
pwd = "-p#{config['password']} " if config['password'].to_s.length > 0
"-u#{config['username'] || config['user']} #{pwd} #{config['database']}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment