Skip to content

Commit

Permalink
PostgreSQL: Add schema to DROP INDEX query when table not in default …
Browse files Browse the repository at this point in the history
…schema
  • Loading branch information
cristi-contiu committed Feb 12, 2025
1 parent 2b09bad commit 327b807
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,12 @@ public function getDropIndexSQL($index, $table = null)
return $this->getDropConstraintSQL($constraintName, $table);
}

$tableName = $table instanceof Table ? $table->getQuotedName($this) : $table;
if ($tableName !== null && strpos($tableName, '.') !== false) {
[$schema] = explode('.', $tableName);
$index = $schema . '.' . ($index instanceof Index ? $index->getQuotedName($this) : $index);
}

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

Expand Down
49 changes: 49 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,55 @@ public static function autoIncrementTypeMigrations(): iterable
}
}
}

/**
* @param callable(AbstractSchemaManager):Comparator $comparatorFactory
*
* @dataProvider dataDropIndexInAnotherSchema
*/
public function testDropIndexInAnotherSchema(
callable $comparatorFactory,
string $quotedSchemaName,
string $tableName
): void {
$this->connection->executeStatement('CREATE SCHEMA IF NOT EXISTS ' . $quotedSchemaName);

$tableFrom = new Table($tableName);
$tableFrom->addColumn('id', Types::INTEGER);
$tableFrom->addColumn('name', Types::TEXT);
$tableFrom->addUniqueIndex(['name'], 'name_unique_index');
$this->dropAndCreateTable($tableFrom);

$tableTo = clone $tableFrom;
$tableTo->dropIndex('name_unique_index');

$diff = $comparatorFactory($this->schemaManager)->compareTables($tableFrom, $tableTo);
self::assertNotFalse($diff);
self::assertSame(
['DROP INDEX ' . $quotedSchemaName . '.name_unique_index'],
$this->connection->getDatabasePlatform()->getAlterTableSQL($diff),
);

$this->schemaManager->alterTable($diff);
$tableFinal = $this->schemaManager->introspectTable($tableName);
self::assertEmpty($tableFinal->getIndexes());
}

/** @return iterable<mixed[]> */
public static function dataDropIndexInAnotherSchema(): iterable
{
foreach (ComparatorTestUtils::comparatorProvider() as $comparatorArguments) {
foreach (
[
'unqouted names' => ['other_schema', 'other_schema.table'],
'quoted names' => ['"other_schema"', '`other_schema`.`table`'],
'reserved names' => ['"case"', 'case.user'],
] as $testArguments
) {
yield array_merge($comparatorArguments, $testArguments);
}
}
}
}

class MoneyType extends Type
Expand Down
42 changes: 42 additions & 0 deletions tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\DBAL\Types\Types;
use UnexpectedValueException;

use function array_values;
use function sprintf;

/** @extends AbstractPlatformTestCase<PostgreSQLPlatform> */
Expand Down Expand Up @@ -611,6 +612,47 @@ public function testDroppingPrimaryKey(): void
self::assertEquals($expectedSql, $sql);
}

/** @dataProvider dataDroppingIndex **/
public function testDroppingIndex(string $tableName, ?string $indexName, string $expectedSql): void
{
$oldTable = new Table($tableName);
$oldTable->addColumn('mycolumn', 'integer');
$oldTable->addIndex(['mycolumn'], $indexName);
$generatedIndexName = array_values($oldTable->getIndexes())[0]->getName();

$newTable = clone $oldTable;
$newTable->dropIndex($generatedIndexName);

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

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

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

/** @return array<string, array{string, ?string, string}> */
public static function dataDroppingIndex(): iterable
{
return [
'default schema' => ['mytable', 'mytable_idx', 'DROP INDEX mytable_idx'],
'default schema, auto index name' => ['mytable', null, 'DROP INDEX IDX_6B2BD6099BADD926'],
'default schema, reserved index name' => ['mytable', 'select', 'DROP INDEX "select"'],
'default schema, quoted index name' => ['mytable', '`foo`', 'DROP INDEX "foo"'],
'named schema' => ['myschema.mytable', 'myschema_mytable_idx', 'DROP INDEX myschema.myschema_mytable_idx'],
'named schema, auto index name' => ['myschema.mytable', null, 'DROP INDEX myschema.IDX_F3CE2AFE9BADD926'],
'named schema, reserved index name' => ['myschema.mytable', 'select', 'DROP INDEX myschema."select"'],
'named schema, quoted index name' => ['myschema.mytable', '`foo`', 'DROP INDEX myschema."foo"'],
'reserved schema name' => ['case.mytable', 'case_mytable_idx', 'DROP INDEX "case".case_mytable_idx'],
'reserved schema name, auto index name' => ['case.mytable', null, 'DROP INDEX "case".IDX_A472B20C9BADD926'],
'reserved schema name, reserved index name' => ['case.mytable', 'select', 'DROP INDEX "case"."select"'],
'reserved schema name, quoted index name' => ['case.mytable', '`foo`', 'DROP INDEX "case"."foo"'],
'quoted schema name' => ['`bar`.mytable', 'bar_mytable_idx', 'DROP INDEX "bar".bar_mytable_idx'],
'quoted schema name, auto index name' => ['`bar`.mytable', null, 'DROP INDEX "bar".IDX_99F4631B9BADD926'],
'quoted schema name, reserved index name' => ['`bar`.mytable', 'select', 'DROP INDEX "bar"."select"'],
'quoted schema name, quoted index name' => ['`bar`.mytable', '`foo`', 'DROP INDEX "bar"."foo"'],
];
}

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

0 comments on commit 327b807

Please sign in to comment.