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

Ruby on-RailsRuby on-Rails-3MigrationResetRollback

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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: https://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload

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

Solution 2 - Ruby on-Rails

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

Solution 3 - Ruby on-Rails

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.

Solution 4 - Ruby on-Rails

If a permission problem raises (like it happened to me), maybe you could try dropping all database tables like I did with rubymine (just open database tool window, select all tables and right-click -> drop), it should be similar with other IDEs. Some tables like sqlite_master and sqlite_sequence were conveniently ignored in the drop.

This allowed me to do

rails db:migrate

and everything worked fine. Of course you loose all data!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLucy WeatherfordView Question on Stackoverflow
Solution 1 - Ruby on-RailsAlex FalkeView Answer on Stackoverflow
Solution 2 - Ruby on-RailsClaudio FloreaniView Answer on Stackoverflow
Solution 3 - Ruby on-RailsOrlandoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAlvaro Rodriguez ScelzaView Answer on Stackoverflow