Put MySQL database under version control?

I'm currently using SVN for my PHP projects. I was thinking I should get my database under version control too, but what's the best way to do that? Do I just make a db-folder in my project in SVN, paste SQL changes into a file called from_1.0_to_2.0.sql and commit?


Note that in December 2012, you had another option: DBV (DataBase Version)

It is based on this Github project (stalled in 2018), and is a database version control web application featuring schema management, revision scripts, and more.

database schema

It has been discontinued since early 2021.


Whenever you make changes to your database, you should save those changes in a migration, that you can then later on run on other servers at the same time you update your code. But you basically got the right idea. You can write some tools to make it more automated; for example version each file, and then create a table like migration_version in your database, which will contain the current version of the database. You can then create a migrate script that will run all the migrations required to get the database up-to-date.

Note that if you want bi-directional db versioning (so that you can revert back to previous db version too), you need to write the required queries for that too for each version.

There are also some tools that can aid you in writing the migrations, such as MySQLdiff