From 8ad6f645cdafde2daba3f3d06b4ca52bdc6deaff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 14 Mar 2024 16:20:20 +0100 Subject: [PATCH] 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. --- EventListener/SchemaFilterListener.php | 51 +++++++++++++++++++ Resources/config/services.xml | 5 ++ .../SchemaFilterListenerTest.php | 48 +++++++++++++++++ phpstan.neon.dist | 1 + 4 files changed, 105 insertions(+) create mode 100644 EventListener/SchemaFilterListener.php create mode 100644 Tests/Collector/EventListener/SchemaFilterListenerTest.php diff --git a/EventListener/SchemaFilterListener.php b/EventListener/SchemaFilterListener.php new file mode 100644 index 0000000..b88fc2b --- /dev/null +++ b/EventListener/SchemaFilterListener.php @@ -0,0 +1,51 @@ +enabled) { + return true; + } + + if ($asset instanceof AbstractAsset) { + $asset = $asset->getName(); + } + + return $asset !== (new TableMetadataStorageConfiguration())->getTableName(); + } + + public function disable(): void + { + $this->enabled = false; + } + + public function onConsoleCommand(ConsoleCommandEvent $event): void + { + $command = $event->getCommand(); + + if (! $command instanceof DoctrineCommand) { + return; + } + + $this->disable(); + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index cb97871..17ff210 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -146,6 +146,11 @@ + + + + + diff --git a/Tests/Collector/EventListener/SchemaFilterListenerTest.php b/Tests/Collector/EventListener/SchemaFilterListenerTest.php new file mode 100644 index 0000000..5a6b7f3 --- /dev/null +++ b/Tests/Collector/EventListener/SchemaFilterListenerTest.php @@ -0,0 +1,48 @@ +disable(); + + self::assertTrue($listener(new Table('doctrine_migration_versions'))); + self::assertTrue($listener(new Table('some_other_table'))); + } + + public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): void + { + $listener = new SchemaFilterListener(); + $migrationsCommand = new class extends DoctrineCommand { + }; + + $listener->onConsoleCommand(new ConsoleCommandEvent( + $migrationsCommand, + new ArrayInput([]), + new NullOutput() + )); + + self::assertTrue($listener(new Table('doctrine_migration_versions'))); + } +} diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 254e7cb..bc9317d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -3,6 +3,7 @@ parameters: phpVersion: 80200 paths: - %currentWorkingDirectory%/DependencyInjection + - %currentWorkingDirectory%/EventListener - %currentWorkingDirectory%/Tests ignoreErrors: