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.

Revisions

  1. plfort created this gist Jan 18, 2024.
    30 changes: 30 additions & 0 deletions switchBranch.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/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