From 1944fffdf8d6136cfed4dbc603f3c3726424343a 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 | 25 +++++++++++++++++++ .../Schema/PostgreSQLSchemaManagerTest.php | 19 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/SQL/Builder/DropSchemaObjectsSQLBuilder.php b/src/SQL/Builder/DropSchemaObjectsSQLBuilder.php index c0384897fa2..aa51e979c84 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,28 @@ 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) { + if ($namespace === 'public') { + continue; + } + + $statements[] = $this->platform->getDropSchemaSQL($namespace); + } + + return $statements; + } } diff --git a/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php b/tests/Functional/Schema/PostgreSQLSchemaManagerTest.php index 2480a472163..3fa69d6341a 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(['public'], $schemaManager->listSchemaNames()); + } + public function testListTableDetailsWhenCurrentSchemaNameQuoted(): void { $this->connection->executeStatement('CREATE SCHEMA "001_test"');