This article is a quick introduction to the Migrations plugin, open sourced a few weeks ago by our company. You will see how simple it is to use the plugin and what you could do with it. I hope this article will show you the benefits of using migrations in your CakePHP applications and make you give it a try right after the reading!
Here is a one-sentence description of the plugin: the Migrations plugin allows developers to easily version and automate the creation / update process of any database schema and application data from the command line.
For information, CakeDC uses this plugin on its project since several years to make team collaboration and deployment easier. The plugin has been entirely rewritten a few months ago and fully tested (code coverage >95% as always at CakeDC) before being open sourced under the MIT license. It is now available to the community along with its documentation... and it is free!
It has been a while since companies integrated Source Code Management in their development process and CVS, SVN, Mercurial or Git are now common tools. Inspired from the open source movement it is also a good practice for single developers to version application source code.
As you might know, an application almost always depends of the database schema it is aimed to use... however it is not easy to version both the source code and database schema with a SCM. Let's take the example of a CakePHP application: until now the only way to do was to version a single file, either a sql dump or a CakePHP schema.php file generated with the cake schema shell. These two approaches are not very convenient to use on a daily basis, the first one forcing the developer to drop and recreate the whole database every time!
Moreover, a web application development is never really finished (there are always new features to add, software updates or bug fixing to do...) and deploying these change on a test or production server is always a delicate task.
Here comes the Migrations plugin! It provides a simple and easy way to version a database... and to perform many other different tasks thanks to its callback system. Here are some features:
Announced a few weeks ago, a packaged version of the plugin can be downloaded from the new "Downloads" section of CakeDC.com. This page contains a link to download the 1.0 version, the plugin documentation and the Codaset project url for tickets and direct Git access to the repository.
To make people aware of the need to show their support to the Cake Software Fundation by donating a few bucks (this is unfortunately not done enough), the plugin was first available to donors only. The "Download without donation" button was added later, when the repository was made public! However, if you find this plugin useful please consider making a donation to the CSF... that is the best thing you could do for thanking us.
Even better! A sample application was also released for those who want to see how migrations could be used and integrated in an application. To play with it, Download the code or git clone the project using:
git clone git://codaset.com/cakedc/sample-migrations-application.git sample_migrations
You will only need to create a database.php configuration file and update CakePHP's core location to make the application work. Git users, run
git submodule init git submodule update
to automatically add the migrations plugin as a submodule!
Adding the plugin to an existing application is very simple. If you downloaded the archive containing the plugin code, unzip it in the "/plugins/migrations" folder of your application. Git users can add it as a submodule with the following command:
git submodule add git://codaset.com/cakedc/migrations.git plugins/migrations
To check that it is installed correctly, execute the following command from your application root (it will display the available command to use the plugin):
cake migration help
If you encounter any problem here, please read the official documentation about CakePHP's console usage.
This post is not aimed at providing a comprehensive tutorial on how to use the plugin, thus I will just introduce the most useful commands along with some use cases.
For a complete documentation, please read the official documentation provided on the plugin page. For a simple (but useful for understanding purpose) use case you can take a look at the sample application introduced above. Going through the commit history will allow you to understand how migrations could be used in a development process.
To generate a new migration, type the following command
cake migration generate
The tool will ask you to give a name to the migration and suggest to do a dump of the current database schema. If a "schema.php" file is found in the application, it will ask you if you want to generate a diff between this schema and your current database one.
Generated migration files will be added to the "/config/migrations" application directory.
When you pull an application containing migrations, several commands are available to apply or revert migrations. The simplest one is:
It will display all the found migrations along with their status (applied or not applied) and id number. Just enter a migration number to update your database to the correct version. Some convenience commands are also available. You can use:
cake migration up, down, all or reset
These commands will respectively:
Adding plugins to an existing application often implies adding new tables to the database or altering existing ones. The Migrations plugin brings a quick and efficient way to automate this installation. On the one hand developers can easily add necessary migrations to their plugin (making upgrades easier), on the other hand users can apply them as easily.
The only difference compared with commands introduced above is the parameter "-plugin pluginname" that needs to be added. Here is how the user will install the database for the newly added / updated plugin "test":
cake migration run all -plugin test
I would like to highlight the fact that callbacks allow the developer to do everything they want before / after each migration. It is convenient for adding initial data, and one can even implement a callback method opening the bootstrap.php file to append plugin's configuration entries there (it is just an example ;)).
Of course, feel free to add any remark or example of migrations use in the comments.
As this post is not aimed at providing support for the plugin, I recommend you to use the official tools available:
I hope you enjoyed this post, it is now time for you to start playing with the Migrations plugin...
CakePHP 2 Application Cookbook
Written by James on August 11, 2014
CakeDC Git Workflow - Working Together
Written by Lubomir on July 31, 2014
Users plugin 2.1.0 released
Written by Florian on July 8, 2014
CakePHP 3.0 is coming
Written by Larry on June 28, 2014
CakeDC Git Workflow - Tips and Tricks
Written by Lubomir on June 24, 2014
Comments plugin 1.3.0 released
Written by Florian on June 17, 2014
The annual CakePHP conference
Written by James on June 12, 2014
Tags plugin 1.6.0 released
Written by Florian on June 4, 2014
CakeDC Git Workflow - An Introduction
Written by Lubomir on May 31, 2014
Search plugin 2.3.0 released
Written by Florian on May 23, 2014