Skip to content

Instantly share code, notes, and snippets.

@freeformz
Created August 16, 2012 23:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freeformz/3374606 to your computer and use it in GitHub Desktop.
Save freeformz/3374606 to your computer and use it in GitHub Desktop.
Using Heroku's Postgres's Fork feature to make/update staging dbs

This script assumes the following:

  1. Your app is named 'something' and that your staging app is named 'something-staging'. This seems to be a common thing.
  2. You only have a single staging HEROKU_POSTGRESQL databae.

Totally untested. 😏

What does it do?

  1. Grabs the current db name
  2. makes a fork of the production apps DATABASE_URL db, onto the staging app
  3. waits for the new db
  4. promotes the new db to DATABASE_URL
  5. removes the original staging db.

How to run it

./stage-me.sh my_prod_app_name

Optionally with a db plan type, defaults to ronin

./stage-me.sh my_prod_app_name crane

app=${1}
db_type=${2:-ronin}
old_db=`heroku config -a ${app}-staging | 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 ${app}-staging
new_db=`heroku config -a ${app}-staging | grep ^HEROKU_POSTGRESQL | grep -v ${old_db} | cut -d : -f 1 | sed s/_URL//`
heroku pg:wait -a ${app}-staging
heroku pg:promote ${new_db} -a ${app}-staging
#Remove the old db
if [ ! -z "${old_db}" ]l; then
heroku addons:remove ${old_db} -a ${app}-staging --confirm ${app}-staging
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment