Skip to content

Instantly share code, notes, and snippets.

@dcolish
Created June 8, 2011 23:10
Show Gist options
  • Save dcolish/1015682 to your computer and use it in GitHub Desktop.
Save dcolish/1015682 to your computer and use it in GitHub Desktop.
Migrations done in bash
#!/bin/bash
RUN_SQL?="$(which psql) i3"
function _info(){
echo "At migration version: $(cat .migration-version)"
}
function _init() {
echo 0 > .migration-version
}
function _down(){
MIGRATION=$1
$RUN_SQL < $MIGRATION-down.sql
echo $(($MIGRATION - 1)) > .migration-version
}
function _new() {
VERSION=$(cat .migration-version)
touch $((${VERSION} + 1))-up.sql
touch $((${VERSION} + 1))-down.sql
echo -n "edit up script? [y/n] "
read ok
if [ $ok = 'y' ] ; then
exec $EDITOR $((${VERSION} + 1))-up.sql
fi
}
function _up() {
MIGRATION=$1
$RUN_SQL < $MIGRATION-up.sql
echo $MIGRATION > .migration-version
}
case $1 in
info) _info ;;
init) _init ;;
down) _down $2 ;;
new) _new $2 ;;
up) _up $2 ;;
*) echo "You need help, read ${0}" ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment