-
-
Save kronn/5586697 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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