Skip to content

Commit

Permalink
lazyload migrations when command is run
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 22, 2015
1 parent 22d4859 commit bd61d88
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 32 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ extensions:
```yaml
migrations:
table: doctrine_migrations # database table for applied migrations
dirs: # list of dirs to load migrations from
- %appDir%/../migrations # first dir is used for generating migrations
- %appDir%/../migrations/2014
- %appDir%/../migrations/setup
directory: %appDir%/../migrations # directory, where all migrations are stored
namespace: Migrations # namespace of migration classes
codingStandard: tabs # or "spaces", coding style for generated classes
```
Expand Down Expand Up @@ -127,6 +124,21 @@ For further use, please check [docs in Symfony bundle](http://symfony.com/doc/cu

## 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.

It can look like this:

```
/migrations/
- VersionZZZ.php
/migrations/2015/
- VersionYYY.php
/migrations/basic-data
- VersionXXXX.php
```


### Injected migrations

Expand Down
2 changes: 1 addition & 1 deletion src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getVersion($version)


/**
* @param string $directory
* {@inheritdoc}
*/
public function setMigrationsDirectory($directory)
{
Expand Down
62 changes: 36 additions & 26 deletions src/DI/MigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ final class MigrationsExtension extends CompilerExtension
*/
private $defaults = [
'table' => 'doctrine_migrations',
'dirs' => [],
'directory' => '%appDir%/../migrations',
'dirs' => [], // deprecated, for BC, to be removed in version 3.0
'namespace' => 'Migrations',
'codingStandard' => CodeStyle::INDENTATION_TABS
];
Expand All @@ -34,10 +35,14 @@ final class MigrationsExtension extends CompilerExtension
public function loadConfiguration()
{
$containerBuilder = $this->getContainerBuilder();
$services = $this->loadFromFile(__DIR__ . '/../config/services.neon');
$this->compiler->parseServices($containerBuilder, $services);

$config = $this->getValidatedConfig($this->defaults);
$this->compiler->parseServices(
$containerBuilder,
$this->loadFromFile(__DIR__ . '/../config/services.neon')
);

$config = $this->getConfig($this->defaults);
$config = $this->getValidatedConfig($config);

$containerBuilder->addDefinition($this->prefix('codeStyle'))
->setClass(CodeStyle::class)
Expand All @@ -60,35 +65,15 @@ public function beforeCompile()
}


/**
* @return array
*/
private function getValidatedConfig(array $defaults)
{
$config = parent::getConfig($defaults);
if (count($config['dirs']) === 0) {
$containerBuilder = $this->getContainerBuilder();
$config['dirs'] = [$containerBuilder->expand('%appDir%/../migrations')];
}

return $config;
}


private function addConfigurationDefinition(array $config)
{
$containerBuilder = $this->getContainerBuilder();

$configurationDefinition = $containerBuilder->addDefinition($this->prefix('configuration'))
$containerBuilder->addDefinition($this->prefix('configuration'))
->setClass(Configuration::class)
->addSetup('setMigrationsTableName', [$config['table']])
->addSetup('setMigrationsDirectory', [reset($config['dirs'])])
->addSetup('setMigrationsDirectory', [$config['directory']])
->addSetup('setMigrationsNamespace', [$config['namespace']]);

$dirs = array_unique($config['dirs']);
foreach ($dirs as $dir) {
$configurationDefinition->addSetup('registerMigrationsFromDirectory', [$dir]);
}
}


Expand All @@ -112,4 +97,29 @@ private function loadCommandsToApplication()
}
}

/**
* @return array
*/
private function getValidatedConfig(array $configuration)
{
$this->validateConfig($configuration);
$configuration = $this->keepBcForDirsOption($configuration);
$configuration['directory'] = $this->getContainerBuilder()->expand($configuration['directory']);
return $configuration;
}


/**
* @deprecated To be removed in 3.0.
*
* @return array
*/
private function keepBcForDirsOption(array $configuration)
{
if (isset($configuration['dirs']) && count($configuration['dirs'])) {
$configuration['directory'] = reset($configuration['dirs']);
}
return $configuration;
}

}
62 changes: 62 additions & 0 deletions src/EventSubscriber/RegisterMigrationsEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* This file is part of Zenify
* Copyright (c) 2014 Tomas Votruba (http://tomasvotruba.cz)
*/

namespace Zenify\DoctrineMigrations\EventSubscriber;

use Doctrine\DBAL\Migrations\Configuration\Configuration;
use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand;
use Kdyby\Events\Subscriber;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;


final class RegisterMigrationsEventSubscriber implements Subscriber
{

/**
* @var Configuration
*/
private $configuration;


public function __construct(Configuration $configuration)
{
$this->configuration = $configuration;
}

/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return [ConsoleEvents::COMMAND => 'registerMigrations'];
}


public function registerMigrations(ConsoleCommandEvent $event)
{
$command = $event->getCommand();
if ( ! $this->isMigrationCommand($command)) {
return;
}

$this->configuration->registerMigrationsFromDirectory(
$this->configuration->getMigrationsDirectory()
);
}


/**
* @return bool
*/
private function isMigrationCommand(Command $command)
{
return $command instanceof AbstractCommand;
}

}
1 change: 1 addition & 0 deletions src/config/services.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
services:
- Zenify\DoctrineMigrations\OutputWriter
- {class: Zenify\DoctrineMigrations\EventSubscriber\ChangeCodingStandardEventSubscriber, tags: [kdyby.subscriber]}
- {class: Zenify\DoctrineMigrations\EventSubscriber\RegisterMigrationsEventSubscriber, tags: [kdyby.subscriber]}

# commands
- Doctrine\DBAL\Migrations\Tools\Console\Command\ExecuteCommand
Expand Down
11 changes: 11 additions & 0 deletions tests/Configuration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ protected function setUp()
{
$container = (new ContainerFactory)->create();
$this->configuration = $container->getByType(Configuration::class);

$this->configuration->registerMigrationsFromDirectory(
$this->configuration->getMigrationsDirectory()
);
}


Expand Down Expand Up @@ -52,4 +56,11 @@ public function testCreateDirectoryOnSet()
$this->assertFileExists($migrationsDir);
}


public function testLoadMigrationsFromSubdirs()
{
$migrations = $this->configuration->getMigrations();
$this->assertCount(2, $migrations);
}

}
27 changes: 27 additions & 0 deletions tests/Migrations/2015/Version546.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Zenify\DoctrineMigrations\Tests\Migrations;

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


final class Version546 extends AbstractMigration
{

/**
* {@inheritdoc}
*/
public function up(Schema $schema)
{
}


/**
* {@inheritdoc}
*/
public function down(Schema $schema)
{
}

}
2 changes: 1 addition & 1 deletion tests/Migrations/Version123.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Zenify\DoctrineMigrations\Tests\Configuration\ConfigurationSource\SomeService;


class Version123 extends AbstractMigration
final class Version123 extends AbstractMigration
{

/**
Expand Down

0 comments on commit bd61d88

Please sign in to comment.