From 9b77907d258bfcaeef297edd171f43555eb07f95 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Thu, 14 Nov 2024 09:24:40 -0800 Subject: [PATCH] Fix dropping the PK on a PostgreSQL table with quoted name --- src/Platforms/PostgreSQLPlatform.php | 8 +++++++- tests/Platforms/PostgreSQLPlatformTest.php | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index 0af29f674a9..11c4d1c4924 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -29,7 +29,9 @@ use function is_string; use function sprintf; use function str_contains; +use function str_ends_with; use function strtolower; +use function substr; use function trim; /** @@ -363,7 +365,11 @@ public function getDropForeignKeySQL(string $foreignKey, string $table): string public function getDropIndexSQL(string $name, string $table): string { if ($name === '"primary"') { - $constraintName = $table . '_pkey'; + if (str_ends_with($table, '"')) { + $constraintName = substr($table, 0, -1) . '_pkey"'; + } else { + $constraintName = $table . '_pkey'; + } return $this->getDropConstraintSQL($constraintName, $table); } diff --git a/tests/Platforms/PostgreSQLPlatformTest.php b/tests/Platforms/PostgreSQLPlatformTest.php index e3b78ebb0e7..9a5ebe16183 100644 --- a/tests/Platforms/PostgreSQLPlatformTest.php +++ b/tests/Platforms/PostgreSQLPlatformTest.php @@ -458,9 +458,11 @@ public function testDroppingConstraintsBeforeColumns(): void self::assertEquals($expectedSql, $sql); } - public function testDroppingPrimaryKey(): void + /** @param list $expectedSql */ + #[DataProvider('dropPrimaryKeyProvider')] + public function testDroppingPrimaryKey(string $tableName, array $expectedSql): void { - $oldTable = new Table('mytable'); + $oldTable = new Table($tableName); $oldTable->addColumn('id', 'integer'); $oldTable->setPrimaryKey(['id']); @@ -472,11 +474,18 @@ public function testDroppingPrimaryKey(): void $sql = $this->platform->getAlterTableSQL($diff); - $expectedSql = ['ALTER TABLE mytable DROP CONSTRAINT mytable_pkey']; - self::assertEquals($expectedSql, $sql); } + /** @return iterable}> */ + public static function dropPrimaryKeyProvider(): iterable + { + return [ + ['test', ['ALTER TABLE test DROP CONSTRAINT test_pkey']], + ['"test"', ['ALTER TABLE "test" DROP CONSTRAINT "test_pkey"']], + ]; + } + #[DataProvider('dataCreateSequenceWithCache')] public function testCreateSequenceWithCache(int $cacheSize, string $expectedSql): void {