当(如果)巩固ActiveRecord的迁移?ActiveRecord

2023-09-09 21:59:33 作者:谢谢赠我空欢喜

当我将通过我的申请*(S)我积累迁移的迭代。截至刚才有48这样的文件,涵盖约24个月的活动。

As I move through the iterations on my application*(s) I accumulate migrations. As of just now there are 48 such files, spanning about 24 months' activity.

我在考虑把我的电流 schema.rb ,使该基准。

I'm considering taking my current schema.rb and making that the baseline.

我也在考虑删除(受源代码控制,当然)现有的迁移和创建从我的我的当前模式一个不错的闪闪发光的新单迁移?迁移往往喜欢的符号,但耙分贝:模式:转储使用字符串:?我应该关心

I'm also considering deleting (subject to source control, of course) the existing migrations and creating a nice shiny new single migration from my my current schema? Migrations tend to like symbols, but rake db:schema:dump uses strings: should I care?

这是否显得合理? 如果是这样,在什么样的区间将这种做法是否合理? 如果不是,为什么不呢?

Does that seem sensible? If so, at what sort of interval would such an exercise make sense? If not, why not?

和我失去了一些(耙?)任务会为我做?

And am I missing some (rake?) task that would do this for me?

* 对我来说,所有的应用程序都是基于Rails的,但它使用的ActiveRecord迁移似乎符合这个问题任何事情。

* In my case, all apps are Rails-based, but anything that uses ActiveRecord migrations would seem to fit the question.

推荐答案

是的,这是有道理的。有巩固迁移的做法。要做到这一点,只需复制当前模式到迁移,并删除所有早期的迁移。然后你有较少的文件进行管理,并且测试可以跑得更快。 你要小心这样做的,特别是如果你对生产自动运行的迁移。我一般替换,我知道每个人都运行新模式的一个迁移。

Yes, this makes sense. There is a practice of consolidating migrations. To do this, simply copy the current schema into a migration, and delete all the earlier migrations. Then you have fewer files to manage, and the tests can run faster. You need to be careful doing this, especially if you have migrations running automatically on production. I generally replace a migration that I know everyone has run with the new schema one.

其他人有稍微不同的方式来做到这一点。

Other people have slightly different ways to do this.

我一般都没有这样做,直到我们有超过100的迁移,但我们可以发展几个月后打这个。随着项目的成熟,但是,移民常来越来越少,所以你可能不必再做了。

I generally haven't done this until we had over 100 migrations, but we can hit this after a few months of development. As the project matures, though, migrations come less and less often, so you may not have to do it again.

这并不违背最佳做法:一旦你迁移到源代码管理检查,不改变它。我做一个罕见的例外,如果有一个错误,但是这是相当罕见(1 100也许)。原因是,一旦他们在野外,有些人可能已经运行它们。它们被记录为已经完成的分贝。如果您更改它们并为您在新的版本中,其他人也不会得到改变的好处。你可以请人回滚一定的变化,并重新运行它们,但失败的自动化的目的。做的时候,就变得一团糟。最好是单独留在家中。

This does go against a best practice: Once you check in a migration to source control, don't alter it. I make a rare exception if there is a bug in one, but this is quite rare (1 in 100 maybe). The reason is that once they are out in the wild, some people may have run them. They are recorded as being completed in the db. If you change them and check in a new version, other people will not get the benefit of the change. You can ask people to roll back certain changes, and re-run them, but that defeats the purpose of the automation. Done often, it becomes a mess. It's better left alone.