Skip to content

Instantly share code, notes, and snippets.

@plfort
Created January 18, 2024 08:21
Show Gist options
  • Save plfort/2c218281f6426f58aa11e24a57271385 to your computer and use it in GitHub Desktop.
Save plfort/2c218281f6426f58aa11e24a57271385 to your computer and use it in GitHub Desktop.
Symfony/Doctrine - Switch git branch while taking account of Doctrine migrations
#!/bin/bash
set -eu
export XDEBUG_MODE=off
NEW_BRANCH=$1
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
MIGRATIONS_DIR=migrations
MIGRATIONS_NAMESPACE=DoctrineMigrations\
echo "Switching to $1"
LAST_COMMON_MIGRATION_FILE=`comm -12 <(git ls-tree -r --name-only $CURRENT_BRANCH $MIGRATIONS_DIR|sort) <(git ls-tree -r --name-only $NEW_BRANCH $MIGRATIONS_DIR|sort) |tail -1`
TARGET_MIGRATION_VERSION=$MIGRATIONS_NAMESPACE\\`echo $LAST_COMMON_MIGRATION_FILE|sed "s/$MIGRATIONS_DIR\/\(.*\)\.php/\1/"`
echo "Last common migration file between current branch ($CURRENT_BRANCH) and target branch ($NEW_BRANCH) is "
echo $LAST_COMMON_MIGRATION_FILE
#Rollback to the last migration file common to both branches
php bin/console d:m:m $TARGET_MIGRATION_VERSION -n
php bin/console d:m:m $TARGET_MIGRATION_VERSION --env=test -n
git switch $NEW_BRANCH
#Sync js deps
npm install
#Sync php deps, cache & clear
composer install
#Run new branch migrations
php bin/console d:m:m -n
@plfort
Copy link
Author

plfort commented Jan 18, 2024

Usage:

./switchBranch.sh name_of_the_branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment