如何避免 ruby​​ 中的错误回滚迁移?

发布于 2024-07-25 00:37:54 字数 108 浏览 2 评论 0原文

我在迁移中的“self.drop”中有一个错误,因此我无法回滚到该迁移之后。 我如何从头开始并从迁移 001 开始构建? 另外,有没有办法做到这一点而不丢失我的数据(这只是测试,但仍然......)

i have a bug in a "self.drop" in a migration such that I cannot roll back past that migration. how can i start from scratch and build up from migration 001? also, is there a way to do this without losing my data (it's just testing, but still...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别闹i 2024-08-01 00:38:02

我意识到这是一个老问题,但恕我直言仍然相关。 如果您想从头开始重新创建数据库,则不会回滚并重新运行迁移,而只是这样做。

rake db:setup

这将删除、创建数据库并使用所有迁移的当前组合状态填充数据库。 这是因为,在大型系统中,运行所有迁移可能不再有效。 但是,运行 schema.rb 将始终有效。

我的建议还包括尽可能避免回滚迁移,除非您刚刚创建迁移并希望在将迁移推送到团队或任何部署之前进行一些添加/拼写错误/修复。

I realise this is an old question, but imho still relevant. If you want to recreate your database from scratch, one does not rollback and rerun migration, one just does

rake db:setup

This will drop, create and fill the database with the current combined state of all the migrations. This is because, definitely in a large system, running all the migrations might no longer work. However, running the schema.rb will always work.

My advice would also be to avoid rollback of migrations as much as possible, unless when you just created a migration and want to make some addition/typo/fix, before having pushed the migration to your team or any deployment.

め可乐爱微笑 2024-08-01 00:38:01
rake db:drop
rake db:create
rake db:migrate

它将重置您的数据库并运行所有迁移。 如果您不想丢失数据,可以使用插件 yaml_db 保存数据:

rake db:data:dump  # stores all data in db/data.yml
...
rake db:data:load  # loads db/data.yml to database

如果迁移过程中出现错误,您可以对其进行编辑,然后尝试回滚。

rake db:drop
rake db:create
rake db:migrate

It will reset your database and run all migrations. If you don't want to lose your data you can save it using plugin yaml_db:

rake db:data:dump  # stores all data in db/data.yml
...
rake db:data:load  # loads db/data.yml to database

If you have an error in a migration you can edit it and then try to rollback.

神经暖 2024-08-01 00:38:00

您可以注释 self.down 迁移和回滚到以前的数据库版本中的所有语句。

然后使用 gui/web 数据库客户端手动应用更改,以在迁移之前匹配数据库架构。

之后您将能够再次运行迁移,并且您的数据不会丢失。

You can comment all statements in self.down migration and rollback to previous db version.

Then apply changes by hand using a gui/web db client to match db schema before migration.

After you will be able to run migration again and your data will not be lost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文