Using version control (Git) on a MySQL database

MysqlGitVersion Control

Mysql Problem Overview


I am a WordPress Designer/Developer, who is getting more and more heavily involved with using version control, notably Git, though I do use SVN for some projects. I am currently using Beanstalk for my remote repo.

Adding all of the WordPress files to my repo is no problem, if I wanted to I know I could .gitignore the wp-config file, but since I'm the only developer, currently, and these projects are closed source, it really makes little sense.

WordPress relies heavily on the database, as any CMS does, to keep textual content, and many settings depending on the specific plugin/theme configuration I'm using. I'm wondering what the best way of using version control on the database would be, if it's even possible. I guess I could do a SQL dump, though my MySQL server is running on Windows (read as: I don't know how to do it), and then add the SQL dump to my repository. But when I push something live, that poses huge security threats.

Is there an accepted practice of doing this?

Mysql Solutions


Solution 1 - Mysql

You can backup your database within a git repository. Of course, if you place the data into git in a binary form, you will lose all of git's ability to efficiently store the data using diffs (changes). So the number one best practice is this: store the data in a text serialised format.

mysqldump is a suitable program to help you do this. It isn't perfect though. If anything disturbs the serialisation order of items (eg. as a result of creating new tables, etc.) then artificial breaks will enter into the diff. That will decrease the efficiency of storage. You could write a custom serialiser to serialise changes only -- but then you are doing the hard work that git is already good at. Just use the sql dump.

That being said, what you are wanting to do isn't what devs normally mean when they talk about putting the database in git. For instance, if you read the link posted by @eggyal ([link to codinghorror] 1) you will see that what is actually placed in git are the scripts needed to generate the initial database. There may be additional scripts, like those to populate the database data with a clean state, or to populate it with testing data. All such sql scripts are text files, and pretty much the same format as the sql dump you would get from mysqldump. So there's no reason you can't do it that way with your day-to-day data as well.

Solution 2 - Mysql

There are not many software available to version control databases like MySQL and MongoDB.

But one is under development and the beta version is about to be launched soon. Check out Klonio - Version Control for databases

Solution 3 - Mysql

The article How to Sync A Local & Remote WordPress Blog Using Version Control gives advice on how to automate sync between two instances (development, production) of a WordPress blog using Mercurial. Is mentions that for this scenario, Git and Mercurial are very similar.

Step 4 (Synchronizing The Databases) is of interest here.

> The database content will be exported to a file that is tracked by the revision control. Each time we pull changes, the database content will be replaced by this file, making our database up-to-date.

Then, it elaborates on conflicts and the scripting part of the job.

There is a version control tutorial in Mercurial out there, if you're not familiar with it.

Solution 4 - Mysql

If you are only interested in schema changes under version control, there is a nice stuff SqlRog. It extracts schema into the project files that can be put under the git.

Solution 5 - Mysql

Be aware that Wordpress stores all news feed content in the database, so even if you don't make any changes, there will be a lot of changing content.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZach RussellView Question on Stackoverflow
Solution 1 - MysqlKevin A. NaudéView Answer on Stackoverflow
Solution 2 - MysqlKevinView Answer on Stackoverflow
Solution 3 - MysqlabanmitraView Answer on Stackoverflow
Solution 4 - MysqlstPatrickView Answer on Stackoverflow
Solution 5 - MysqlAnonymousView Answer on Stackoverflow