Skip to content

Instantly share code, notes, and snippets.

@kronn
Forked from niklas/branch_database.sh
Last active December 17, 2015 09:19
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 kronn/5586697 to your computer and use it in GitHub Desktop.
Save kronn/5586697 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# An own database for every branch
#
# 1) put this script somewhere, make it executable (chmod +x)
# 2) modify your database.yml:
# development:
# adapter: postgresql
# database: "<%= `path/to/stored_script` %>"
# 3) If you want a separate db for your branch, follow the instructions on STDERR on environment startup
PREFIX=$(basename $(pwd))
echoerr() { echo "$@" 1>&2; }
BRANCH=$(git rev-parse --abbrev-ref HEAD | sed 's/\//-/g')
DATABASE="${PREFIX}_${BRANCH}"
if [ $(psql -l | grep "\b${DATABASE}\b" | wc -l) -eq 1 ]; then
echoerr "using branch specific database: ${DATABASE}"
else
echoerr "falling back to default database: ${DATABASE}"
echoerr "run \`createdb ${DATABASE}\` for profit"
DATABASE="${PREFIX}_develop"
fi
echo -n $DATABASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment