Using Git to Track DB Schema Changes with Git Hook

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

If you use Git for version control and you would like to also keep a track of your database schema (and possibly content though I’m not doing that due to potential file sizes / speed issues) then all you need to do is this simple step:

  1. Go to your project folder and into the hidden .git folder, then a sub folder in there called hooks

    cd .git/hooks 
  2. Create a file called pre-commit and open it in vim (or whatever text editor you like)

    vim pre-commit 
  3. Add a mysql dump command to that file and save it ```

#!/bin/sh mysqldump -u DBUSER -pDBPASSWORD DATABASE –no-data=true > SQLVersionControl/vc.sql git add SQLVersionControl/vc.sql exit 0

(note this assumes you have a folder called SQLVersionControl in the root of your project. If you don't just create it.)

No without any further effort, you will update the schema file on every commit.


git commit -am ‘this commit will include a mysql schema dump that has been run just before the commit - sweet :)’