Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonidezman/736f6b89d1d9e07a85ecc726b1516b60 to your computer and use it in GitHub Desktop.
Save tonidezman/736f6b89d1d9e07a85ecc726b1516b60 to your computer and use it in GitHub Desktop.
Script for detecting if we need to rollback migrations when switching branches
# This git hook script helps us detect if we need to
# rollback some migrations when switching branches
# Recipe:
# 1) cd /path/to/your/repo
# 2) touch .git/hooks/post-checkout
# 3) chmod u+x .git/hooks/post-checkout
# 4) add code below to the post-checkout file
#!/bin/bash
set -e
set -u
echo "Inside git post-checkout hook"
echo "Running rake db:migrate:status"
LAST_MIGRATION_FILE=$(bin/rake db:migrate:status 2>/dev/null | tail -n 2 | head -1 | awk '{print $3$4$5$6}')
if [[ $LAST_MIGRATION_FILE == "**********NOFILE**********" ]]
then
echo -en "\\033[1;31mMaybe you need to get back to the previous branch and rollback some migrations?\\033[0;39m\n"
else
echo -en "\\033[32mMigrations are OK!\\033[0;39m\n"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment