Ruby-on-rails Rake db:migrate - 如何撤消所有迁移并重做它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16191820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:52:11  来源:igfitidea点击:

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

ruby-on-railsdatabaseruby-on-rails-3migration

提问by Lucy Weatherford

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

是否有针对所有迁移的快速 rake db:rollback 命令?

回答by Alex Falke

Rolling back all migrations

回滚所有迁移

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

要回滚所有迁移,最好的解决方案是@Claudio Floreani 提出的解决方案:

rake db:migrate VERSION=0

This will rollback any migrations without losing data. Then, run all migrations again with

这将回滚任何迁移而不会丢失数据。然后,再次运行所有迁移

rake db:migrate


Resetting the database (this will drop all data)

重置数据库(这将删除所有数据)

Both methods explained below will drop your database and you will lose all the data in them, so you should only use them if you are sure what you are doing.

下面解释的两种方法都会删除您的数据库,并且您将丢失其中的所有数据,因此只有在您确定自己在做什么时才应该使用它们。

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

您可以在这篇文章中看到更多信息:rake db:migrate db:reset 和 db:schema:load 的区别

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

感谢@Claudio Floreani 和所有评论以改进答案的用户。

回答by Claudio Floreani

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:resetor 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.

暗示 a rake db:resetor 的其他命令rake db:drop(例如@Orlando 或@Alex Falke 的答案)根本不会进行任何回滚:也就是说,它们不会确保每次迁移都是可逆的。

Moreover, rake db:dropcannot 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:statusafter 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:status在删除数据库后运行,他将回复“架构迁移表尚不存在”,并且不会有任何线索可以应用哪些迁移(除非他知道或可以列出它们)。

回答by Orlando

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:migrate:reset。这将删除数据库,再次创建它并运行所有迁移,而不是重置为最新模式。

回答by Loaderon

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.

如果出现权限问题(就像发生在我身上),也许您可​​以像我使用 ruby​​mine 一样尝试删除所有数据库表(只需打开数据库工具窗口,选择所有表并右键单击 -> 删除),它应该类似于其他 IDE。一些表如 sqlite_master 和 sqlite_sequence 在 drop 中被方便地忽略。

This allowed me to do

这让我可以做

rails db:migrate

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

一切正常。当然,您会丢失所有数据!