69

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?

3
  • I dump the database and check it into version control. But I don't push to the server on Wordpress projects (git isn't always available on shared hosting). So generally I upload manually via FTP, as bad as that is. Interested to hear some proper practices when using git to deploy.
    – Christian
    Commented Nov 28, 2012 at 15:43
  • Also, you could keep the database checked into a develop branch, and use the technique here to exclude the database from being merged into master. Then only deploy from master. In theory, that should work.
    – Christian
    Commented Nov 28, 2012 at 15:45
  • I did a quick google for "Wordpress Version Control" and found this list of Wordpress plugins: wordpress.org/extend/plugins/tags/version-control Would any of them be useful? Commented Nov 29, 2012 at 14:56

5 Answers 5

24

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) 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.

2
  • 13
    For most enterprise applications, putting the scripts needed to generate the database under version control is all that's needed. But with content management systems like WordPress, a lot of stuff that's in the database is effectively code and configuration, not end-user data. Code and configuration need to be under version control, even if they're stored as data in rows of SQL tables. WordPress doesn't make that easy for you. Commented Dec 3, 2012 at 19:44
  • 1
    @Catcall I agree. That is what I was talking about when I said some of the sql scripts might populate the database with clean initial state. The strategy described would be very effective if you wanted to quickly deploy several wordpress sites according to some preconfigured state, e.g. with a preferred initial config of desired plugins, etc. Commented Dec 3, 2012 at 21:30
8

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

6

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.

1

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.

0

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.

1
  • What about auto-commit? Commented Nov 13, 2016 at 13:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.