Created
June 2, 2012 03:45
-
-
Save choxi/2856444 to your computer and use it in GitHub Desktop.
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 | |
namespace :migrate do | |
desc "create an empty timestamped migration file" | |
task :new do | |
raise "Usage: rake db:migrate:new NAME='create_cohorts'" if ENV["NAME"].nil? | |
current_migration = Dir.glob("./db/migrate/[0-9]*_*.rb").collect do |file| | |
File.basename(file).split("_").first.to_i | |
end.max.to_i | |
next_migration_number = ActiveRecord::Migration.next_migration_number(current_migration) | |
File.open File.join("db", "migrate", "#{next_migration_number}_#{(ENV["NAME"])}.rb"), "w+" | |
end | |
end | |
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).' | |
task :rollback => :configure_connection do | |
step = ENV['STEP'] ? ENV['STEP'].to_i : 1 | |
ActiveRecord::Migrator.rollback "./db/migrate", step | |
end | |
task :configure_connection do | |
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || 'postgres://admin@localhost/blocapi') | |
ActiveRecord::Base.logger = Logger.new STDOUT | |
end | |
desc "runs database migrations" | |
task :migrate => :configure_connection do | |
ActiveRecord::Migration.verbose = true | |
ActiveRecord::Migrator.migrate File.join("db", "migrate"), ENV['VERSION'] ? ENV['VERSION'].to_i : nil | |
ActiveRecord::Migration.verbose = false | |
File.open File.join("db", "schema.rb"), "w+" do |file| | |
ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, file | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment