How to rollback Rails database migrations
When you perform database migrations in Rails you use the following command:
rake db:migrateIf you want to roll back your change to need need to find out what version your database is currently at and then roll back to a previous version:
rake db:migrate VERSION=<version to roll back to>I've never liked this way to roll back, seems to much like hard work to me so I found a migration rollback script on the programmingishard site and simplified so that it only rollback by one version:
namespace :db do
namespace :migrate do
desc "Rollback the database schema to the previous version"
task :rollback => :environment do
previous_version = ActiveRecord::Migrator.current_version.to_i - 1
ActiveRecord::Migrator.migrate("db/migrate/", previous_version)
puts "Schema rolled back to previous verison (#{previous_version})."
end
end
endTo use this scriptlet, copy and paste the code into a file called db_rollback.rake and place it the lib/tasks directory within your Rails application.To use it to roll back your most recent migration simply run:
rake db:migrate:rollbackTechnorati Tags: Ruby, Rails, Database, Migrations, Rake, Rollback, Schema, Andrew Beacock


Buy Stuff From Amazon 
11 Comments:
Behold, for I am become a cow, the mo-oer of worlds.
I tried a :
"rake db:migrate VERSION=X" to see what it did ..."
It removed all my my database tables
DON'T DO IT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
Useful info. I managed to rollback using "rake db:migrate VERSION=002" where the current version was named "003_add_test_data.rb" and the previous version named "002_add_price.rb". Thanks for the help.
Paul, you can also just use "2" as your version number as Rails equates "002" as "2" internally anyway.
Glad you found my post some use though! :)
rake db:migrate:down VERSION=20090326173033
might help
Thanks for this tip.
Just a minor point, you are missing an "end" towards the end of your script.
Cheers Puneet, I'll update it now!
Also, it should be pointed out that this only works with the sequential migration numbering which I suppose, was used in older rails versions.
If your rails version use timestamp based naming of migrations, you can add the following to your environment.rb to force usage of sequential migration numbers.
config.active_record.timestamped_migrations = false
Puneet, again thanks for the info! I've not played with Rails for nearly two years now (I changed jobs) so it's good to get advice on how it's changed since I wrote these posts back in 2007.
I had problems with the code. The current_version.to_i - 1 took the current version and just did a minus one. so 20100329180520 became 20100329180519 which is not what is desired. I've made some changes to the code that use the rollback method instead.
namespace :db do
namespace :migrate do
desc "Rollback the database schema to the previous version"
task :rollback => :environment do
ActiveRecord::Migrator.rollback("db/migrate/")
previous_version= ActiveRecord::Migrator.current_version.to_i
puts "Schema rolled back to previous verison (#{previous_version})."
end
end
end
Cheers for your updated code, this migration rollback code is not intended for the date-based versioning (this wasn't available at the time of the original post).
I don't work with Rails anymore so thank you for the correction! :)
Post a Comment