Skip to content

Commit

Permalink
Merge pull request #6599 from morozov/postgresql-quoted-drop-pk
Browse files Browse the repository at this point in the history
Fix dropping the PK on a PostgreSQL table with quoted name
  • Loading branch information
morozov authored Nov 14, 2024
2 parents 9a3d08d + 9b77907 commit 727bd6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
Expand Down
17 changes: 13 additions & 4 deletions tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,11 @@ public function testDroppingConstraintsBeforeColumns(): void
self::assertEquals($expectedSql, $sql);
}

public function testDroppingPrimaryKey(): void
/** @param list<string> $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']);

Expand All @@ -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<array{string,list<string>}> */
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
{
Expand Down

0 comments on commit 727bd6b

Please sign in to comment.