Skip to content

Commit

Permalink
Fix incorrect syntax for dropping primary indexes in PostgreSQL (doct…
Browse files Browse the repository at this point in the history
…rine#6025)

|      Q       |   A
|------------- | -----------
| Type         | bug/feature/improvement
| Fixed issues | doctrine#6024

#### Summary

The correct way of dropping primary index in PostgreSQL is not dropping
the index, but removing the primary key constraints

---------

Co-authored-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
f-lombardo and derrabus authored Jun 15, 2023
1 parent 4de9c19 commit 19f0dec
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\PostgreSQLSchemaManager;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\BinaryType;
use Doctrine\DBAL\Types\BlobType;
Expand Down Expand Up @@ -809,6 +810,36 @@ public function getDropForeignKeySQL($foreignKey, $table)
return $this->getDropConstraintSQL($foreignKey, $table);
}

/**
* {@inheritDoc}
*/
public function getDropIndexSQL($index, $table = null)
{
if ($index instanceof Index && $index->isPrimary() && $table !== null) {
$constraintName = $index->getName() === 'primary' ? $this->tableName($table) . '_pkey' : $index->getName();

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

if ($index === '"primary"' && $table !== null) {
$constraintName = $this->tableName($table) . '_pkey';

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

return parent::getDropIndexSQL($index, $table);
}

/**
* @param Table|string|null $table
*
* @return string
*/
private function tableName($table)
{
return $table instanceof Table ? $table->getName() : (string) $table;
}

/**
* {@inheritDoc}
*/
Expand Down
43 changes: 43 additions & 0 deletions tests/Functional/Driver/DBAL6024Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Doctrine\DBAL\Tests\Functional\Driver;

use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;

class DBAL6024Test extends FunctionalTestCase
{
protected function setUp(): void
{
if (TestUtil::isDriverOneOf('pdo_pgsql', 'pgsql')) {
return;
}

self::markTestSkipped('This test requires the pdo_pgsql or the pgsql driver.');
}

public function testDropPrimaryKey(): void
{
$table = new Table('mytable');
$table->addColumn('id', 'integer');
$table->setPrimaryKey(['id']);
$this->dropAndCreateTable($table);

$newTable = clone $table;
$newTable->dropPrimaryKey();

$schemaManager = $this->connection->createSchemaManager();
$diff = $schemaManager->createComparator()->compareTables($table, $newTable);

$statements = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff);
foreach ($statements as $statement) {
$this->connection->executeStatement($statement);
}

$validationSchema = $schemaManager->introspectSchema();
$validationTable = $validationSchema->getTable($table->getName());

$this->assertNull($validationTable->getPrimaryKey());
}
}
38 changes: 38 additions & 0 deletions tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,44 @@ public function testDroppingConstraintsBeforeColumns(): void
self::assertEquals($expectedSql, $sql);
}

public function testDroppingPrimaryKey(): void
{
$oldTable = new Table('mytable');
$oldTable->addColumn('id', 'integer');
$oldTable->setPrimaryKey(['id']);

$newTable = clone $oldTable;
$newTable->dropPrimaryKey();

$diff = (new Comparator())->compareTables($oldTable, $newTable);

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

$expectedSql = ['ALTER TABLE mytable DROP CONSTRAINT mytable_pkey'];

self::assertEquals($expectedSql, $sql);
}

public function testDroppingPrimaryKeyWithUserDefinedName(): void
{
self::markTestSkipped('Edge case not covered yet');

$oldTable = new Table('mytable');
$oldTable->addColumn('id', 'integer');
$oldTable->setPrimaryKey(['id'], 'a_user_name');

$newTable = clone $oldTable;
$newTable->dropPrimaryKey();

$diff = (new Comparator())->compareTables($oldTable, $newTable);

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

$expectedSql = ['ALTER TABLE mytable DROP CONSTRAINT a_user_name'];

self::assertEquals($expectedSql, $sql);
}

public function testUsesSequenceEmulatedIdentityColumns(): void
{
self::assertTrue($this->platform->usesSequenceEmulatedIdentityColumns());
Expand Down

0 comments on commit 19f0dec

Please sign in to comment.