Rake db: migration—如何撤销所有迁移并重新执行它们

时间:2023-01-21 00:16:25

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

是否有一个快速的rake db:用于所有迁移的回滚命令?

3 个解决方案

#1


120  

While @Orlandos' response is correct, rake db:reset does not redo all migrations, and, as he says, loads the data from the last schema. If you want to undo all the migrations and then redo them, you should use:

虽然@Orlandos的响应是正确的,但是rake db:reset并没有重做所有的迁移,而且正如他所说,从上一个模式加载数据。如果您想撤消所有迁移,然后重新执行,您应该使用:

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

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

您可以在本文中看到更多信息:rake db:migrate db:reset和db:schema:load之间的区别

#2


17  

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.

您将看到所有迁移仍然存在,但是它们处于“down”(未应用)状态。

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.

其他暗示rake db:reset或rake db:drop(如@Orlando或@Alex Falke的答案)的命令不会做任何回滚:也就是说,它们不会确保每个迁移都是可逆的。

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

此外,当其他用户访问数据库时,rake db:drop不能运行,而回滚可以实时执行(即使通常不建议这样做)。和最后但并非最不重要,简单地删除和重建数据库也将删除模式迁移表:如果有人运行rake db:migrate:状态数据库已下降后,他会回答说“模式迁移表还不存在”,并将没有线索迁移可以应用(除非他知道或可以列出)。

#3


14  

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

只需使用rake db:reset,就会删除数据库(与取消所有迁移相同)并重置到最后一个模式。

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.

更新:一个更正确的方法将使用rake db:迁移:reset。这将删除数据库,再次创建它并运行所有迁移,而不是重新设置为最新的模式。

#1


120  

While @Orlandos' response is correct, rake db:reset does not redo all migrations, and, as he says, loads the data from the last schema. If you want to undo all the migrations and then redo them, you should use:

虽然@Orlandos的响应是正确的,但是rake db:reset并没有重做所有的迁移,而且正如他所说,从上一个模式加载数据。如果您想撤消所有迁移,然后重新执行,您应该使用:

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

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

您可以在本文中看到更多信息:rake db:migrate db:reset和db:schema:load之间的区别

#2


17  

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.

您将看到所有迁移仍然存在,但是它们处于“down”(未应用)状态。

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.

其他暗示rake db:reset或rake db:drop(如@Orlando或@Alex Falke的答案)的命令不会做任何回滚:也就是说,它们不会确保每个迁移都是可逆的。

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

此外,当其他用户访问数据库时,rake db:drop不能运行,而回滚可以实时执行(即使通常不建议这样做)。和最后但并非最不重要,简单地删除和重建数据库也将删除模式迁移表:如果有人运行rake db:migrate:状态数据库已下降后,他会回答说“模式迁移表还不存在”,并将没有线索迁移可以应用(除非他知道或可以列出)。

#3


14  

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

只需使用rake db:reset,就会删除数据库(与取消所有迁移相同)并重置到最后一个模式。

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.

更新:一个更正确的方法将使用rake db:迁移:reset。这将删除数据库,再次创建它并运行所有迁移,而不是重新设置为最新的模式。