Last active
July 18, 2016 14:56
-
-
Save echohn/0f4d277c245e626ef5649533e3ec9b91 to your computer and use it in GitHub Desktop.
active-record database tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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