[英]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作為分支模型開展項目。 因此,我們對位於不同分支的遷移存在同樣的問題。 出現此問題時,我使用doctrine遷移工具手動處理它以同步遷移版本:
$php public/index.php migrations:version 20150417121714 --add
$php public/index.php migrations:version 20150417202439 --remove
接着:
$php public/index.php migrations:execute 20150417121714
這個解決方案需要一些手動工作,但不幸的是到目前為止我沒有更好的工作。
我編寫了一個腳本來自動化遷移當前分支中所有不同遷移的過程,並遷移回您正在檢出的新分支。
以上鏈接適用於Symfony,但此處針對ZF2進行了修改(未經測試,因為我不使用ZF2):
#!/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
提交,如果是這種情況,則會添加遷移以重命名遷移。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.