When you perform database migrations in Rails you use the following command:
To use it to roll back your most recent migration simply run:
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
Comments
"rake db:migrate VERSION=X" to see what it did ..."
It removed all my my database tables
DON'T DO IT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
Glad you found my post some use though! :)
might help
Just a minor point, you are missing an "end" towards the end of your script.
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
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
I don't work with Rails anymore so thank you for the correction! :)
At present am working on rails 1.2.1 version. And am trying to do rake db:migrate:down VERSION=229 for which am getting "rake aborted!
Don't know how to build task 'db:migrate:down'". Is this the issue related to rails versions or am missing something.
Any idea will be helpful.