Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect syntax for dropping primary indexes in PostgreSQL #6025

Merged
merged 17 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,22 @@ public function getDropForeignKeySQL($foreignKey, $table)
return $this->getDropConstraintSQL($foreignKey, $table);
}

/**
* {@inheritDoc}
*/
public function getDropIndexSQL($index, $table = null)
{
if ($index instanceof Index && $index->isPrimary()) {
$constraintName = 'primary' === $index->getName() ? $table . '_pkey' : $index->getName();
return $this->getDropConstraintSQL($constraintName, $table);
} elseif (is_string($index) && '"primary"' === $index) {
derrabus marked this conversation as resolved.
Show resolved Hide resolved
$constraintName = $table . '_pkey';
return $this->getDropConstraintSQL($constraintName, $table);
}

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

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

namespace Doctrine\DBAL\Tests\Functional\Driver\PDO\PgSQL;

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

/**
* How to run this test:
* 1) Start a PostgreSQLInstance
* 2) If needed, change ci/github/phpunit/pdo_pgsql.xml according to your PostgreSQL local settings
* 3) Run
* vendor/bin/phpunit -c ci/github/phpunit/pdo_pgsql.xml tests/Functional/Driver/PDO/PgSQL/DBAL6024Test.php
*/

/** @requires extension pdo_pgsql */
f-lombardo marked this conversation as resolved.
Show resolved Hide resolved
class DBAL6024Test extends FunctionalTestCase
{
protected function setUp(): void
{
if (TestUtil::isDriverOneOf('pdo_pgsql')) {
f-lombardo marked this conversation as resolved.
Show resolved Hide resolved
return;
}

self::markTestSkipped('This test requires the pdo_pgsql driver.');
derrabus marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

xdebug_break();
derrabus marked this conversation as resolved.
Show resolved Hide resolved

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

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

$this->assertTrue($this->count($statements) > 0);
derrabus marked this conversation as resolved.
Show resolved Hide resolved
}
}
42 changes: 42 additions & 0 deletions tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,48 @@ 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);
self::assertNotFalse($diff);

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

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

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

public function testDroppingPrimaryKeyWithUserDefinedName(): void
{
$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);
self::assertNotFalse($diff);

$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