4

我正在开发具有 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

假设我需要修补并在生产分支上创建迁移 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 迁移的最佳方式是什么?

4

2 回答 2

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

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

于 2015-04-22T16:12:54.277 回答
1

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

上面的链接是针对 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="$1"

# 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添加迁移的提交中重命名迁移。

于 2016-04-27T15:35:35.680 回答