2017 © Pedro Peláez
 

library migration-service-provider

Doctrine migration service provider for Silex

image

knplabs/migration-service-provider

Doctrine migration service provider for Silex

  • Saturday, June 18, 2016
  • by ubermuda
  • Repository
  • 22 Watchers
  • 18 Stars
  • 5,940 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 16 Forks
  • 5 Open issues
  • 1 Versions
  • 2 % Grown

The README.md

DEPRECATED

Unfortunately we decided to not maintain this project anymore (see why). If you want to mark another package as a replacement for this one please send an email to hello@knplabs.com., (*1)

Migrations

This is a simple homebrew schema migration system for silex and doctrine., (*2)

Install

As usual, just include knplabs/migration-service-provider in your composer.json (don't tell me you don't have one, it's 2012 already), and register the service. You will have to pass the migration.path option, which should contain the path to your migrations files:, (*3)

$app->register(new \Knp\Provider\MigrationServiceProvider(), array(
    'migration.path' => __DIR__.'/../src/Resources/migration'
));

Enough small talk, I want to write migrations!

And I am too lazy to write a comprehensive documentation right now, so you will have to rely on two external resources:, (*4)

  1. The marketplace's migrations
  2. The official documentation for Doctrine's DBAL Schema Manager

Running migrations

There are two ways of running migrations, (*5)

Using the before handler

If you pass a migration.register_before_handler (set to true) when registering the service, then a before handler will be registered for migration to be run. It means that the migration manager will be run for each hit to your application., (*6)

You might want to enable this behavior for development mode, but please don't do that in production!, (*7)

Using the knp:migration:migrate command

If you installed the console service provider right, you can use the knp:migration:migrate command., (*8)

Writing migrations

A migration consist of a single file, holding a migration class. By design, the migration file must be named something like <version>_<migration_name>Migration.php and located in src/Resources/migrations, and the class <migration_name>Migration. For example, if your migration adds a bar field to the foo table, and is the 5th migration of your schema, you should name your file 05_FooBarMigration.php, and the class would be named FooBarMigration., (*9)

In addition to these naming conventions, your migration class must extends Knp\Migration\AbstractMigration, which provides a few helping method such as getVersion and default implementations for migration methods., (*10)

The migration methods consist of 4 methods:, (*11)

  • schemaUp
  • schemaDown
  • appUp
  • appDown

The names are pretty self-explanatory. Each schema* method is fed a Doctrine\DBAL\Schema\Schema instance of which you're expected to work to add, remove or modify fields and/or tables. The app* method are given a Silex\Application instance, actually your very application. You can see an example of useful appUp migration in the marketplace's CommentMarkdownCacheMigration., (*12)

Migration infos

There's one last method you should know about: getMigrationInfo. This method should return a self-explanatory description of the migration (it is optional though, and you can skip its implementation). When a migration implementing the getMigrationInfo method is run, and if you use twig, a global variable is set in your twig environment containing an array of all run migration informations., (*13)

You can then use it with something like that:, (*14)

      {% if migration_infos is defined %}
        <div class="alert alert-success">
          <p>Some migrations have been run:</p>
          <ul>
          {% for info in migration_infos %}
            <li>{{ info }}</li>
          {% endfor %}
        </div>
      {% endif %}

The Versions

18/06 2016

dev-master

9999999-dev http://knplabs.com

Doctrine migration service provider for Silex

  Sources   Download

MIT

The Requires

 

silex migration doctrine2