Phinx 0.2.3 is out with reversible migrations and much more

3 min read
phinxphp

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:

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:

gist:robmorgan/5325000#20130406160342_example_migration.php

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:

And now to showcase the power of reversible migrations we can simply roll it back:

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.