diff --git a/lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php b/lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php new file mode 100644 index 000000000..d375e2e59 --- /dev/null +++ b/lib/Doctrine/Migrations/Event/Listeners/RemoveMigrationTableFromSchemaListener.php @@ -0,0 +1,60 @@ +configuration = $dependencyFactory->getConfiguration(); + } + + /** {@inheritDoc} */ + public function getSubscribedEvents() + { + return [ + ToolEvents::postGenerateSchema, + ToolEvents::postGenerateComparisonSchema, + ]; + } + + public function postGenerateSchema(GenerateSchemaEventArgs $args): void + { + $this->removeMigrationsTableFromSchema($args->getSchema()); + } + + public function postGenerateComparisonSchema(GenerateSchemaEventArgs $args): void + { + $this->removeMigrationsTableFromSchema($args->getSchema()); + } + + private function removeMigrationsTableFromSchema(Schema $schema): void + { + $metadataConfiguration = $this->configuration->getMetadataStorageConfiguration(); + + if (! ($metadataConfiguration instanceof TableMetadataStorageConfiguration)) { + return; + } + + $tableName = $metadataConfiguration->getTableName(); + + if (! $schema->hasTable($tableName)) { + return; + } + + $schema->dropTable($tableName); + } +} diff --git a/lib/Doctrine/Migrations/Version/SortedMigrationPlanCalculator.php b/lib/Doctrine/Migrations/Version/SortedMigrationPlanCalculator.php index f7f96a8c9..cf631b598 100644 --- a/lib/Doctrine/Migrations/Version/SortedMigrationPlanCalculator.php +++ b/lib/Doctrine/Migrations/Version/SortedMigrationPlanCalculator.php @@ -45,7 +45,7 @@ public function getPlanForVersions(array $versions, string $direction): Migratio $availableMigrations = array_filter( $migrationsToCheck, // in_array third parameter is intentionally false to force object to string casting - static fn (AvailableMigration $availableMigration): bool => in_array($availableMigration->getVersion(), $versions, false) + static fn (AvailableMigration $availableMigration): bool => in_array($availableMigration->getVersion(), $versions, false), ); $planItems = array_map(static fn (AvailableMigration $availableMigration): MigrationPlan => new MigrationPlan($availableMigration->getVersion(), $availableMigration->getMigration(), $direction), $availableMigrations); diff --git a/tests/Doctrine/Migrations/Tests/Event/Listeners/RemoveMigrationTableFromSchemaListenerTest.php b/tests/Doctrine/Migrations/Tests/Event/Listeners/RemoveMigrationTableFromSchemaListenerTest.php new file mode 100644 index 000000000..1fd766cd8 --- /dev/null +++ b/tests/Doctrine/Migrations/Tests/Event/Listeners/RemoveMigrationTableFromSchemaListenerTest.php @@ -0,0 +1,76 @@ +getTableName(); + $this->configuration->setMetadataStorageConfiguration($metadataConfiguration); + + $schema = new Schema(); + $schema->createTable($tableName); + + static::assertTrue($schema->hasTable($tableName)); + + $this->listener->postGenerateSchema(new GenerateSchemaEventArgs($this->entityManager, $schema)); + + static::assertFalse($schema->hasTable($tableName)); + } + + public function testListenerIgnoresMissingTable(): void + { + $metadataConfiguration = new TableMetadataStorageConfiguration(); + $tableName = $metadataConfiguration->getTableName(); + $this->configuration->setMetadataStorageConfiguration($metadataConfiguration); + + $schema = new Schema(); + + static::assertFalse($schema->hasTable($tableName)); + + $this->listener->postGenerateSchema(new GenerateSchemaEventArgs($this->entityManager, $schema)); + + static::assertFalse($schema->hasTable($tableName)); + } + + public function testListenerIgnoresMissingConfiguration(): void + { + static::expectNotToPerformAssertions(); + + $this->listener->postGenerateSchema(new GenerateSchemaEventArgs($this->entityManager, new Schema())); + } + + protected function setUp(): void + { + $this->entityManager = $this->createMock(EntityManager::class); + + $this->configuration = new Configuration(); + + $dependencyFactory = DependencyFactory::fromEntityManager( + new ExistingConfiguration($this->configuration), + new ExistingEntityManager($this->entityManager), + ); + + $this->listener = new RemoveMigrationTableFromSchemaListener($dependencyFactory); + } +}