Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put MySQL code into source control?

I know I can copy all my MySQL code manually to files and then put those files into source control. But is there any way to do this automatically?

I would like to do this to stored procedures, but also to table/event/trigger creation scripts.

like image 956
Jader Dias Avatar asked Dec 04 '25 21:12

Jader Dias


2 Answers

You can create triggers on data change, which would store the change automatically to some source control. However there is no automatic way to track structure changes (tables, stored procedures and so on) this way. So probably the best way is to dump database and store these dumps in source control. You can do this periodically to automate the things.

like image 119
Michal Čihař Avatar answered Dec 07 '25 11:12

Michal Čihař


Based on Michal answer, the solution I am using so far is:

#!/bin/bash
BACKUP_PATH=/root/database_name
DATABASE=database_name
PASSWORD=Password
rm -f "$BACKUP_PATH/*.sql"
mysqldump -p$PASSWORD --routines --skip-dump-date --no-create-info --no-data --skip-opt $DATABASE > $BACKUP_PATH/$DATABASE.sql
mysqldump -p$PASSWORD --tab=$BACKUP_PATH --skip-dump-date --no-data --skip-opt $DATABASE
hg commit -Am "automatic commit" $BACKUP_PATH
like image 39
Jader Dias Avatar answered Dec 07 '25 11:12

Jader Dias