07 MarRollback migrations with an easy command
Monday, 07 March 2011 — 12:44There are some times in Rails development, where you need to test your migrations several times. Most of the times i have to look at cheat pages to remember how i can rollback migrations, which is the version number, and then run it in console. Too much for a rails developers. So i’d like to have something like this:
rake db:migrate:rollback
So create a new file under lib/tasks in your project and name it db_rollback.rake. Copy there this:
namespace :db do namespace :migrate do desc "Rolls the schema back to a previous version. Specify the number of steps with STEP=n" task :rollback => :environment do step = ENV['STEP'] ? ENV['STEP'].to_i : 1 ActiveRecord::Migrator.rollback('db/migrate/', step) end end end
and you will be ready to go. You will be able to specify even more than one steps to rollback with STEP=n. Enjoy it!
Ger