Created
March 22, 2012 19:05
-
-
Save gilderlan/2161856 to your computer and use it in GitHub Desktop.
Rakefile for Sinatra and Active-Record
This file contains hidden or 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
require 'rubygems' | |
require 'active_record' | |
require 'yaml' | |
namespace :db do | |
task :environment do | |
@config = YAML::load(File.open(File.dirname(__FILE__) + '/db/database.yml')) | |
ActiveRecord::Base.establish_connection(@config) | |
end | |
desc "Create database" | |
task :create do | |
config = YAML::load(File.open(File.dirname(__FILE__) + '/db/database.yml')) | |
begin | |
ActiveRecord::Base.establish_connection(config) | |
ActiveRecord::Base.connection | |
rescue Exception => e | |
puts e | |
end | |
end | |
desc "Drop database" | |
task :drop => :environment do | |
begin | |
ActiveRecord::Base.connection.drop_database @config['database'] | |
puts "database successfully deleted" | |
rescue Exception => e | |
puts e | |
end | |
end | |
desc "Migrate the database" | |
task :migrate => :environment do | |
begin | |
ActiveRecord::Migrator.migrate('db/migrate') | |
rescue Exception => e | |
puts e | |
end | |
end | |
desc 'Load the seed data from db/seeds.rb' | |
task :seed => :environment do | |
seed_file = File.join('db/seeds.rb') | |
load(seed_file) if File.exist?(seed_file) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment