Statistics
100313
26
0
172d
Badges

Doctrine Migrations

Build Status Quality Score Code Coverage Downloads this Month Latest stable

Implementation of Doctrine\Migrations to Nette.

Install

composer require zenify/doctrine-migrations

Register extensions in config.neon:

extensions:
	- Arachne\ContainerAdapter\DI\ContainerAdapterExtension
	- Arachne\EventDispatcher\DI\EventDispatcherExtension
	migrations: Zenify\DoctrineMigrations\DI\MigrationsExtension

	# Kdyby\Doctrine or another Doctrine integration
	doctrine: Kdyby\Doctrine\DI\OrmExtension

Configuration

config.neon with default values

migrations:
	table: doctrine_migrations # database table for applied migrations
	column: version # database column for applied migrations
	directory: %appDir%/../migrations # directory, where all migrations are stored
	namespace: Migrations # namespace of migration classes
	codingStandard: tabs # or "spaces", coding style for generated classes
	versionsOrganization: null # null, "year" or "year_and_month", organizes migrations to subdirectories

Usage

Open your CLI and run command (based on Kdyby\Console integration):

php www/index.php

And then you should see all available commands:

CLI commands

Migrate changes to database

If you want to migrate existing migration to your database, just run migrate commmand:

php www/index.php migrations:migrate

If you get lost, just use -h option for help:

php www/index.php migrations:migrate -h

Create new migration

To create new empty migration, just run:

php www/index.php migrations:generate

A new empty migration will be created at your migrations directory. You can add your sql there then.

Migration that would add new role "superadmin" to user_role table would look like this:

namespace Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
 * New role "superadmin" added.
 */
final class Version20151015000003 extends AbstractMigration
{

	public function up(Schema $schema)
	{
		$this->addSql("INSERT INTO 'user_role' (id, value, name) VALUES (3, 'superadmin', 'Super Admin')");
	}
	

	public function down(Schema $schema)
	{
		$this->addSql("DELETE FROM 'user_role' WHERE ('id' = 3);");
	}

}

Simple as that!

For further use, please check docs in Symfony bundle.

Features

Migrations organization

If you have over 100 migrations in one directory, it might get messy. Fortunately doctrine migrations can organize your migrations to directories by year or by year and month. You can configure it in your config.neon (see above).

/migrations/2015/11
	- VersionXXX.php
/migrations/2015/12
	- VersionYYY.php
/migrations/2016/01
	- VersionZZZ.php

Injected migrations

Note: this is not really best practise, so try to use it only if there is no other way.

namespace Migrations;

final class Version20140801152432 extends AbstractMigration
{

	/**
	 * @inject
	 * @var Doctrine\ORM\EntityManagerInterface
	 */
	public $entityManager;


	public function up(Schema $schema)
	{
		// ...
	}

	// ...

}

Testing

composer check-cs
vendor/bin/phpunit

Contributing

Rules are simple:

  • new feature needs tests
  • all tests must pass
  • 1 feature per PR

We would be happy to merge your feature then!

  • v3.0.0 v3.0.0

    • Add "column" option
    • Add "versionsOrganization" option
    • Switch from symnedi/event-dispatcher to arachne/event-dispatcher
    • Remove deprecated features
  • v2.3.5 v2.3.5

    • Fix compatibility with Nette 2.4 (thx @Vrtak-CZ).
  • v2.3.4 v2.3.4

  • v2.3.0 Released version 2.3

    Changes

    • [#20] Kdyby\Events replaced by Symfony\EventDispatcher
  • v2.2.3 Released version 2.2.3

    Changes

    • use options directory instead of dirs (nested directories are supported by doctrine/migrations, see doctrine/migrations#238)
    • directory is created by default
    • migrations are loaded only on migrations command run
  • v2.2.1 Released version 2.2

    This version now depends on first stable version of doctrine/migrations! Hooray!

    So installation is now simple as:

    require zenify/doctrine-migrations
    

    New features

    • [#19] --allow-empty-migration fixed

    Changes

    • enable/disable option removed, to confusing with no added value
  • v2.1.0 Released version 2.1

    New features

    • compatible with Nette 2.3
    • now uses PHPUnit for testing
    • README in now complete with rich Usage section

    Bug fixes

    • added missing Kdyby\Doctrine dependency

    Full diff: v2.0.0...v2.1.0

  • v0.8.2 Released version 0.8.2

    • PHP 5.3 support added, thx to @Nemrtvej
    • tests added
    • configuration is now validated

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com