在不同的 GIT 分支上使用 Doctrine 2 迁移是否有任何最佳实践?

Are there any best practices using Doctrine 2 migrations on different GIT branches?

我正在处理依赖于 Doctrine 2 的 Zend Framework 2 项目。源版本控制由 GIT 处理。我们使用 GitFlow 作为分支模型。

问题情况:

Migrations on Develop branch:
001.php
002.php
003.php
004.php

Migrations on Production branch:
001.php
002.php

假设我需要打补丁并在 Production 分支上创建迁移 003.php。我还必须挑选 003.php 更改为 Develop 分支,最终结果如下所示:

Migrations on Develop branch:
001.php
002.php
*003.php*
003.php
004.php

Migrations on Production branch:
001.php
002.php
*003.php*

但问题来了。如果Develop数据库当前迁移是004,添加了003,则不会执行。

处理 Doctrine 2 迁移的最佳方法是什么?

我还在开发一个使用 ZF2、Doctrine 2 和 Migrations 以及 Gitflow 作为分支模型的项目。因此,我们对位于不同分支的迁移有同样的问题。当出现这个问题时,我使用学说迁移工具手动处理,同步迁移版本:

$php public/index.php migrations:version 20150417121714 --add
$php public/index.php migrations:version 20150417202439 --remove

然后:

$php public/index.php migrations:execute 20150417121714

此解决方案需要一些手动工作,但不幸的是,到目前为止我还没有更好的解决方案。

我写了一个 script 来自动化迁移回当前分支中的所有不同迁移并迁移回您正在签出的新分支的过程。

上面的link是针对Symfony的,但是这里针对ZF2做了修改(因为我不使用ZF2所以没有测试):

学说-checkout.sh

#!/bin/bash

# Commit ref of current HEAD
start_commit="$(git rev-parse HEAD)"

# Commit ref of what is to be checked out
dest_commit=""

# First common ancestor commit between the two branches
ancestor="$(git merge-base HEAD "$dest_commit")"

# Shorthand for `sudo -u nginx /home/user/project/public/index.php`
# Modify this if you don't run `php public/index.php` as `nginx`
appconsole="sudo -u nginx php $(git rev-parse --show-toplevel)/public/index.php"

# Checkout the ancestor commit to find the first common migration between the
# two branches.  Migrate backwards to this version.
git checkout "$ancestor"
ancestor_migration="$($appconsole migrations:latest)"
git checkout "$start_commit"
$appconsole migrations:migrate "$ancestor_migration"

# Checkout the destination branch and migrate back up

git checkout "$dest_commit"
$appconsole migrations:migrate

要使用它,当您需要在功能分支之间切换时,而不是 运行 git checkout another-branch,请执行 doctrine-checkout.sh another-branch.

如果你重新设置你的分支的基线,使它们包含以前在另一个分支中的迁移提交,这将导致难以从另一个分支再次检出该分支,因为它可能有更新日期的迁移,一些提交落后于迁移分行补充。如果是这种情况,您可以 git rebase -i 在您的功能分支中和 edit 添加迁移以重命名迁移的提交。