Skip to content

Commit 727bd6b

Browse files
authored
Merge pull request #6599 from morozov/postgresql-quoted-drop-pk
Fix dropping the PK on a PostgreSQL table with quoted name
2 parents 9a3d08d + 9b77907 commit 727bd6b

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/Platforms/PostgreSQLPlatform.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
use function is_string;
3030
use function sprintf;
3131
use function str_contains;
32+
use function str_ends_with;
3233
use function strtolower;
34+
use function substr;
3335
use function trim;
3436

3537
/**
@@ -363,7 +365,11 @@ public function getDropForeignKeySQL(string $foreignKey, string $table): string
363365
public function getDropIndexSQL(string $name, string $table): string
364366
{
365367
if ($name === '"primary"') {
366-
$constraintName = $table . '_pkey';
368+
if (str_ends_with($table, '"')) {
369+
$constraintName = substr($table, 0, -1) . '_pkey"';
370+
} else {
371+
$constraintName = $table . '_pkey';
372+
}
367373

368374
return $this->getDropConstraintSQL($constraintName, $table);
369375
}

tests/Platforms/PostgreSQLPlatformTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,11 @@ public function testDroppingConstraintsBeforeColumns(): void
458458
self::assertEquals($expectedSql, $sql);
459459
}
460460

461-
public function testDroppingPrimaryKey(): void
461+
/** @param list<string> $expectedSql */
462+
#[DataProvider('dropPrimaryKeyProvider')]
463+
public function testDroppingPrimaryKey(string $tableName, array $expectedSql): void
462464
{
463-
$oldTable = new Table('mytable');
465+
$oldTable = new Table($tableName);
464466
$oldTable->addColumn('id', 'integer');
465467
$oldTable->setPrimaryKey(['id']);
466468

@@ -472,11 +474,18 @@ public function testDroppingPrimaryKey(): void
472474

473475
$sql = $this->platform->getAlterTableSQL($diff);
474476

475-
$expectedSql = ['ALTER TABLE mytable DROP CONSTRAINT mytable_pkey'];
476-
477477
self::assertEquals($expectedSql, $sql);
478478
}
479479

480+
/** @return iterable<array{string,list<string>}> */
481+
public static function dropPrimaryKeyProvider(): iterable
482+
{
483+
return [
484+
['test', ['ALTER TABLE test DROP CONSTRAINT test_pkey']],
485+
['"test"', ['ALTER TABLE "test" DROP CONSTRAINT "test_pkey"']],
486+
];
487+
}
488+
480489
#[DataProvider('dataCreateSequenceWithCache')]
481490
public function testCreateSequenceWithCache(int $cacheSize, string $expectedSql): void
482491
{

0 commit comments

Comments
 (0)