From 2cfbb72e546646bf49f9767c57aa95e2e9066285 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 +++++++++++++++++ 3 files changed, 104 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'))); + } +}