Last active
December 27, 2015 14:58
-
-
Save stephencroberts/7343932 to your computer and use it in GitHub Desktop.
Development pre-push git hook that creates a versioned database patch if pushing to the staging server
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
#!/bin/bash | |
# Rename develop.sql to <version>.sql | |
# Exit if develop.sql is empty | |
if [ -s database/develop.sql ]; then exit 0; fi | |
# Get version tag from the latest commit | |
VER=`git tag --contains HEAD` | |
# Exit if not pushing to staging | |
if [ "$1" != "staging" ]; then exit 0; fi | |
# Exit if this push doesn't have a version | |
if [ -z "$VER" ]; then exit 0; fi | |
# Exit if <version>.sql already exists (probably from a failed push) | |
if [ -f database/$VER.sql ]; then exit 0; fi | |
# Copy develop.sql to <version>.sql | |
cat database/develop.sql > database/$VER.sql | |
rc=$? | |
if [[ $rc != 0 ]]; then | |
echo "Failed to create $VER.sql!" | |
exit 1 | |
fi | |
# Clear develop.sql | |
cat /dev/null > database/develop.sql | |
rc=$? | |
if [[ $rc != 0 ]]; then | |
echo "Failed to clear develop.sql!" | |
fi | |
# Add <version>.sql to latest commit | |
msg=`git log -1 --pretty=%B` | |
git add database/$VER.sql | |
git add database/develop.sql | |
git commit -q --amend -m "$msg" | |
git tag -a -f -m '' $VER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment