在 Rails 中转换数据库结构/数据

发布于 2024-07-20 06:12:16 字数 497 浏览 2 评论 0原文

这是这个问题的后续: Can Rails Migrations be use转换数据?

如果我正在开发一个分支,当它重新集成并发布到生产环境时,数据库架构将发生相当大的变化。 确保生产中的所有数据都转换为新格式的最佳方法是什么?

一个简单的例子是,有一个数字列,我们想要将其更改为文本并对数据进行一些转换工作。 然后我们要删除旧的列。

我被建议不要在迁移中进行任何数据操作,而是为此创建 rake 任务。 是否有一种机制可以确保 rake 任务可以与迁移一起按顺序运行?

现在,我能想到的唯一解决方案是将所有将失效列放入第二个集合的迁移捆绑在一起。 运行添加新表的第一组迁移。 运行 rake 任务,然后运行第二组迁移。 对我来说这似乎不是一个理想的解决方案,而且很容易出错。

This is kind of a follow up to this question: Can Rails Migrations be used to convert data?

If I'm working on a branch that, when it is re-integrated and released to production, will change the database schema quite drastically. What is the best way of ensuring that all the data in production gets converted to the new format?

A simple example would be, there is a numeric column which we want to change to text and do some conversion work on the data. Then we want to drop the old column.

I've been advised not to do any data manipulation in migrations but rather to create rake tasks for this. Is there a mechanism for ensuring rake tasks can be ran in order alongside migrations?

Right now, the only solution I can think of is to bundle up all the migrations that drop the defunct columns into a second collection. Run the first set of migrations that add the new tables. Run the rake tasks, then run the second set of migrations. This doesn't seem like an ideal solution to me and could easily go wrong.

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

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

发布评论

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

评论(1

莫言歌 2024-07-27 06:12:16

迁移正是针对此类内容。 将数据库中的数据转换为新版本的应用程序,无论是否更改架构,都应该是一次迁移。 这可以保证在新版本的应用程序上线之前在生产中完成新的更改。

当您在分支中工作时,只需创建一个新的迁移,这将创建一个新的带时间戳的文件。 当您合并回发布分支时,该文件将被复制。 由于时间戳的存在,迁移(可能)将按正确的顺序执行。

但是,如果您正在进行的转换将继续不时执行,那么您应该使用 Rake 任务。

Migrations are exactly for this kind of stuff. Converting data in the database for a new version of the application, with or without a schema change, should be a migration. This is guarantee that new changes are done in production before the new version of the application goes live.

When you're working in the branch, just create a new migration, which will create a new time stamped file. When you merge back into the release branch, the file will be just be copied over. Because of the timestamp, the migrations will (likely) execute in the correct order.

If the conversion you're doing, though, will continue to be executefrom time to time, then you should use a Rake task.

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