Bash script to relink alembic migrations

Jun 12th, 2017 in Python, Tools, Bash, MySQL, SQLAlchemy by Michael Cho

← All articles


I had written previously about using `alembic merge heads` for fixing the dreaded `Multiple head revisions are present` error. This usually when you have written a migration on your branch, but when it's time to merge to master - somebody else has committed another migration before you, meaning you are both pointing at the same downstream migration.

Using `merge heads` is fine, but if you keep doing this and you need to keep running all your migrations (eg for continuous integration tests!) this starts to slow down your build. I suspect this is because alembic has to parse through all the different heads and resolve them at runtime. 

So I wanted to write a short bash script with 2 objectives:

  1. Resolve these down revision conflicts with alembic
  2. Also rename the timestamped migration file, so that my migrations are listed on the filesystem in the same order they run

Here is the script I came up with, feel free to use in your own projects:

#!/usr/bin/env bash

# A script which finds any migrations not yet merged to master, and
# ensures it is timestamped last in the directory and with an updated revision number.

# Similar in effect to `alembic merge heads`, but neater...

# Common

# eg pretty_print(color, colorised_text, normal_text)
function pretty_print {
    echo -e "$1 $2 $Color_Off $3"


# Step 1 : Find any migrations not yet in master
UNMERGED_MIGRATIONS=(`git diff --name-only --diff-filter=A origin/master | grep alembic/versions`)
if [ -z "$UNMERGED_MIGRATIONS" ]; then
    pretty_print $Red "No unmerged migrations!"

for i in "${UNMERGED_MIGRATIONS[@]}"
   pretty_print $Green "Found unmerged migration: " $i

# Step 2 : Find the last migration merged to master
LAST_MERGED_MIGRATION=`ls -1 alembic/versions/*.py |
    while read N
        if [[ ! ${UNMERGED_MIGRATIONS[@]} =~ "${N}" ]]; then
            echo $N

} | tail -n1`

pretty_print $Green "Last merged migration: " $LAST_MERGED_MIGRATION

# Step 3 : Parse the last migration file and find the line which says "revision = 'abc123'"
LAST_REVISION=`cat $LAST_MERGED_MIGRATION | grep "revision = " | head -n1 | sed -n " s,[^']*'\([^']*\).*,\1,p "`
pretty_print $Green "Last revision: " $LAST_REVISION

LAST_MERGED_TIMESTAMP=`echo $LAST_MERGED_MIGRATION | cut -d'/' -f3 | cut -d'_' -f1`
pretty_print $Green "Last merged timestamp: " $LAST_MERGED_TIMESTAMP

# Step 4 : Update the unmerged migrations to link to the last merged
for migration in "${UNMERGED_MIGRATIONS[@]}"
    pretty_print $Red "------ Relinking: " $migration

    CURRENT_DOWN_REVISION=`cat $migration | grep "down_revision = " | head -n1 | sed -n " s,[^']*'\([^']*\).*,\1,p "`
    pretty_print $Red "Modifying unmerged revision: " $CURRENT_DOWN_REVISION
    sed -i'.bak' "s/down_revision = '$CURRENT_DOWN_REVISION'/down_revision = '$LAST_REVISION'/" $migration
    rm "$migration.bak"

    # Step 5 : Move unmerged migrations to last file in directory
    LAST_UNMERGED_TIMESTAMP=`echo $migration | cut -d'/' -f3 | cut -d'_' -f1`
    LAST_UNMERGED_FILENAME=`echo $migration | cut -d'/' -f3`

    # Store this migrations filename and timestamp so next migration in loop is also updated correctly

    pretty_print $Red "Renaming file: " $NEW_MIGRATION_TIMESTAMP
    mv $migration "alembic/versions/$NEW_MIGRATION_FILENAME"

Assuming you name this file ``, you can just run it with ./