A crude version control system for mysql written in php
Effective mysql database version control in one library. Quick and simple to use, but with a rich and flexible API for those who want to customise the set up.
- PHP >= 5.4
- MySQL client
Install with composer:
$ composer require smrtr/mysql-version-control:~1.0
Your database versions will be stored in
<project_root>/db/versions by default.
The sql for each version is stored in a directory directly under this directory.
So the directories are named
db/versions/2 and so on.
Each version must contain at least one of the following files:
schema.sql- always runs first, contains
CREATE TABLE IF NOT EXISTSand
ALTERstatements and the like.
DELETEstatements and the like.
testing.sql- same as
data.sqlbut with test data which doesn't need to exist outside of testing environments.
runme.php- a custom php hook for running php code with your version.
The files for each version are run in the order specified above.
The quickest way to get started is to set up your database configuration in a file at
See examples/db.ini for an example of this file.
Define a list of environments and testing environments under the tag
List out all of the available environments with entries like so:
environments = "local-dev".
List the testing environments like so:
testing_environments = "local-dev".
This list is a subset of the environments list and comprises those environments which should receive test data.
You must define two database connection configurations for each environment.
The two configurations are called
runtime and they are used for processing schema changes and data
Each connection requires a
database. You can optionally add a
The command line tool is located at
Remember it! It stands for Smrtr MYsql VERsion control.
vendor/bin/smyver.php status <environment> to get the current status of the database for that environment.
vendor/bin/smyver.php up <environment> to install or update the database on the given environment.
This command looks at the available versions in the
db/versions directory and applies new versions sequentially
from the current version.
If this is the first run on the given environment, then a table called
db_config is created and used to store the
current database version.
You may optionally provide a second argument specifying the mysql client binary to use. This argument is required if mysql is not on your $PATH.
Use this flag to skip the schema files. This can be useful if you use an ORM to build the database schema.
Use this flag to install a provisional version. This allows you to test out your database version, which may currently
be in development, before you commit to it by giving it a version number. This command looks for your provisional
<project_root>/db/versions/new by default.
vendor/bin/smyver.php teardown <environment> to tear down the tables on the given environment.
This command is useful for development & testing environments.
confirm option to bypass the confirmation prompt, e.g.
vendor/bin/smyver.php <environment> --confirm
These options can be used with all console commands.
Specify a configuration adapter to use instead of the Ini adapter which is used by default.
If you are using one of the standard adapters shipped with this package you only need to enter the class name, e.g. PhpFile.
If you are using your own custom adapter class then you must provide a fully qualified class name and your class
You can specify one or more constructor parameters for the configuration adapter class with this option.
To specify multiple parameters simply use the option more than once, e.g.
would result in the configuration adapter being instantiated like so:
new $adapter("One", "Two").
Use this option to provide a custom path to your provisional version. Your custom path is relative to the versions path.
Use this option, or
-p for short, to provide a custom path to your versions.
This allows you to override the default versions path which is
If the path provided is not an absolute path then it is assumed to be relative to the project root.