Created
August 27, 2021 07:29
-
-
Save Synkevych/14b5a110d1ce8ff60a9815c66e2a381c to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/sh | |
echo "post-checkout hook will be executed" | |
# source rvm and .rvmrc if present | |
[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm" | |
[ -s "$PWD/.rvmrc" ] && . "$PWD/.rvmrc" | |
# rollback migrations if present | |
OLDBRANCH=$1 | |
NEWBRANCH=$2 | |
ISBRANCHCHANGE=$3 | |
if [ ISBRANCHCHANGE ]; then | |
AORD=$(git diff --name-status $OLDBRANCH $NEWBRANCH | egrep 'db/migrate' | egrep -m 1 -o '^[A-Z]') | |
if [ "$AORD" = "A" ]; then | |
echo 'Migrating database...' | |
bundle exec rake db:migrate | |
else | |
MIGRATIONS=$(git diff --name-only $OLDBRANCH $NEWBRANCH | egrep '^db/migrate'| wc -l) | |
REVERTFILECOUNT=$(($MIGRATIONS + 1)) | |
FILE=$(git ls-tree -r --name-status $OLDBRANCH db/migrate | tail -$REVERTFILECOUNT | head -1 | cut -d'_' -f 1 | cut -d/ -f3) | |
if [ $MIGRATIONS -ne 0 ]; then | |
echo 'Rolling back migrations...' | |
echo "bundle exec rake db:migrate VERSION="$FILE | |
git checkout $OLDBRANCH db/migrate/ | |
bundle exec rake db:migrate VERSION=$FILE | |
git reset db/migrate/ | |
git clean -f db/migrate -q | |
fi | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment