Rake db:migrate - how do I undo all migrations and redo them

Is there a quick rake db:rollback command for all of the migrations?


Rolling back all migrations

To rollback all migrations the best solution is the one @Claudio Floreani proposed:

rake db:migrate VERSION=0

This will rollback every migration. You can read why this is the best approach in his answer. Then, run all migrations again with

rake db:migrate

Resetting the database

Reset

rake db:migrate:reset #runs db:drop db:create db:migrate

This method drops the database and runs the migrations again.

Loading the last schema

rake db:reset

This method will drop the database and load the data from the last schema.

You can see more information in this post: Difference between rake db:migrate db:reset and db:schema:load

Thanks to @Claudio Floreani and all the users who commented to improve the answer.


If you really want to rollback all of the migrations, and not just take the database to a pristine state or to the last schema, you have to run:

rake db:migrate VERSION=0

This will actually rollback all the way down every migration and ensure that every migration is reversible.

If you now issue

rake db:migrate:status

you will see that all the migrations are still there but they are in a 'down' (not applied) state.

Other commands that imply a rake db:reset or rake db:drop (such as in the answers by @Orlando or @Alex Falke) won't do any rollback at all: that is, they won't ensure that every migration is reversible.

Moreover, rake db:drop cannot be run while the database is being accessed by other users, while rollbacks can be performed live (even if this is generally not recommended). And last but not least, simply dropping and recreating the database will also delete the schema migrations table: if someone runs rake db:migrate:status after the database has been dropped, he will be replied with "Schema migrations table does not exist yet" and will have no clues about which migrations can be applied (unless he knows it yet or can list them).


just use rake db:reset, that will drop your database (same as undoing all migrations) and reset to the last schema.

UPDATE: a more correct approach will be using rake db:migrate:reset. That will drop the database, create it again and run all the migrations, instead of resetting to the latest schema.