Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 387
Lucy Weatherford Avatar asked Apr 24 '13 12:04

Lucy Weatherford


People also ask

How do I undo a rake database?

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

How do I rollback migrations?

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements.


3 Answers

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.

like image 199
Alex Falke Avatar answered Oct 25 '22 18:10

Alex Falke


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).

like image 36
Claudio Floreani Avatar answered Oct 25 '22 18:10

Claudio Floreani


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.

like image 16
Orlando Avatar answered Oct 25 '22 19:10

Orlando