Skip to content

Instantly share code, notes, and snippets.

@jpopesculian
Last active April 24, 2017 00:49
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 jpopesculian/ae52f54cd36e9242627b7a8008c6a91b to your computer and use it in GitHub Desktop.
Save jpopesculian/ae52f54cd36e9242627b7a8008c6a91b to your computer and use it in GitHub Desktop.
Simple script to create and apply a patch file to deal with misaligned migrations / databases
#!/bin/bash
scriptname="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
patch="$dir/migration.patch"
read -r -d '' help << EOM
Usage:
$scriptname command [file]
Create / apply patch file to better manage changes, annotations and specs
caused by db migrations and misaligned databases
Commands:
create Create the patch file
apply Apply the patch file
help Display this text
Options
file File to save the patch file in
(current: $patch)
EOM
display-help ()
{
echo "$help"
}
exit-if-dirty ()
{
if [[ `git status --porcelain` ]]; then
echo "Changes must be committed or stashed before creating patch!"
exit 1
fi
}
do-migrate ()
{
spring rake db:migrate > /dev/null
}
create-patch ()
{
temp="$patch.temp"
git diff -u > $temp --color=never
interdiff $temp /dev/null > $patch
rm $temp
}
apply-patch ()
{
git apply $patch
}
if [ $# -eq 0 ] ; then
display-help
exit 1
fi
if [ $# -gt 1 ] ; then
patch="$2"
fi
case "$1" in
help)
display-help
exit 0
;;
apply)
apply-patch
exit 0
;;
create)
exit-if-dirty
do-migrate
create-patch
apply-patch
exit 0
;;
*)
display-help
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment