Our blog
Using Git to Track DB Schema Changes with Git Hook
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.
eg
-
git commit -am 'this commit will include a mysql schema dump that has been run just before the commit - sweet
'
More Reading:
8 Comments
|
[...] Using Git to Track DB Schema Changes with Git Hook | Edmonds Commerce Blog (tags: git, hook, pre-commit, sql, schema, backup, bash) [...] |
|
How would you commit the schema file itself as part of the pre-commit without running in to some sort of loop? Is there a way to use a conditional logic block in the pre-commit script to only run the schema dump/add/commit on the correct situation? My point being, I'd rather have an atomic commit for the schema file itself rather than bundle it with another commit. |
|
good question, let us know if you figure it out |
|
@Steve @admin You could use: "--no-verify" bypasses the pre-commit hook. http://www.kernel.org/pub/software/scm/git/docs/git-commit.html Haven't tested this, only read it in the manual. |
|
Marcio April 1st, 2011 |
Thanks a lot for sharing this. This seems a very nice trick indeed. If we change the mysql schema but we want to preserve the data of an already existent production database, dump will deal with it ? |
|
Jean Gionet October 26th, 2011 |
Thanks for this method! I'm fairly new to using git. My question is: Will this only run when there's actually a change that occurs within the repository? thanks! |
|
inf3rno February 11th, 2012 |
By me it says that command mysqldump not found. I have windows 7, is there a way to make it work? Another problem, that if I have space in the directory name then it breaks the path name, and prints directory not found. I'm trying with this: |
|
inf3rno February 11th, 2012 |
Now I figured out that the path containing spaces must be between double quotes, and I called the dump with exec and full path. With these modifications it creates the sql file, but doesn't add it to the commit :S |
RSS Feed
links for 2010-06-09 | Edmonds Commerce Blog
June 9th, 2010