Skip to content

Instantly share code, notes, and snippets.

@cadecairos
Last active September 2, 2016 02:22
Show Gist options
  • Save cadecairos/4ca05411f1156c2b6303b010d0f74f14 to your computer and use it in GitHub Desktop.
Save cadecairos/4ca05411f1156c2b6303b010d0f74f14 to your computer and use it in GitHub Desktop.
Script to automate database upgrades on Heroku
#!/bin/bash
set -e
app=$APP_NAME
old_db_env_var=$OLD_DB_ENV_VAR
new_db_env_var=$NEW_DB_ENV_VAR
remove_addon_name=$REMOVE_ADDON_NAME
old_db_url=`heroku config:get -a $app $OLD_DB_ENV_VAR`
new_db_url=`heroku config:get -a $app $new_db_env_var`
scale_in="web=0" # or "worker=0" or "web=0 worker=0"
scale_out="web=1" # or "worker=1" or "web=1 worker=1"
# enable maintenance mode
heroku maintenance:on -a $app
# scale in
heroku ps:scale -a $app $scale_in
# begin migration
heroku pg:copy -a $app $old_db_url $new_db_env_var
# remove old addon
heroku addons:destroy -a $app --confirm $app $remove_addon_name
# update config
heroku config:set -a $app DATABASE_URL=$new_db_url
# scale out
heroku ps:scale -a $app $scale_out
# maintainence mode off
heroku maintenance:off -a $app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment