• How To Politely Ask Git To Remind You About Migrations

    Two dark guide rails merge at a pale stone tile that activates a brass reminder bell

    Sometimes it is important to remember that along with a code update, a new migration has appeared and should not be forgotten. The first option is git merge-base and a few more useful commands. It solves the task, but requires many actions; there is room for imagination and automation. Let git remind us about new migrations itself!

    bash
    #!/bin/bash
    
    HAS_NEW_MIGRATIONS=0
    git diff HEAD@{1} HEAD@{0} --name-only --diff-filter=A | grep 'migration' | while read FILENAME; do
      if [ "$HAS_NEW_MIGRATIONS" == 0 ] ; then
        echo -en "\033[32mMigrations added: \033[0m \n"
        HAS_NEW_MIGRATIONS=1
      fi
    
      echo -en "\033[32m — " $FILENAME "\033[0m \n"
    done
    
    HAS_MODIFIED_MIGRATIONS=0
    git diff HEAD@{1} HEAD@{0} --name-only --diff-filter=M | grep 'migration' | while read FILENAME; do
      if [ "$HAS_MODIFIED_MIGRATIONS" == 0 ] ; then
        echo -en "\033[31mMigrations modified: \033[0m \n"
        HAS_MODIFIED_MIGRATIONS=1
      fi
    
      echo -en "\033[31m — " $FILENAME "\033[0m \n"
    done
    
    exit 0
    
    ~/project/.git/hooks/post-checkout
    ~/project/.git/hooks/post-merge
    ~/project/.git/hooks/post-rewrite

    Mission accomplished. Now, when updating, we will receive a green list of new migrations and a red list of modified migrations, though it is better not to allow the latter at all. Do not forget to make the file executable.

    In fact, it is better to use some ready-made solution for tracking migrations, one that can also roll them back. Better still, do this automatically using Continuous Delivery.