Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git hook for diff sqlite table

I have a Sqlite db in a Git repository. Today I wanted to do a diff of a view in two different commits. I did it this way:

$ sqlite3 -list file.sqlite "SELECT * FROM contact_list_detailed" >/tmp/newlist
$ git checkout 51c24d13c file.sqlite
$ sqlite3 -list file.sqlite "SELECT * FROM contact_list_detailed" >/tmp/oldlist
$ git checkout -- file.sqlite
$ diff /tmp/oldlist /tmp/newlist

It works and I could script it if I want. But are there any "nice" ways of doing this with hooks?

like image 764
Niclas Nilsson Avatar asked Nov 07 '12 14:11

Niclas Nilsson


1 Answers

Here is how to use git's textconv feature for showing diffs between versions of the sqlite file. It just does a dump, so it may not be super efficient for large databases. No hook necessary.

That link seems to be no longer available, so I'm using the the archived version instead.

The gist of it is, in the git attributes file (.gitattributes, or .git/info/attributes), add a pattern match to force sqlite3 diffs (assuming your database files have the extension .sqlite3):

*.sqlite3 diff=sqlite3

Then in your git config file (~/.gitconfig or .git/config):

[diff "sqlite3"]
    binary = true
    textconv = "echo .dump | sqlite3"

If you just want to track schema changes, use .schema instead of .dump.

like image 162
Brian Minton Avatar answered Oct 09 '22 10:10

Brian Minton