Skip to content

Instantly share code, notes, and snippets.

@tsertkov
Created January 18, 2018 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsertkov/ae883170766be79cf40782f9d9ea21ea to your computer and use it in GitHub Desktop.
Save tsertkov/ae883170766be79cf40782f9d9ea21ea to your computer and use it in GitHub Desktop.
DB schema migration with heroku hosted postgres
#!/usr/bin/env bash
set -e
SOURCE_APP=web-app
TARGET_APP=$1
[ -z "$TARGET_APP" ] \
&& echo "usage: `basename $0` <TARGET_APP>" \
&& exit 1
echo -n "Fetching follower db name from for '${SOURCE_APP}': "
followerName=$(
heroku pg:info --app "$SOURCE_APP" \
| sed -n 's/^=== HEROKU_POSTGRESQL_\(.*\)_URL$/\1/p'
)
echo $followerName
echo -n "Fetching database credentials: "
followerUrl=$(
heroku pg:credentials "$followerName" --app "$SOURCE_APP" \
| sed -n '/^Connection URL:$/{n;p;}' \
| sed 's/^[[:blank:]]*//'
)
[ -z $followerUrl ] && exit 1
echo done
echo -n "Fetching target database credentials: "
targetUrl=$(
heroku pg:credentials DATABASE --app "$TARGET_APP" \
| sed -n '/^Connection URL:$/{n;p;}' \
| sed 's/^[[:blank:]]*//'
)
[ -z $targetUrl ] && exit 1
echo done
echo -n "Copy schema (STRUCTURE ONLY): "
cmd="pg_dump '$followerUrl' --schema-only --clean --no-acl | psql '$targetUrl' -q"
docker run -it --rm postgres sh -c "$cmd"
echo done
echo -n "Copy SezuelizeMeta table data: "
cmd="pg_dump '$followerUrl' --data-only --table '\"SequelizeMeta\"' | psql '$targetUrl' -q"
docker run -it --rm postgres sh -c "$cmd"
echo done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment