18

Stackoverflow and MySQL-via-command-line n00b here, please be gentle! I've been looking around for answers to my question but could only find topics dealing with GitHubbing MySQL dumps (as in: data dumps) for collaboration or MySQL "version control" via GitHub, neither of which tells me what I want to know:

How does one include MySQL database schemas/information on tables with PHP projects on GitHub?

I want to share a PHP project on GitHub which relies on the existence of a MySQL database with certain tables. If someone wanted to copy/make use of this project, they would need to have these particular tables in place to make the script work (all tables but one are empty in the beginning and only get filled by the user over time, via the script; the non-empty table holds three values from the start). How does one go about this, what is common practice?

  • Would I just get a (complete) dump file of my own db/tables, then delete all the data parts (except for that one non-empty table), set all autoincrements to zero and then upload that .sql file to GitHub along with the rest of the project? OR
  • Is it best/better practice to write a (PHP) script with which the (maybe not-so-experienced) user can create these tables without having to use mysqldump/command line magic?

If solution #1 is the way to go, would I include further instructions on how to use such a .sql file?

Sorry if my questions sound silly, but as I said above, I myself am new to using the command line for MySQL-related things and had only ever used phpMyAdmin until yesterday (when I created my very first dump file with mysqldump - yay!).

2 Answers 2

8

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

2
  • Ha, I really thought it would be the cheap & easy (and not so nice to not so savvy users) solution #1! Means I'll have to put some more work into the project. :) Thanks for your help!
    – Kay
    Apr 30, 2013 at 20:29
  • +1 for migrations. Allows you to keep changes separate from your initial database schema. I've been using Phinx with relative ease coming from a Laravel background.
    – thorne51
    Jul 5, 2017 at 14:36
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.