Skip to content

Instantly share code, notes, and snippets.

@echohn
Last active July 18, 2016 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save echohn/0f4d277c245e626ef5649533e3ec9b91 to your computer and use it in GitHub Desktop.
Save echohn/0f4d277c245e626ef5649533e3ec9b91 to your computer and use it in GitHub Desktop.
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