Last active
August 29, 2015 14:04
-
-
Save entrity/72a386a169248fec9638 to your computer and use it in GitHub Desktop.
Rails migration rollback for bad deploy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Run this script before deploying to make a text file containing all | |
# schema_migrations. | |
# | |
# After deploying, if a rollback is required, run this script again | |
# with an argument of 'back' to cherry-pick new migrations out of | |
# schema migrations (ones that weren't in the pre-deploy log of | |
# schema_migrations) and reverse them with rake db:migrate:down VERSION=X. | |
PRE_DEPLOY_MIGRATIONS=schema_migrations_pre.txt | |
POST_DEPLOY_MIGRATIONS=schema_migrations_post.txt | |
DB_PW=my_pw | |
DB_USER=my_user | |
DB_HOST=my_host | |
DB_PORT=my_port | |
DB_NAME=my_name | |
log_migrations () { | |
mysql -h$DB_HOST -P$DB_PORT -u$DB_USER -p -e 'select * from $DB_NAME.schema_migrations' > $1 | |
} | |
case "$1" in | |
# Rollback migrations | |
post) | |
echo ROLLING BACK MIGRATIONS AFTER BAD DEPLOY | |
log_migrations $POST_DEPLOY_MIGRATIONS | |
declare -a migs | |
migs=diff $PRE_DEPLOY_MIGRATIONS $POST_DEPLOY_MIGRATIONS | grep \> | cut -f2 -d' ' | |
migs_ct=`echo $migs | wc -w` | |
echo $migs_ct migrations to roll back | |
for mig in $migs; do | |
echo Rolling back migration $mig | |
RAILS_ENV=production rake db:migrate:down $mig | |
done | |
;; | |
# Log existing migrations prior to deploy | |
pre) | |
echo LOGGING MIGRATIONS PRIOR TO DEPLOY | |
log_migrations $PRE_DEPLOY_MIGRATIONS | |
;; | |
# Echo usage | |
*) | |
echo "Usage: ./this.sh <pre|post>" | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment