57

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

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Lucy Weatherford
  • 5,452
  • 16
  • 50
  • 76

4 Answers4

172

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.

Alex Falke
  • 2,178
  • 2
  • 14
  • 11
  • 3
    This answer should be selected to avoid confusion. – Seong Lee Jan 10 '15 at 03:17
  • 2
    `rake db:migrate:reset` doesn't actually rollback any migration, that's what the question is really asking. – Claudio Floreani Aug 27 '15 at 14:59
  • Thank you! I couldn't find this answer anywhere – user1828780 Oct 11 '16 at 01:30
  • 1
    Caution: This will **DROP** your database. – Mr. Alien Dec 27 '17 at 09:34
  • 1
    Claudio Floreani's answer is as correct as this one. On services like Heroku, where you don't have access to drop a database, if you edit a migration, truly *rolling back* may be your only option. – Mark Schneider Aug 22 '18 at 05:42
  • This does NOT answer the OP's question and WILL destroy data. This drops the database (and builds it again...) which is quite a bit different than rolling back all migrations. @ClaudioFloreani answer more accurately below: `rake db:migrate VERSION=0` – David Hempy Jun 03 '19 at 20:26
  • @ClaudioFloreani, Mr.Alien and David Hempy thanks for your feedback. I updated the answer – Alex Falke Jun 04 '19 at 16:42
  • Can anyone tell me Why rake db:migrate:reset is not listed in $ rake -T ? – Ken Ratanachai S. Feb 07 '20 at 10:56
  • «_…the best solution is the one @Claudio Floreani proposed:_ `rake db:migrate VERSION=0` _This will rollback any migrations without losing data._» **Warning** I never said that! **This will delete the database tables and you will lose all stored data**. Please read the reasons why this is a better approach in my answer. – Claudio Floreani Sep 03 '20 at 08:30
  • Thanks, I got confused reading all the answers. I updated this answer. – Alex Falke Sep 07 '20 at 16:10
29

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

Claudio Floreani
  • 2,441
  • 28
  • 34
  • 1
    THIS IS THE MOST CORRECT ANSWER! The other solutions (e.g. db:reset) will destroy data. It is entirely possible that the Rails app was built on a pre-existing database, so the migrations do not describe the entire database. Resetting the database would lose the pre-existing data, as well as the legacy schema itself. – David Hempy Jun 03 '19 at 20:30
16

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.

Orlando
  • 9,374
  • 3
  • 56
  • 53
  • 9
    This is not what the question wants to do. If you change something in the migrations, it will not update the changes in the schema. – Claudio Floreani Jul 08 '15 at 10:43
  • @Alex Falke's got the correct answer to this question. – Qasim Nov 26 '16 at 04:43
  • This does NOT answer the OP's question and WILL destroy data. This drops the database (and builds it again...) which is quite a bit different than rolling back all migrations. @ClaudioFloreani answer more accurately below: rake db:migrate VERSION=0 – David Hempy Jun 03 '19 at 20:31
  • @DavidHempy I think you are making an assumption of what OP wants, what you describe in the comment below (an application that handles data schema outside Rails) is an edge case. This is more oriented to development since you shouldn't be running rollbacks in your production database (if you drop a field it will drop the data of that field too). In most cases, people just want to revert the database because they made a mistake during development, and this way of doing it is ok for that. – Orlando Jun 03 '19 at 20:36
  • The OP was completely unambiguous in her question. – David Hempy Jun 03 '19 at 20:52
0

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!

Alvaro Rodriguez Scelza
  • 3,643
  • 2
  • 32
  • 47