Skip to content

Instantly share code, notes, and snippets.

@echohn
Last active Jul 18, 2016
Embed
What would you like to do?
active-record database tasks
namespace :db do
desc "Migrate the db"
task :migrate do
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details)
ActiveRecord::Migrator.migrate("db/migrate/")
end
desc "Create the db"
task :create do
connection_details = YAML::load(File.open('config/database.yml'))
admin_connection = connection_details.merge({'database'=> 'mysql',
'schema_search_path'=> 'public'})
ActiveRecord::Base.establish_connection(admin_connection)
ActiveRecord::Base.connection.create_database(connection_details.fetch('database'))
end
desc "drop the db"
task :drop do
connection_details = YAML::load(File.open('config/database.yml'))
admin_connection = connection_details.merge({'database'=> 'mysql',
'schema_search_path'=> 'public'})
ActiveRecord::Base.establish_connection(admin_connection)
ActiveRecord::Base.connection.drop_database(connection_details.fetch('database'))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment