From 83a2951a68e03e55f9b55f35b2377f689b2102f4 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Fri, 3 Oct 2025 18:30:38 +0200 Subject: [PATCH] fix: unfreeze dependencies after command --- src/DependencyFactory.php | 5 ++++ src/Tools/Console/Command/CurrentCommand.php | 2 +- src/Tools/Console/Command/DiffCommand.php | 2 +- src/Tools/Console/Command/DoctrineCommand.php | 11 +++++++++ .../Console/Command/DumpSchemaCommand.php | 2 +- src/Tools/Console/Command/ExecuteCommand.php | 2 +- src/Tools/Console/Command/GenerateCommand.php | 2 +- src/Tools/Console/Command/LatestCommand.php | 2 +- src/Tools/Console/Command/ListCommand.php | 2 +- src/Tools/Console/Command/MigrateCommand.php | 2 +- src/Tools/Console/Command/RollupCommand.php | 2 +- src/Tools/Console/Command/StatusCommand.php | 2 +- .../Console/Command/SyncMetadataCommand.php | 2 +- src/Tools/Console/Command/UpToDateCommand.php | 2 +- src/Tools/Console/Command/VersionCommand.php | 2 +- .../Console/Command/DoctrineCommandTest.php | 23 +++++++++++-------- 16 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/DependencyFactory.php b/src/DependencyFactory.php index fece1f5c7..80e1c7dfd 100644 --- a/src/DependencyFactory.php +++ b/src/DependencyFactory.php @@ -123,6 +123,11 @@ public function freeze(): void $this->frozen = true; } + public function unfreeze(): void + { + $this->frozen = false; + } + private function assertNotFrozen(): void { if ($this->frozen) { diff --git a/src/Tools/Console/Command/CurrentCommand.php b/src/Tools/Console/Command/CurrentCommand.php index b46ed83e0..84c6fac94 100644 --- a/src/Tools/Console/Command/CurrentCommand.php +++ b/src/Tools/Console/Command/CurrentCommand.php @@ -29,7 +29,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $aliasResolver = $this->getDependencyFactory()->getVersionAliasResolver(); diff --git a/src/Tools/Console/Command/DiffCommand.php b/src/Tools/Console/Command/DiffCommand.php index 751a142b3..339dd04ee 100644 --- a/src/Tools/Console/Command/DiffCommand.php +++ b/src/Tools/Console/Command/DiffCommand.php @@ -98,7 +98,7 @@ protected function configure(): void } /** @throws InvalidOptionUsage */ - protected function execute( + protected function doExecute( InputInterface $input, OutputInterface $output, ): int { diff --git a/src/Tools/Console/Command/DoctrineCommand.php b/src/Tools/Console/Command/DoctrineCommand.php index c1003bce7..2c2d7b064 100644 --- a/src/Tools/Console/Command/DoctrineCommand.php +++ b/src/Tools/Console/Command/DoctrineCommand.php @@ -119,6 +119,17 @@ protected function initialize(InputInterface $input, OutputInterface $output): v $dependencyFactory->freeze(); } + protected function execute(InputInterface $input, OutputInterface $output): int + { + try { + return $this->doExecute($input, $output); + } finally { + $this->dependencyFactory?->unfreeze(); + } + } + + abstract protected function doExecute(InputInterface $input, OutputInterface $output): int; + protected function getDependencyFactory(): DependencyFactory { if ($this->dependencyFactory === null) { diff --git a/src/Tools/Console/Command/DumpSchemaCommand.php b/src/Tools/Console/Command/DumpSchemaCommand.php index 86a37e493..f1f819e35 100644 --- a/src/Tools/Console/Command/DumpSchemaCommand.php +++ b/src/Tools/Console/Command/DumpSchemaCommand.php @@ -80,7 +80,7 @@ protected function configure(): void } /** @throws SchemaDumpRequiresNoMigrations */ - public function execute( + public function doExecute( InputInterface $input, OutputInterface $output, ): int { diff --git a/src/Tools/Console/Command/ExecuteCommand.php b/src/Tools/Console/Command/ExecuteCommand.php index 92d4101c4..137da9011 100644 --- a/src/Tools/Console/Command/ExecuteCommand.php +++ b/src/Tools/Console/Command/ExecuteCommand.php @@ -111,7 +111,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory(); $migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration($input); diff --git a/src/Tools/Console/Command/GenerateCommand.php b/src/Tools/Console/Command/GenerateCommand.php index 043d6485e..99f991ada 100644 --- a/src/Tools/Console/Command/GenerateCommand.php +++ b/src/Tools/Console/Command/GenerateCommand.php @@ -41,7 +41,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $migrationGenerator = $this->getDependencyFactory()->getMigrationGenerator(); diff --git a/src/Tools/Console/Command/LatestCommand.php b/src/Tools/Console/Command/LatestCommand.php index 5ce6af0c8..e47806c24 100644 --- a/src/Tools/Console/Command/LatestCommand.php +++ b/src/Tools/Console/Command/LatestCommand.php @@ -29,7 +29,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $aliasResolver = $this->getDependencyFactory()->getVersionAliasResolver(); diff --git a/src/Tools/Console/Command/ListCommand.php b/src/Tools/Console/Command/ListCommand.php index 86aa661f3..5ac827c4b 100644 --- a/src/Tools/Console/Command/ListCommand.php +++ b/src/Tools/Console/Command/ListCommand.php @@ -41,7 +41,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $versions = $this->getSortedVersions( $this->getDependencyFactory()->getMigrationPlanCalculator()->getMigrations(), // available migrations diff --git a/src/Tools/Console/Command/MigrateCommand.php b/src/Tools/Console/Command/MigrateCommand.php index 956a2fb4a..23252a9d1 100644 --- a/src/Tools/Console/Command/MigrateCommand.php +++ b/src/Tools/Console/Command/MigrateCommand.php @@ -139,7 +139,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $migratorConfigurationFactory = $this->getDependencyFactory()->getConsoleInputMigratorConfigurationFactory(); $migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration($input); diff --git a/src/Tools/Console/Command/RollupCommand.php b/src/Tools/Console/Command/RollupCommand.php index 1c97985d3..f89064da7 100644 --- a/src/Tools/Console/Command/RollupCommand.php +++ b/src/Tools/Console/Command/RollupCommand.php @@ -37,7 +37,7 @@ protected function configure(): void EOT); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $question = sprintf( 'WARNING! You are about to execute a migration in database "%s" that could result in schema changes and data loss. Are you sure you wish to continue?', diff --git a/src/Tools/Console/Command/StatusCommand.php b/src/Tools/Console/Command/StatusCommand.php index ce710dc91..c3e496dc6 100644 --- a/src/Tools/Console/Command/StatusCommand.php +++ b/src/Tools/Console/Command/StatusCommand.php @@ -32,7 +32,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $infosHelper = $this->getDependencyFactory()->getMigrationStatusInfosHelper(); $infosHelper->showMigrationsInfo($output); diff --git a/src/Tools/Console/Command/SyncMetadataCommand.php b/src/Tools/Console/Command/SyncMetadataCommand.php index 3bb68ed57..eeb983378 100644 --- a/src/Tools/Console/Command/SyncMetadataCommand.php +++ b/src/Tools/Console/Command/SyncMetadataCommand.php @@ -31,7 +31,7 @@ protected function configure(): void EOT); } - public function execute( + public function doExecute( InputInterface $input, OutputInterface $output, ): int { diff --git a/src/Tools/Console/Command/UpToDateCommand.php b/src/Tools/Console/Command/UpToDateCommand.php index dfcf07864..81d7dbb39 100644 --- a/src/Tools/Console/Command/UpToDateCommand.php +++ b/src/Tools/Console/Command/UpToDateCommand.php @@ -47,7 +47,7 @@ protected function configure(): void parent::configure(); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $statusCalculator = $this->getDependencyFactory()->getMigrationStatusCalculator(); diff --git a/src/Tools/Console/Command/VersionCommand.php b/src/Tools/Console/Command/VersionCommand.php index 63bfea873..1528f7110 100644 --- a/src/Tools/Console/Command/VersionCommand.php +++ b/src/Tools/Console/Command/VersionCommand.php @@ -101,7 +101,7 @@ protected function configure(): void } /** @throws InvalidOptionUsage */ - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { if ($input->getOption('add') === false && $input->getOption('delete') === false) { throw InvalidOptionUsage::new('You must specify whether you want to --add or --delete the specified version.'); diff --git a/tests/Tools/Console/Command/DoctrineCommandTest.php b/tests/Tools/Console/Command/DoctrineCommandTest.php index d192644a9..64aea7420 100644 --- a/tests/Tools/Console/Command/DoctrineCommandTest.php +++ b/tests/Tools/Console/Command/DoctrineCommandTest.php @@ -40,7 +40,7 @@ public function testCommandFreezes(): void $command = new class ($dependencyFactory) extends DoctrineCommand { - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { return 0; } @@ -67,7 +67,7 @@ public function testCustomConfiguration(): void $command = new class ($dependencyFactory) extends DoctrineCommand { - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $migrationDirectories = $this->getDependencyFactory()->getConfiguration()->getMigrationDirectories(); DoctrineCommandTest::assertSame(['DoctrineMigrationsTest' => 'bar'], $migrationDirectories); @@ -88,6 +88,11 @@ public function testDependencyFactoryIsSetFirst(): void $dependencyFactory = $this->createMock(DependencyFactory::class); $command = new class ($dependencyFactory) extends DoctrineCommand { + protected function doExecute(InputInterface $input, OutputInterface $output): int + { + return 0; + } + protected function configure(): void { $this->getDependencyFactory(); @@ -122,7 +127,7 @@ public function __construct(EntityManager $entityManager, DependencyFactory $dep $this->expectedEm = $entityManager; } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $em = $this->getDependencyFactory()->getEntityManager(); DoctrineCommandTest::assertSame($this->expectedEm, $em); @@ -163,7 +168,7 @@ public function __construct(Connection $connection, DependencyFactory $dependenc $this->expectedConnection = $connection; } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $connection = $this->getDependencyFactory()->getConnection(); DoctrineCommandTest::assertSame($this->expectedConnection, $connection); @@ -196,7 +201,7 @@ public function testCanNotSpecifyBothEmAndConnection(): void $command = new class ($dependencyFactory) extends DoctrineCommand { - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { return 0; } @@ -239,7 +244,7 @@ protected function configure(): void ); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { DoctrineCommandTest::assertSame($this->getNamespace($input, $output), 'DoctrineMigrations'); @@ -282,7 +287,7 @@ protected function configure(): void ); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { $this->getNamespace($input, $output); @@ -327,7 +332,7 @@ protected function configure(): void ); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { DoctrineCommandTest::assertSame($this->getNamespace($input, $output), 'DoctrineMigrations'); @@ -368,7 +373,7 @@ protected function configure(): void ); } - protected function execute(InputInterface $input, OutputInterface $output): int + protected function doExecute(InputInterface $input, OutputInterface $output): int { DoctrineCommandTest::assertSame($this->getNamespace($input, $output), 'DoctrineMigrationsTwo');