Doctrine Migrations
Implementation of Doctrine\Migrations to Nette., (*1)
Install
composer require dtforce/doctrine-migrations
Register extensions in config.neon
:, (*2)
extensions:
migrations: DTForce\DoctrineMigrations\DI\MigrationsExtension
Configuration
config.neon
with default values, (*3)
migrations:
table: doctrine_migrations # database table for applied migrations
directory: %appDir%/../migrations # directory, where all migrations are stored
namespace: Migrations # namespace of migration classes
Usage
Open your CLI and run command (based on DTForce\NetteConsole
integration):, (*4)
php bin/console
Migrate changes to database
If you want to migrate existing migration to your database, just run migrate commmand:, (*5)
php bin/console migrations:migrate
If you get lost, just use -h
option for help:, (*6)
php bin/console migrations:migrate -h
Create new migration
To create new empty migration, just run:, (*7)
php bin/console migrations:generate
A new empty migration will be created at your migrations directory. You can add your sql there then., (*8)
Migration that would add new role "superadmin"
to user_role
table would look like this:, (*9)
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!, (*10)
For further use, please check docs in Symfony bundle., (*11)
Features
Cleanup your directories
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., (*12)
It can look like this:, (*13)
```
/migrations/
- VersionZZZ.php
/migrations/2015/
- VersionYYY.php
/migrations/basic-data
- VersionXXXX.php, (*14)
### Injected migrations
```php
namespace Migrations;
final class Version20140801152432 extends AbstractMigration
{
/**
* @inject
* @var Doctrine\ORM\EntityManagerInterface
*/
public $entityManager;
public function up(Schema $schema)
{
// ...
}
// ...
}