Friday, July 17, 2009

Announcing MP: Migrations for PHP

As my latest project has grown to need multiple developers, we have started to run into some friction in managing concurrent database changes. Over the last few months I've done a bunch of research on migrations infrastructure, including rails migrations, CakePHP migrations, and several other less-popular implementations.

None of them were perfect for my needs and since the architecture for migrations is pretty simple, I decided to write my own and open-source the results, a php migrations infrastructure called MP.

A few of the features I wanted in MP:
  • Easy-to-use
  • 100% PHP 5+ implementation
  • Command-line utility
  • API access for integration into existing apps, frameworks, or ORMs.
  • Ability to execute arbitrary code in each migration
  • Rollback capabilities
  • "Clean" option to rebuild app state from scratch
  • Persist current version in a file or in DB
  • Prevent collisions of migrations
  • Automatically preserve migrations order without manual sorting effort
Getting Started with MP
One you download mp, you can migration-enable your application in moments:

$ cd myproject

# Migration-enable myproject by creating a first migration
$ mp -c create
MP - The PHP Migrator.
Created migration 20090717_014019 at ./migrations/20090717_014019.php.

# migrate to latest migration
$ mp
MP - The PHP Migrator.

Upgrading from version 0 to 20090717_014019.
Running Upgrade: Migration created at 20090717_014019.
Upgrading to 20090717_014019 succeeded.

To implement the specifics of your migration code, just edit the corresponding migration file's up() method.

Integration
MP's API is intended as an integration point for frameworks to be able to directly implement migrations support without having to write a migrations infrastructure from scratch. Your framework's CLI utilities can programmatically configure, execute and create migrations easily. MP can also be configured with a delegate class to which all migrations have access, making it easy to bootstrap your framework before running migrations so that all of your framework's infrastructure is available for use inside the migrations with no additional effort.

Conclusion
I hope that MP can become a widely adoption standard for php migrations. I will continue to work on it and integrate it into the PHOCOA php framework and also add support for SQL migrations with Propel.

4 comments:

  1. Hi Alan,

    First I'd like to thank you for releasing this library :-)

    The only thing that I missed was that the command line utility doesn't work on windows because of 'getopt' method. It is a method that only works on windows since PHP 5.3

    I was working on something similar but didn't have the time to get to the migration itself. I started implementing what you've called 'generic schema manipulation API' and I'm willing to integrate it into MP soon.


    Regards,
    Fábio Rehm
    ReplyDelete
  2. Fabio-

    Thanks for the comment! I am excited to see people excited about the project.

    I didn't realize getopt didn't work on windows. PHP getopt kinda sucks anyway, as you'll noticed long options don't work either since they don't work on my platform.

    I actually want to re-write the options infrastructure, too. That might be another little project :) Good CLI apps are really useful but unfortunately there's not really any good tools to do so in PHP that I know of.

    Please let me know if you need anything for working on the schema manipulation API. Clearly it'd be a great addition for apps that care about db-agnosticism.

    Thanks!
    Alan
    ReplyDelete
  3. Hey - this sounds pretty awesome. How are you handling different RDBMS? I'm also very interested to see your support for stored procedures (or PL/SQL in Oracle).

    Thank you for working on this!
    ReplyDelete
  4. Right now mp just provides infrastructure for running SQL, so you can just put the SQL statements to load your stored procedures directly into your migrations.

    I've never tested on Oracle. I'd love to hear if it works well (I don't see any reason it shouldn't).

    Also someone else is working on a OO DDL for use from inside mp migrations.

    Alan
    ReplyDelete