Skip to content

Commit c85cd25

Browse files
committed
Add support for --complete
When using orm:schema-tool:update together with --complete, all assets not described by the current metadata is dropped. This is an issue when using doctrine/migrations, because not using that option is deprecated, and because when it is used, the table that holds the migrations is dropped as well since it is not described by ORM metadata. A solution to that is configuring an asset filter that filters out the metadata table except when running commands inside the migrations namespace.
1 parent 2609656 commit c85cd25

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

config/services.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
<tag name="console.command" command="doctrine:migrations:version" />
147147
</service>
148148

149+
<service id="doctrine_migrations.schema_filter_listener" class="Doctrine\Bundle\MigrationsBundle\EventListener\SchemaFilterListener">
150+
<tag name="kernel.event_listener" event="console.command" method="onConsoleCommand" />
151+
<tag name="doctrine.dbal.schema_filter" />
152+
</service>
153+
149154
</services>
150155

151156
</container>

src/DependencyInjection/DoctrineMigrationsExtension.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,21 @@ public function load(array $configs, ContainerBuilder $container): void
9090
$diDefinition->addMethodCall('setDefinition', [$doctrineId, new Reference($symfonyId)]);
9191
}
9292

93-
if (! isset($config['services'][MetadataStorage::class])) {
93+
if (isset($config['services'][MetadataStorage::class])) {
94+
$container->removeDefinition('doctrine_migrations.schema_filter_listener');
95+
} else {
96+
$filterDefinition = $container->getDefinition('doctrine_migrations.schema_filter_listener');
9497
$storageConfiguration = $config['storage']['table_storage'];
9598

9699
$storageDefinition = new Definition(TableMetadataStorageConfiguration::class);
97100
$container->setDefinition('doctrine.migrations.storage.table_storage', $storageDefinition);
98101
$container->setAlias('doctrine.migrations.metadata_storage', 'doctrine.migrations.storage.table_storage');
99102

100-
if ($storageConfiguration['table_name'] !== null) {
103+
if ($storageConfiguration['table_name'] === null) {
104+
$filterDefinition->addArgument('doctrine_migration_versions');
105+
} else {
101106
$storageDefinition->addMethodCall('setTableName', [$storageConfiguration['table_name']]);
107+
$filterDefinition->addArgument($storageConfiguration['table_name']);
102108
}
103109

104110
if ($storageConfiguration['version_column_name'] !== null) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Bundle\MigrationsBundle\EventListener;
6+
7+
use Doctrine\DBAL\Schema\AbstractAsset;
8+
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
9+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
10+
11+
/**
12+
* Acts as a schema filter that hides the migration metadata table except
13+
* when the execution context is that of command inside the migrations
14+
* namespace.
15+
*/
16+
final class SchemaFilterListener
17+
{
18+
/** @var string */
19+
private $configurationTableName;
20+
21+
public function __construct(string $configurationTableName)
22+
{
23+
$this->configurationTableName = $configurationTableName;
24+
}
25+
26+
/** @var bool */
27+
private $enabled = true;
28+
29+
/** @param AbstractAsset|string $asset */
30+
public function __invoke($asset): bool
31+
{
32+
if (! $this->enabled) {
33+
return true;
34+
}
35+
36+
if ($asset instanceof AbstractAsset) {
37+
$asset = $asset->getName();
38+
}
39+
40+
return $asset !== $this->configurationTableName;
41+
}
42+
43+
private function disable(): void
44+
{
45+
$this->enabled = false;
46+
}
47+
48+
public function onConsoleCommand(ConsoleCommandEvent $event): void
49+
{
50+
$command = $event->getCommand();
51+
52+
if (! $command instanceof DoctrineCommand) {
53+
return;
54+
}
55+
56+
$this->disable();
57+
}
58+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Bundle\MigrationsBundle\Tests\Collector\EventListener;
6+
7+
use Doctrine\Bundle\MigrationsBundle\EventListener\SchemaFilterListener;
8+
use Doctrine\DBAL\Schema\Table;
9+
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\Console\Event\ConsoleCommandEvent;
12+
use Symfony\Component\Console\Input\ArrayInput;
13+
use Symfony\Component\Console\Output\NullOutput;
14+
15+
class SchemaFilterListenerTest extends TestCase
16+
{
17+
public function testItFiltersOutMigrationMetadataTableByDefault(): void
18+
{
19+
$listener = new SchemaFilterListener('doctrine_migration_versions');
20+
21+
self::assertFalse($listener(new Table('doctrine_migration_versions')));
22+
self::assertTrue($listener(new Table('some_other_table')));
23+
}
24+
25+
public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): void
26+
{
27+
$listener = new SchemaFilterListener('doctrine_migration_versions');
28+
$migrationsCommand = new class extends DoctrineCommand {
29+
};
30+
31+
$listener->onConsoleCommand(new ConsoleCommandEvent(
32+
$migrationsCommand,
33+
new ArrayInput([]),
34+
new NullOutput()
35+
));
36+
37+
self::assertTrue($listener(new Table('doctrine_migration_versions')));
38+
}
39+
}

0 commit comments

Comments
 (0)