Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.