I am really excited to announce Phinx 0.2.3. Phinx has come a long way since it started as a tiny open-source tool I used on a few projects. It has received a stack of contributions and is used by thousands of people from all around the world.
Phinx now includes some great features including reversible migrations, a verbosity parameter, support for foreign keys and PHP configuration files. I want to talk briefly about the first two today.
When using reversible migrations you only need to define the ‘up logic’ and Phinx is smart enough to figure out how to migrate down automatically for you. This feature was inspired by the work tenderlove introduced into Rails 3.1.
To define a reversible migration you simply add a change method to your migration file and insert the database transformations you require. If Phinx detects the change method it will automatically handle the migration both up and down for you.
Note: Not all commands can be reversed. Phinx will throw an exception if you try to use an irreversible command.
Let me demonstrate reversible migrations in an example project. First we create a new migration:
$ php vendor/bin/phinx create ExampleMigration Phinx by Rob Morgan. version 0.2.3 using config file ./phinx.yml using config parser yaml using migration path /Users/robbym/Sites/webapp/scripts/migrations created ./scripts/migrations/20130406160342_example_migration.php
And open it using our text editor. Next I’ll uncomment the change method and add some code to create a simple blog posts table. Here’s the source code for the file:
Now when combined with the new verbose parameter you can see detailed insight into exactly how long various commands take to execute. This is very handy to benchmark various operations. Let’s run the migration we created using the verbose parameter:
$ php vendor/bin/phinx migrate -e testing -v Phinx by Rob Morgan. version 0.2.3 using config file ./phinx.yml using config parser yaml using migration path /Users/robbym/Sites/webapp/scripts/migrations using environment testing using adapter mysql using database webapp == 20130406160342 ExampleMigration: migrating -- createTable('posts') -> 0.0863s == 20130406160342 ExampleMigration: migrated 0.0918s All Done. Took 0.1215s
And now to showcase the power of reversible migrations we can simply roll it back:
$ php vendor/bin/phinx rollback -e testing -v Phinx by Rob Morgan. version 0.2.3 using config file ./phinx.yml using config parser yaml using migration path /Users/robbym/Sites/webapp/scripts/migrations using environment testing == 20130406160342 ExampleMigration: reverting == 20130406160342 ExampleMigration: reverted 0.0059s
There we have it. Phinx was smart enough to reverse the createTable command and drop it when migrating down. Reversible migrations save you both code and time.
Both these features bring Phinx in-line with other migration tools and platforms. More information on reversible migrations is available on the official Phinx documentation. Thanks for using my project. If your interested in contributing the GitHub project is here: https://github.com/robmorgan/phinx.