Setting up git to track changes to a web site. The web site uses a MySQL database. I need a dump of the database to be included in every commit. Sometimes there are only changes to the database, but still I want to be able to commit this change.
I've setup a git pre-commit hook to dump the database, and add that dump file to git. The git hook looks as follows:
#!/bin/bash
systemname="mysystem"
dumppathname=".dbexports/${systemname}_dbdump_$(date +%Y%m%d_%H%M%S).sql"
./.scripts/dump_database.sh $dumppathname
if test $? -ne 0
then
echo "Dumping database failed. Aborting the commit..."
exit 1
else
git add $dumppathname
exit 0
fi
The dump script runs a mysqldump
command to dump the database with desires options. This command works as designed and writes the dump to the file designated by the $dumppathname
variable.
The problem is with the git add
. The dump file is only added every second time.
- Git is in a clean state, i.e. all changes have been commited, no untracked files.
- I apply some changes to the web site which reflect in some database changes only. I want to commit this state, so I do:
me@mysystem /website> git commit -m "Track database changes, only."
The response is (the first line is written by the dump script):
Dumping current database 'mywebsite' to '.dbexports/mysystem_dbdump_20211024_132309.sql'
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.dbexports/mysystem_dbdump_20211024_132309.sql
nothing added to commit but untracked files present (use "git add" to track)
It seems the git add
from the git hook did not work.
- But then, I immediately do another
commit
:
me@mysystem /website> git commit -m "Track database changes, only. Second time."
The response is (again, the first line is written by the dump script):
Dumping current database 'mywebsite' to '.dbexports/mysystem_dbdump_20211024_132323.sql'
[master 87cfdb9] Track database changes, only. Second time., 1 uncommited files.
2 files changed, 22630 insertions(+)
create mode 100644 .dbexports/mysystem_dbdump_20211024_132309.sql
create mode 100644 .dbexports/mysystem_dbdump_20211024_132323.sql
So, this time both the dump file form the first, as well as the dump file from the current commit
seem to have made it into the commit. What am I missing?