From ad2a79f564590c11aecd8b00928056487a450f1a Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Sun, 27 Oct 2024 17:28:01 +0100 Subject: [PATCH] fix: add missing drop schema when dropping database --- .../Builder/DropSchemaObjectsSQLBuilder.php | 21 +++++++++++++++++++ .../Schema/PostgreSQLSchemaManagerTest.php | 19 +++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/SQL/Builder/DropSchemaObjectsSQLBuilder.php b/src/SQL/Builder/DropSchemaObjectsSQLBuilder.php index c0384897fa2..a6fbdda2471 100644 --- a/src/SQL/Builder/DropSchemaObjectsSQLBuilder.php +++ b/src/SQL/Builder/DropSchemaObjectsSQLBuilder.php @@ -23,6 +23,7 @@ public function buildSQL(Schema $schema): array return array_merge( $this->buildSequenceStatements($schema->getSequences()), $this->buildTableStatements($schema->getTables()), + $this->buildNamespaceStatements($schema->getNamespaces()), ); } @@ -51,4 +52,24 @@ private function buildSequenceStatements(array $sequences): array return $statements; } + + /** + * @param list $namespaces + * + * @return list + */ + private function buildNamespaceStatements(array $namespaces): array + { + if (! $this->platform->supportsSchemas()) { + return []; + } + + $statements = []; + + foreach ($namespaces as $namespace) { + $statements[] = $this->platform->getDropSchemaSQL($namespace); + } + + return $statements; + } } diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index 2480a472163..c375d8c5519 100644 --- a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php +++ b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php @@ -366,6 +366,25 @@ public function testDropWithAutoincrement(): void self::assertFalse($schemaManager->tablesExist(['test_autoincrement'])); } + public function testDropWithSchema(): void + { + $this->dropTableIfExists('some_schema.test_namespace'); + + $schema = new Schema(); + $table = $schema->createTable('some_schema.test_namespace'); + $table->addColumn('id', Types::INTEGER, ['notnull' => true]); + $table->setPrimaryKey(['id']); + + $schemaManager = $this->connection->createSchemaManager(); + $schemaManager->createSchemaObjects($schema); + self::assertSame(['public', 'some_schema'], $schemaManager->listSchemaNames()); + + $schema = $schemaManager->introspectSchema(); + $schemaManager->dropSchemaObjects($schema); + + self::assertSame([], $schemaManager->listSchemaNames()); + } + public function testListTableDetailsWhenCurrentSchemaNameQuoted(): void { $this->connection->executeStatement('CREATE SCHEMA "001_test"');