PHP - 数据库模式:版本控制、分支、迁移

发布于 2024-08-28 18:57:27 字数 1191 浏览 1 评论 0原文

我正在尝试提出(或找到)一个可重用的系统,用于 php 项目中的数据库模式版本控制。

有许多可用于 php 的 Rails 风格的迁移项目。 http://code.google.com/p/mysql-php-migrations/ 就是一个很好的例子。它使用迁移文件的时间戳,这有助于解决分支之间的冲突。

此类系统的一般问题: 当开发分支 A 被签出,而您想要签出分支 B 时, B 可能有新的迁移文件。这很好,迁移到更新的内容是直接的。

如果分支 A 有较新的迁移文件,则需要向下迁移到最近的共享补丁。如果分支 A 和 B 的代码库显着不同,您可能需要进一步向下迁移。这可能意味着:签出B,确定共享补丁号,签出A,向下迁移到该补丁。这必须从 A 完成,因为实际应用的补丁在 B 中不可用。然后,签出分支 B,并迁移到最新的 B 补丁。从 B 到 A 时再次进行相反的过程。

建议的系统: 向上迁移时,不要只存储补丁版本,而是在数据库中序列化整个补丁以供以后使用,尽管我可能只需要 down() 方法。 更改分支时,将已运行的补丁与目标分支中可用的补丁进行比较。通过 ID 或哈希确定运行补丁的数据库表与目标分支中的补丁之间最近的共享补丁(或者可能是最旧的差异)。还可以查找隐藏在两个分支之间的许多共享补丁下的新补丁或丢失补丁。

使用数据库表存储的 down() 方法自动向下合并到最近的共享补丁,然后向上合并到分支的最新补丁。

我的问题是: 这个系统是否太疯狂和/或充满后果而懒得开发?我在数据库模式版本控制方面的经验仅限于 PHP autopatch,这是一个仅限 up() 的系统,需要具有顺序 ID 的文件名。

更新,2 年后

这是一篇旧文章,但我想提一下,我在开发过程中一般放弃了迁移,因为它们不必要地复杂且容易出错。

相反,我使用构建脚本来:

  1. 清除数据库、
  2. 创建架构、
  3. 添加已知应用程序数据(真实内容)以及
  4. 添加固定数据(开发内容)。

当更改分支或接收其他开发人员的更新时,您可以使用一个命令完全重新加载数据库以达到已知状态。

生产服务器仍然需要数据库补丁,但无论如何都必须手动创建这些补丁。

I'm trying to come up with (or find) a reusable system for database schema versioning in php projects.

There are a number of Rails-style migration projects available for php. http://code.google.com/p/mysql-php-migrations/ is a good example. It uses timestamps for migration files, which helps with conflicts between branches.

General problem with this kind of system:
When development branch A is checked out, and you want to check out branch B instead,
B may have new migration files. This is fine, migrating to newer content is straight forward.

If branch A has newer migration files, you would need to migrate downwards to the nearest shared patch. If branch A and B have significantly different code bases, you may have to migrate down even further. This may mean: Check out B, determine shared patch number, check out A, migrate downwards to this patch. This must be done from A since the actual applied patches are not available in B. Then, checkout branch B, and migrate to newest B patch. Reverse process again when going from B to A.

Proposed system:
When migrating upwards, instead of just storing the patch version, serialize the whole patch in database for later use, though I'd probably only need the down() method.
When changing branches, compare patches that have been run to patches that are available in the destination branch. Determine nearest shared patch (or oldest difference, maybe) between db table of run patches and patches in destination branch by ID or hash. Could also look for new or missing patches that are buried under a number of shared patches between the two branches.

Automatically merge down to the nearest shared patch, using the db table stored down() methods, and then merge up to the branche's latest patch.

My question is:
Is this system too crazy and/or fraught with consequences to bother developing? My experience with database schema versioning is limited to PHP autopatch, which is an up()-only system requiring filenames with sequential IDs.

Update, 2 years later

This is an old post, but I wanted to mention that I've abandoned migrations in general during development as they're unnecessarily complicated and error prone.

Instead, I use build scripts to:

  1. clear the database,
  2. create the schema,
  3. add known application data (real content), and
  4. add fixture data (development content).

When changing branches, or receiving updates from other developers, you reload the database completely with one command to get to a known state.

Production servers still need database patches, but those would have to be manually created anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

心欲静而疯不止 2024-09-04 18:57:27

好吧,我找不到任何理由不继续前进。

该项目位于:

http://github.com/Billiam/MySQL -PHP-AutoMigrations

需要一些爱(准确的注释、单元测试、实际的错误测试),但现在似乎对我来说效果很好。

它是 http://code.google.com/p/mysql-php 的分支-migrations/ 包含上面的想法,以及其他一些小东西。

向下迁移是通过向上迁移时保存在数据库中的方法完成的,因此文件更改(如在分支之间切换时的更改)不会影响向下迁移。
添加了两个功能:

  • 一个神奇的“自动”功能,用于处理向下迁移到最旧的共享迁移,然后向上迁移到迁移目录中的最新迁移。
  • 'propose' 函数显示 auto 实际会做什么。

然而,仍然非常对这种方法潜在的(甚至预期的)陷阱持开放态度。

Well, I wasn't able to find any reason not to move forward.

The project, such as it is, is here:

http://github.com/Billiam/MySQL-PHP-AutoMigrations

Needs some love (accurate comments, unit testing, actual bug testing), but seems to be working well for me now.

It's a fork of http://code.google.com/p/mysql-php-migrations/ to include the ideas above, and some other little stuff.

Migrating down is done from methods saved in the database on the way up so that file changes (like those when switching between branches) do not affect downward migrations.
Added two functions:

  • a magic 'auto' function that handles migrating down to the oldest shared migration, and then up to the newest migration in the migrations directory.
  • 'propose' function that shows what auto will actually do.

Still very open to hearing potential (or even expected) pitfalls from this approach however.

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