Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save screamingjungle/8db2cd330b06b8d4a44234105f52acf1 to your computer and use it in GitHub Desktop.
Save screamingjungle/8db2cd330b06b8d4a44234105f52acf1 to your computer and use it in GitHub Desktop.
GIT MySQL DB Schema Pre-Commit-Hook
#!/bin/bash -e
# -e means exit if any command fails
DBHOST="localhost"
DBUSER="root"
DBPASS="passwd"
DBNAME="test"
SCHEMAPATH="__sql"
MYSQLBINPATH=""
# leave blank if it is in PATH.
#MYSQLBINPATH="/Application/XAMPP/xamppfiles/bin" ## MACOS
#MYSQLBINPATH="C:/xampp/mysql/bin" ## WINDOWS
if [ ! -d "$SCHEMAPATH" ]; then
mkdir $SCHEMAPATH
fi
$MYSQLBINPATH/mysqldump -h $DBHOST -u $DBUSER --password=$DBPASS $DBNAME --no-data=true > $SCHEMAPATH/$DBNAME_schema.sql
git add $SCHEMAPATH/$DBNAME_schema.sql
## uncomment this to use as a commit script
# git commit -m "$DBNAME schema version $(`date`)"
# git push # assuming you have a remote to push to
echo 'Exported and added Database to commit'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment