管理 Git 操作创建的 schema.rb 中的冲突
我创建了一个迁移,运行了 rake db:migrate,这提高了我的 db/schema.rb 版本号。然后我做了一个 git fetch origin master 并看到我的团队成员进行了更改。所以我做了一个 git stash 和一个 git rebase FETCH_HEAD ,然后是一个 git stash pop 。这导致 db/schema.rb 版本号发生冲突。
Upstream>>>
ActiveRecord::Schema.define(:version => 20110930179257) do
===========
ActiveRecord::Schema.define(:version => 20110930161932) do
<<<Stashed
我认为适当的修复方法是手动将版本号增加到高于上游的版本号。
这是明智的还是坏消息?
谢谢, 最大限度
I created a migration, ran rake db:migrate
, which bumped my db/schema.rb version number. Then I did a git fetch origin master
and saw that there were changes from my team members. So I did a git stash
and a git rebase FETCH_HEAD
, followed by a git stash pop
. This resulted in a conflict in db/schema.rb over the version number.
Upstream>>>
ActiveRecord::Schema.define(:version => 20110930179257) do
===========
ActiveRecord::Schema.define(:version => 20110930161932) do
<<<Stashed
I think the appropriate fix would be to manually increment the version number to something higher than the upstream.
Is this sensible, or bad news?
Thanks,
Max
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您当前的数据库具有正确的架构,您应该:
运行挂起的迁移(如果有)
从当前数据库架构覆盖您的
schema.rb
并提交
If your current database has the correct schema, you should:
Run pending migrations (if any)
Overwrite your
schema.rb
from your current database schemaAnd commit
当我发现自己遇到这种冲突时,我只需迁移数据库即可。无论是否有待处理的迁移,冲突都会得到纠正。
When I find myself with this conflict, I simply migrate the database. Whether there are pending migrations or not, the conflict will be corrected.
tldr
接受上游版本并像平常一样运行
rake db:migrate
。为什么要这样?
不要担心您创建的迁移(低于上游版本
20110930179257
)。 ActiveRecord 使用一个表 schema_migrations 来放置所有已运行的迁移。如果您的迁移不在列表中,而是在 db/migrate 目录中,则 ActiveRecord 将运行它们。这是表格,以便您可以更好地形象化:
人们很容易认为它实际上是这一行:
ActiveRecord::Schema.define(:version => 20110930179257)
它定义了最新的迁移运行,因此不会运行低于其版本的迁移。幸运的是这不是真的。 Rails 将运行
db/migrate
文件夹中但尚未在schema_migrations
表中的任何迁移。tldr
Accept the Upstream version and run
rake db:migrate
as you'd normally do.why is that the way to go
Don't worry about the migrations you've created (which are below Upstream version
20110930179257
). ActiveRecord uses a tableschema_migrations
where it puts all of the migrations that have been run. If your migrations aren't on the list but indb/migrate
directory, then ActiveRecord will run them.Here's the table so you can visualise it better:
It's tempting to think that it's actually this line:
ActiveRecord::Schema.define(:version => 20110930179257)
that defines latest migration run, so no migrations with version below it are going to be run. This is fortunately not true. Rails will run any migrations that are in
db/migrate
folder and not yet in theschema_migrations
table.根据 这个答案,肯定会发生冲突。用户必须手动合并,并将版本设置为两者中较高的版本。
According to this answer, a conflict is guaranteed. The user has to to manually merge, and set the version as the higher of the two.
以下是将 master 合并到我的功能分支以故障转移 db/schema.rb 中的冲突时所做的操作:
Here's what I do when merging master into my feature branch fails over conflicts in db/schema.rb:
~/bin/update-schema-rb
:~/bin/update-schema-rb
:一种选择是使用仅附加的手动版本脚本。在那里你想要冲突。如果你不掌握模式,它就会给你带来很大的困扰。因此,一朝被蛇咬,十年怕井绳,我不介意使用仅附加的变更管理方案来保留模式和查找信息(客户类型列表)。
An option is to have a manual version script which is additive only. There you want conflicts. Schema is something that will bite you hard if you don't keep on top of it. So once bitten, twice shy, I don't mind keeping schema and lookup info (list of customer types) with an additive only change management scheme.
我认为最好的方法是使用仅存在于当前分支上的迁移来执行 rake db:drop db:create db:migrate 来重新生成架构。这样,从其他分支的迁移就不会泄漏到您的模式文件中,并且以后会让您头痛 - 以防您频繁切换分支。
I feel the best approach is to do
rake db:drop db:create db:migrate
to regenerate schema using migrations that exist only on the current branch. That way migrations from other branches won't leak into your schema file and give you headache later - in case if you switch branches a lot.