From 66c74dcca50d8f421ef6430cbc1f5a85e2641544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 20 Jan 2025 18:20:18 +0100 Subject: [PATCH 1/2] Remove useless private method There is only one call to it. --- src/EventListener/SchemaFilterListener.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/EventListener/SchemaFilterListener.php b/src/EventListener/SchemaFilterListener.php index 203739b..6116cbc 100644 --- a/src/EventListener/SchemaFilterListener.php +++ b/src/EventListener/SchemaFilterListener.php @@ -40,11 +40,6 @@ public function __invoke($asset): bool return $asset !== $this->configurationTableName; } - private function disable(): void - { - $this->enabled = false; - } - public function onConsoleCommand(ConsoleCommandEvent $event): void { $command = $event->getCommand(); @@ -53,6 +48,6 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void return; } - $this->disable(); + $this->enabled = false; } } From 31417c043f62ca9a77bdd57e4b0eb4145e8f7e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sun, 19 Jan 2025 22:23:10 +0100 Subject: [PATCH 2/2] Apply schema filter more selectively It is more careful, since there seems to be a lot of situations where we do not want to apply the filter. Let us take the opposite approach and: - Have the filter disabled by default, instead of enabled by default. - Enable it for precise commands where we know we need it. --- src/EventListener/SchemaFilterListener.php | 9 ++-- .../SchemaFilterListenerTest.php | 41 ++++++++++++++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/EventListener/SchemaFilterListener.php b/src/EventListener/SchemaFilterListener.php index 6116cbc..bba1622 100644 --- a/src/EventListener/SchemaFilterListener.php +++ b/src/EventListener/SchemaFilterListener.php @@ -5,7 +5,8 @@ namespace Doctrine\Bundle\MigrationsBundle\EventListener; use Doctrine\DBAL\Schema\AbstractAsset; -use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand; +use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand; +use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand; use Symfony\Component\Console\Event\ConsoleCommandEvent; /** @@ -24,7 +25,7 @@ public function __construct(string $configurationTableName) } /** @var bool */ - private $enabled = true; + private $enabled = false; /** @param AbstractAsset|string $asset */ public function __invoke($asset): bool @@ -44,10 +45,10 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void { $command = $event->getCommand(); - if (! $command instanceof DoctrineCommand) { + if (! $command instanceof ValidateSchemaCommand && ! $command instanceof UpdateCommand) { return; } - $this->enabled = false; + $this->enabled = true; } } diff --git a/tests/Collector/EventListener/SchemaFilterListenerTest.php b/tests/Collector/EventListener/SchemaFilterListenerTest.php index 2d2e0a1..f1e47d9 100644 --- a/tests/Collector/EventListener/SchemaFilterListenerTest.php +++ b/tests/Collector/EventListener/SchemaFilterListenerTest.php @@ -7,6 +7,10 @@ use Doctrine\Bundle\MigrationsBundle\EventListener\SchemaFilterListener; use Doctrine\DBAL\Schema\Table; use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand; +use Doctrine\ORM\Tools\Console\Command\AbstractEntityManagerCommand; +use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand; +use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand; +use Doctrine\ORM\Tools\Console\EntityManagerProvider; use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Input\ArrayInput; @@ -14,18 +18,17 @@ class SchemaFilterListenerTest extends TestCase { - public function testItFiltersOutMigrationMetadataTableByDefault(): void + public function testItFiltersNothingByDefault(): void { $listener = new SchemaFilterListener('doctrine_migration_versions'); - - self::assertFalse($listener(new Table('doctrine_migration_versions'))); + self::assertTrue($listener(new Table('doctrine_migration_versions'))); self::assertTrue($listener(new Table('some_other_table'))); } - public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): void + public function testItFiltersNothingWhenNotRunningSpecificCommands(): void { $listener = new SchemaFilterListener('doctrine_migration_versions'); - $migrationsCommand = new class extends DoctrineCommand { + $migrationsCommand = new class () extends DoctrineCommand { }; $listener->onConsoleCommand(new ConsoleCommandEvent( @@ -35,5 +38,33 @@ public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): )); self::assertTrue($listener(new Table('doctrine_migration_versions'))); + self::assertTrue($listener(new Table('some_other_table'))); + } + + /** + * @param class-string $command + * + * @dataProvider getCommands + */ + public function testItFiltersOutMigrationMetadataTableWhenRunningSpecificCommands(string $command): void + { + $listener = new SchemaFilterListener('doctrine_migration_versions'); + $ormCommand = new $command($this->createStub(EntityManagerProvider::class)); + + $listener->onConsoleCommand(new ConsoleCommandEvent( + $ormCommand, + new ArrayInput([]), + new NullOutput() + )); + + self::assertFalse($listener(new Table('doctrine_migration_versions'))); + self::assertTrue($listener(new Table('some_other_table'))); + } + + /** @return iterable}> */ + public static function getCommands(): iterable + { + yield 'orm:validate-schema' => [ValidateSchemaCommand::class]; + yield 'orm:schema-tool:update' => [UpdateCommand::class]; } }