Ruby on Rails: How can I revert a migration with rake db:migrate?
Run the following command
rake db:migrate:down VERSION=<version>
where <version>
is the version number of your migration file you want to revert.
eg. if you want to revert a migration with file name 3846656238_create_users.rb
rake db:migrate:down VERSION=3846656238
Just run this command:
rake db:rollback
I believe there are three options available for reverting migrations (they also overlap):
-
Roll down the most recent migration:
rake db:migrate:down
# Rails 2 only. -
Roll down a number(n) of recent migrations:
rake db:rollback STEP=n
-
Roll down to a previous, specific version:
$ rake db:migrate:down VERSION=nnn
# Rails 3 (provide version number also).
Version Number means the SHA(Secure Hash Algorithm) for the commit which is a long hexadecimal number which looks something like 886af3194768917c78e... You can see it by doing git log
You can see these commands (and others) with their descriptions by using rake -T db:
which for rails 3.2 includes:
rake db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false)
rake db:migrate:status # Display status of migrations
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
You can do rollback and specify how many last migrations will be rollbacked, e.g.
rake db:rollback STEP=3
for 3 last migrations.