Last active
December 16, 2016 18:55
-
-
Save wancw/7696774 to your computer and use it in GitHub Desktop.
列出切換 branch 前需要 rollback 的 South migration。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
if [[ $# != 1 ]]; then | |
cat - << USAGE | |
Usage: `basename $0` <branch> | |
USAGE | |
return 1 | |
fi | |
local old_branch=$(git rev-parse --abbrev-ref HEAD) | |
local new_branch=$(git rev-parse --abbrev-ref $1) | |
if [[ $new_branch == $old_branch ]]; then | |
echo "# No changes" | |
return | |
fi | |
local merge_base=$(git merge-base $old_branch $new_branch) | |
awk -f <( cat - <<'SCRIPT' | |
migrations[$1] <= 0 || $2 < migrations[$1] { | |
migrations[$1]=$2 | |
} | |
END { | |
if (length(migrations) > 0) { | |
print "# Migrations neeed to rollback:" | |
for (app in migrations) { | |
last = migrations[app] - 1; | |
if (last>0) { | |
printf "./manage.py migrate %s %04d\n", app, last | |
} else { | |
printf "./manage.py migrate %s zero\n", app | |
} | |
} | |
print "" | |
} | |
} | |
SCRIPT | |
) <( | |
git diff --name-only --diff-filter=A \ | |
--relative=$(git rev-parse --show-prefix) \ | |
$merge_base $old_branch \ | |
| grep -E '/migrations/[0-9]{4}.+py' \ | |
| sed -E -e 's,/migrations/, ,' -e 's/( [0-9]{4}).+$/\1/' | |
) | |
echo "# Checkout new branch:" | |
echo "git checkout $new_branch" | |
echo | |
echo "# Migrate" | |
echo "./manage.py migrate" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
如果有 app 在 new branch 才有 migration 的話會失效。