Skip to content
idler edited this page Jul 31, 2012 · 1 revision

Table of Contents

Mysql Migrations with PHP

MMP is a tool to enable automatic creation of MySQL migration scripts and to handle their loading/removal to a given database schema.

It can:

  • create initial schema from an existing database
  • load the schema into a database (for example on new test or production servers)
  • create migration scripts (as PHP classes)
  • Load migrations into a database
  • Update to a specified revision (time)

Configuration

The configuration is done by creating a config.ini file in MMP's main folder. A sample config file called config.ini.sample is provided to help a little bit with that.

The location of config file can be overridden via command line option --config="<path_to_file></path_to_file>"

Available configuration options:

Option Description
host Hostname of the database server
user User on the database server
password Password for the database user
db Database to track for any schema changes
exclude_tables Tables matching this regex pattern will not be tracked for changes. Value should be quoted (ex.)
savedir Folder where migration files shall be stored
verbose Outputs additional info when executing commands (a bunch of queries at the moment)
versiontable Database table where revision numbers of the applied migrations will be stored
Note: all the configuration options can also be supplied via command line. Their format in that case would be --config option=value. For example: --host=localhost, --db="testing", etc.

Commands

All MMP commands are supplied in the form like this: ./migration.php [configuration] command [command]

!Command ! Description
help List available commands. Use help command for a more detailed description of each.
schema Take initial snapshot of the database schema.
init Load initial database schema (of what's been created with schema command above). No migrations are loaded after this.
create Generate a new migration.
list List available and applied migrations.
migrate Load or remove migrations. Target migration point is provided as an absolute or relative date from the current moment, in the format supported by strtotime() php function, such as "-2 hours", "2011.03.01 16:00", etc. If nothing is provided, all migrations up to current time will be loaded.

Usage

To start using this tool you must first create the initial schema

./migration.php schema

Then add db/ dir to version control (or whatever else savedir was set to).

Now you can change database structure and then call:

./migration.php create

Add the newly-created migration file to version control, go to the another working copy, update filesystem from VC, and load initial schema

./migration.php init

Now your initial database created and you can migrate to any time

./migration.php migrate -2 hour

Or migrate database to latest version

./migration.php migrate

./migration.php help

                    MySQL Migration with PHP
 ---------------------------------------------------------------------
  help:       display this help and exit
  schema:     create schema for initial migration/installation
  init:       load initial schema (install)
  create:     create new migration
  migrate:    migrate to specified time
 
 In migrate comand you can use strtotime format
 Examples:
 *********************************************************************
 ./migrate.php migrate yestarday
 ./migrate.php migrate -2 hour
 ./migrate.php migrate +2 month
 ./migrate.php migrate 20 September 2001
 ./migrate.php migrate
 ********************************************************************
 Last example will update your database to last version 
  
 
 ---------------------------------------------------------------------
 Licenced under: GPL v3
 Author: Maxim Antonov &lt;max.antonoff@gmail.com&gt;&lt;/max.antonoff@gmail.com&gt;
 

Russian version of this tutorial

Clone this wiki locally