Rails中的汇总迁移 [英] Aggregate migrations in Rails

查看:80
本文介绍了Rails中的汇总迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数十笔Rails DB迁移,这些迁移是一年来写的.有没有一种方法可以将它们聚合到一个迁移中,这样我就可以看到数据库的完整DDL语句,因为它现在已经存在?我只需要当前的快照,而无需了解我们如何使用它的所有历史记录.

I have several dozens Rails DB migrations which were written over a year. Is there a way to aggregate them to one migration so that I will just see a full DDL statement for the database as it exists now? I just need the current snaphot without all the history of how we got to it.

推荐答案

可以,但是可能不是一个好主意来汇总迁移!

It is possible, but probably not a good idea to aggregate the migrations!

也许会问:

  • 您为什么要这样做?
  • 您真正需要多长时间迁移一次到VERSION = 0,然后再次备份?
  • 是不是真的坏了? (如果没有,那就不要修复它)

我曾经遇到过同样的问题..我最终只是重新排序了迁移,因为架构中的更改导致它不再能够正确地向上/向下迁移.我会犹豫再做一次.

I've had the same problem once.. I ended up just re-ordering my migrations, because changes in the schema caused it to not correctly migrate up/down anymore. I would be hesitant to do that again.

如果您的迁移仅添加字段或索引,则可以将它们与模型的主要迁移结合起来,但是请注意,您不再能够重现旧的情况,例如较旧的DB转储可能与其应兼容的迁移号不兼容-这可能是反对汇总的最大理由...

If you have migrations which just add fields or indexes, then maybe you can combine them with the main migration for the model -- but beware that you can't reproduce old situations anymore, e.g. older DB-dumps may not be compatible with what migration number they should be compatible with -- that is probably the biggest argument against aggregating...

从技术上讲,您可以转储模式,然后直接加载它-这是一种方法:

Technically, you can dump the schema and then load it directly - that is one way:

rake db:schema:dump

然后使用架构转储文件db/schema.rb的内容创建一个新的迁移

then create a single new migration with the contents of the schema dump file db/schema.rb

以下是一些类似的问题:

Here are some similar questions:

删除/重新设置"; Rails迁移

平展"方式Rails迁移?

我应该展平Rails迁移吗?

P.S .:我发现坚持旧的迁移编号方案非常有用,因为在这种迁移方案中,迁移不使用时间戳-对​​我来说,效果更好(更容易看出它们的顺序).

P.S.: I found it useful to stick with the old migration numbering scheme, where the migrations do not use timestamps - for me this works better (is easier to see in which order they are).

例如在您的config/application.rb文件中:

e.g. in your config/application.rb file:

config.active_record.timestamped_migrations = false

这篇关于Rails中的汇总迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆