Skip to content

Instantly share code, notes, and snippets.

@stevenharman
Last active July 6, 2023 15:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenharman/6515891 to your computer and use it in GitHub Desktop.
Save stevenharman/6515891 to your computer and use it in GitHub Desktop.
Fork and promote one Heroku app's Postgres Database to another app.
#!/bin/sh
set -e
app=${1}
staging_app=staging-${app}
db_type=${2:-crane}
old_db=`heroku config -a ${staging_app} | grep ^HEROKU_POSTGRESQL | cut -d : -f 1 | sed s/_URL//`
heroku addons:add heroku-postgresql:${db_type} --fork `heroku config -a ${app} | grep ^DATABASE_URL | cut -d : -f 2-5` -a ${staging_app}
# Fork and promote new db for staging
new_db=`heroku config -a ${staging_app} | grep ^HEROKU_POSTGRESQL | grep -v ${old_db} | cut -d : -f 1 | sed s/_URL//`
heroku pg:wait -a ${staging_app}
heroku pg:promote ${new_db} -a ${staging_app}
# Remove the old staging db
if [ ! -z "${old_db}" ]l; then
heroku addons:remove ${old_db} -a ${staging_app} --confirm ${staging_app}
fi
set +e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment