Implementation of Doctrine\Migrations to Nette.
composer require dtforce/doctrine-migrationsRegister extensions in config.neon:
extensions:
migrations: DTForce\DoctrineMigrations\DI\MigrationsExtensionconfig.neon with default values
migrations:
table: doctrine_migrations # database table for applied migrations
directory: %appDir%/../migrations # directory, where all migrations are stored
namespace: Migrations # namespace of migration classesOpen your CLI and run command (based on DTForce\NetteConsole integration):
php bin/consoleIf you want to migrate existing migration to your database, just run migrate commmand:
php bin/console migrations:migrateIf you get lost, just use -h option for help:
php bin/console migrations:migrate -hTo create new empty migration, just run:
php bin/console migrations:generateA 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
{
/**
* {@inheritdoc}
*/
public function up(Schema $schema)
{
$this->addSql("INSERT INTO 'user_role' (id, value, name) VALUES (3, 'superadmin', 'Super Admin')");
}
/**
* {@inheritdoc}
*/
public function down(Schema $schema)
{
$this->addSql("DELETE FROM 'user_role' WHERE ('id' = 3);");
}
}As simple as that!
For further use, please check docs in Symfony bundle.
If you have over 100 migrations in one directory, it might get messy. How to make it nicer? You can create a subdirectory and move some migrations there. I would group them up by year or by purpose. All subdirectories of directory you set up in configuration will be scanned.
It can look like this:
/migrations/
- VersionZZZ.php
/migrations/2015/
- VersionYYY.php
/migrations/basic-data
- VersionXXXX.php
namespace Migrations;
final class Version20140801152432 extends AbstractMigration
{
/**
* @inject
* @var Doctrine\ORM\EntityManagerInterface
*/
public $entityManager;
public function up(Schema $schema)
{
// ...
}
// ...
}