Rails:重构迁移

发布于 2024-11-19 13:14:37 字数 376 浏览 0 评论 0原文

数据库迁移的概念对我来说是新的,但同时它也非常棒。 :)

我很好奇迁移的正确使用,因为我需要知道如何通过迁移正确添加/删除/修改表中的列。过去,我总是通过图形用户界面直接编辑数据库。 :/

添加新列的最佳方法是什么?我尝试过使用rails g migration :new_col => :attributes,但是如果我要创建/删除/摆弄许多列,这似乎是一件很麻烦的事情。

我现在最大的恐惧是学习如何使用实时数据库来做到这一点 - 我不能冒险编辑实时数据库,但我需要能够添加新的 user_profile 字段和诸如此类的。

我有兴趣首先听听具体的最佳方法,然后听听您的建议和过去的经验。

谢谢,SO 社区基本上给了我一个准教育。

The concept of db migrations is new to me, but it's pretty awesome at the same time. :)

I'm curious about the proper use of migrations in that I need to know how to properly add/remove/modify columns in tables via migrations. In the past, I've always directly edited the databases via a gui. :/

What's the best way to add a new column? I've tried using rails g migration :new_col => :attributes, but this seems like a messy thing to do if I'm going to be creating/removing/fiddling with many columns.

My biggest fear right now is learning how to do this with a live database--I can't go around hazardously editing the live database, but I need to be able to add, say new user_profile fields and whatnot.

I'm interested to hear first specifically the best way to go about this, and secondly your recommendations and past experience in doing this.

Thanks, SO community for basically giving me a quasi education.

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

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

发布评论

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

评论(1

爱*していゐ 2024-11-26 13:14:37

应用多个数据库更改时,您可以将这些更改分成多个命令,并在迁移文件中各行上显示。如果某个操作需要计算,您还可以将 Ruby 代码嵌入到这些方法中,就像您在任何其他 Ruby 类中工作一样。您还可以在 ActiveRecord 之外执行 SQL 来执行特定于数据库的操作,或者对于 ORM 来说过于复杂而无法有效管理的操作:

可以在此处找到有关迁移方法的更多信息:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

至于在实时环境中执行此操作,我将首先在临时环境中测试您的迁移,该环境是您实时环境的镜像。如果暂存成功,我将在生产中运行任何迁移之前备份实时数据库。

警告一下,如果迁移中途失败,您很容易陷入半应用状态,其中 rake db:migrate:down 也会失败。这些操作可能需要了解 DDL 语法(或生成此 DDL 的 GUI 程序)才能解决。这就是为什么在生产中应用迁移之前良好的分阶段试运行非常重要。

When applying multiple database changes, you can separate these into multiple commands on their own lines inside the migration file. If an operation requires calculations, you can also embed Ruby code into these methods, the same as if you were working in any other Ruby class. You can also execute SQL outside of ActiveRecord to do database specific operations, or operations which are too complex for an ORM to manage effectively:

More information on Migration methods can be found here:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

As for doing this in a live environment, I would first test your migrations in a staging environment that is a mirror of your live environment. If staging is successful, I would then backup the live database before running any migrations in production.

A word of warning, if a migration fails part way through, you can easily get stuck in a half applied state, where rake db:migrate:down will also fail. These operations may require an understanding of DDL syntax (or a GUI program to generate this DDL) to resolve. That is why a good staging dry-run is important before applying migrations in production.

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