Rails:'schema.rb' 中的版本号是否用于任何用途? [英] Rails: Is the version number in 'schema.rb' used for anything?

查看:14
本文介绍了Rails:'schema.rb' 中的版本号是否用于任何用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在 Rails 有了 时间戳迁移/db/schema.rb 顶部的单个版本号似乎毫无意义.在与多个开发人员或多个分支打交道时,有时版本号最终会不正确.

Now that Rails has timestamped migrations, the single version number at the top of /db/schema.rb seems pointless. Sometimes the version number ends up incorrect when dealing with multiple developers or multiple branches.

Rails 甚至不再使用那个 :version 参数了吗?

Does Rails even utilize that :version parameter anymore?

它不正确有什么害处吗(例如:它不反映最近应用提交的时间戳)?

And is there any harm in it being incorrect (as in: it doesn't reflect the timestamp of most recently applied commit)?

例子:

ActiveRecord::Schema.define(:version => 20100417022947) do
  # schema definition ...
end

推荐答案

其实版本比这个重要多了.您引用的代码实际上只是假设_migrated_upto_version 所做的一小部分.迁移版本的真正效果是 所有先前的迁移 (在 db/migrate 目录中找到) 假定已运行.(所以,是的,它按照函数名的意思做.)

Actually, the version is much more important than this. The code you've cited is actually only a small part of what assume_migrated_upto_version does. The real effect of the migration version is that all prior migrations (as found in the db/migrate directory) are assumed to have been run. (So yes, it does what the function name suggests.)

这有一些有趣的含义,尤其是在多人同时提交新迁移的情况下.

This has some interesting implications, particularly in the case where multiple people commit new migrations at the same time.

如果您按照 Rails 团队的建议对 schema.rb 进行版本控制,那就没问题了.您 100% 保证有冲突(架构版本),提交/合并用户必须通过合并他们的更改并将 :version 设置为两者中的最高值来解决它.希望他们正确地合并.

If you version your schema.rb, which is what the Rails team recommends, you're okay. You're 100% guaranteed to have a conflict (the schema version), and the committing/merging user has to resolve it, by merging their changes and setting the :version to the highest of the two. Hopefully they do this merge correctly.

一些项目选择通过将 schema.rb 排除在版本控制之外来避免这种持续的冲突问题.他们可能仅依赖于迁移,或者保留他们偶尔更新的架构的单独版本控制副本.

Some projects choose to avoid this continual conflict issue by keeping the schema.rb out of version control. They might rely solely on migrations, or keep a separate version-controlled copy of the schema that they occasionally update.

如果有人使用时间戳之前创建迁移到您的 schema.rb 的 :version,则会出现问题.如果您使用 db:migrate,您将应用他们的迁移,您的 schema.rb 将被更新(但保留相同的更高版本),一切都很好.但是,如果您碰巧使用了 db:schema:load(或 db:reset),您不仅会错过他们的迁移,而且 assume_migrated_upto_version 会将他们的迁移标记为已应用.

The problem occurs if someone creates a migration with a timestamp prior to your schema.rb's :version. If you db:migrate, you'll apply their migration, your schema.rb will be updated (but retain the same, higher :version), and everything is fine. But if you should happen to db:schema:load (or db:reset) instead, you'll not only be missing their migration, but assume_migrated_upto_version will mark their migration as having been applied.

此时最好的解决方案可能是要求用户将他们的迁移重新时间戳到他们的合并时间.

The best solution at this point is probably to require that users re-timestamp their migrations to the time of their merge.

理想情况下,我希望 schema.rb 实际上包含应用迁移编号的列表,而不是假设到这里的 :version.但我怀疑这是否会发生——Rails 团队似乎认为通过检查 schema.rb 文件可以充分解决问题.

Ideally, I would prefer if schema.rb actually contained a list of applied migration numbers rather than an assume-up-to-here :version. But I doubt this will happen -- the Rails team seems to believe the problem is adequately solved by checking in the schema.rb file.

这篇关于Rails:'schema.rb' 中的版本号是否用于任何用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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