How to include MySQL database schema on GitHub?

If you want also track database schema changes

You can use git hooks.
In directory [your_project_dir]/.git/hooks add / edit script pre-commit

#!/bin/sh -e
set -o errexit

# -- you can omit next line if not using version table
version=`git log --tags --no-walk --pretty="format:%d" | sed 1q | sed 's/[()]//g' | sed s/,[^,]*$// | sed  's ......  '`

BASEDIR=$(dirname "$0")

# -- set directorey wher schema dump is placed
dumpfile=`realpath "$BASEDIR/../../install/database.sql"`

echo "Dumping database to file: $dumpfile"

# -- dump database schema
mysqldump -u[user] -p[password] --port=[port] [database-name] --protocol=TCP --no-data=true --skip-opt --skip-comments  --routines | \
sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' > "$dumpfile"

# -- dump versions table and update core vorsiom according to last git tag
mysqldump -u[user] -p[password] --port=[port] [database-name] [versions-table-name] --protocol=TCP --no-    data=false --skip-opt --skip-comments  --no-create-info  | \
sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | \
sed -e "/INSERT INTO \`versions\` VALUES ('core'/c\\INSERT INTO \`versions\` VALUES ('core','$version');" >> "$dumpfile"

git add "$dumpfile"

# --- Finished
exit 0    

Change [user], [password], [port], [database-name], [versions-table-name]

This script is executed autamatically by git on each commit. If commiting tag new version is saved to table dump by tag name. If no changes in database, nothing is commited. Make sure if script is executable :) Your install script can take sql queries from this dump and developer can easy track database changes.


Common practice is to include an install script that creates the necessary tables, so solution #2 would be the way to go.

[edit] That script could ofc just replay a dump. ;)

You might also be interested in migrations: How to automate migration (schema and data) for PHP/MySQL application