管理 Git 操作创建的 schema.rb 中的冲突

发布于 2024-12-07 14:20:38 字数 476 浏览 2 评论 0原文

我创建了一个迁移,运行了 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 技术交流群。

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

发布评论

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

评论(8

冬天的雪花 2024-12-14 14:20:38

如果您当前的数据库具有正确的架构,您应该:

  • 运行挂起的迁移(如果有)

    rake db:migrate
    
  • 从当前数据库架构覆盖您的schema.rb

    rake db:schema:dump
    
  • 并提交

If your current database has the correct schema, you should:

  • Run pending migrations (if any)

    rake db:migrate
    
  • Overwrite your schema.rb from your current database schema

    rake db:schema:dump
    
  • And commit

能怎样 2024-12-14 14:20:38

当我发现自己遇到这种冲突时,我只需迁移数据库即可。无论是否有待处理的迁移,冲突都会得到纠正。

When I find myself with this conflict, I simply migrate the database. Whether there are pending migrations or not, the conflict will be corrected.

木森分化 2024-12-14 14:20:38

tldr

接受上游版本并像平常一样运行rake db:migrate

为什么要这样?

不要担心您创建的迁移(低于上游版本 20110930179257)。 ActiveRecord 使用一个表 schema_migrations 来放置所有已运行的迁移。如果您的迁移不在列表中,而是在 db/migrate 目录中,则 ActiveRecord 将运行它们。

这是表格,以便您可以更好地形象化:
schema_migrations 表

人们很容易认为它实际上是这一行:
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 table schema_migrations where it puts all of the migrations that have been run. If your migrations aren't on the list but in db/migrate directory, then ActiveRecord will run them.

Here's the table so you can visualise it better:
schema_migrations table

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 the schema_migrations table.

允世 2024-12-14 14:20:38

根据 这个答案,肯定会发生冲突。用户必须手动合并,并将版本设置为两者中较高的版本。

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.

失眠症患者 2024-12-14 14:20:38

以下是将 master 合并到我的功能分支以故障转移 db/schema.rb 中的冲突时所做的操作:

$ git merge --abort
$ git checkout master
$ rake db:drop db:create db:migrate
$ git checkout -- db/schema.rb
$ git checkout my_feature_branch
$ rake db:migrate
$ git add db/schema.rb
$ git commit -m 'Updated schema'
$ git merge master

Here's what I do when merging master into my feature branch fails over conflicts in db/schema.rb:

$ git merge --abort
$ git checkout master
$ rake db:drop db:create db:migrate
$ git checkout -- db/schema.rb
$ git checkout my_feature_branch
$ rake db:migrate
$ git add db/schema.rb
$ git commit -m 'Updated schema'
$ git merge master
盛夏尉蓝 2024-12-14 14:20:38

~/bin/update-schema-rb

#!/usr/bin/env bash

git co master
bin/rake db:reset db:seed
git co -
bin/rake db:migrate

~/bin/update-schema-rb:

#!/usr/bin/env bash

git co master
bin/rake db:reset db:seed
git co -
bin/rake db:migrate
━╋う一瞬間旳綻放 2024-12-14 14:20:38

一种选择是使用仅附加的手动版本脚本。在那里你想要冲突。如果你不掌握模式,它就会给你带来很大的困扰。因此,一朝被蛇咬,十年怕井绳,我不介意使用仅附加的变更管理方案来保留模式和查找信息(客户类型列表)。

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.

你穿错了嫁妆 2024-12-14 14:20:38

我认为最好的方法是使用仅存在于当前分支上的迁移来执行 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.

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