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 14, 2025
1 parent 2b09bad commit 368e8a2
Show file tree
Hide file tree
Showing 2 changed files with 57 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);

Check warning on line 839 in src/Platforms/PostgreSQLPlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/PostgreSQLPlatform.php#L838-L839

Added lines #L838 - L839 were not covered by tests
}

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

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

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

$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(
[$expectedDropIndexSQL],
$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 $comparatorScenario => $comparatorArguments) {
foreach (
[
'default schema' => ['table', 'DROP INDEX name_unique_index'],
'unquoted schema' => ['other_schema.table', 'DROP INDEX other_schema.name_unique_index'],
'quoted schema' => ['"other_schema".table', 'DROP INDEX "other_schema".name_unique_index'],
'reserved schema' => ['case.table', 'DROP INDEX "case".name_unique_index'],
] as $testScenario => $testArguments
) {
yield $comparatorScenario . ' - ' . $testScenario => array_merge($comparatorArguments, $testArguments);
}
}
}
}

class MoneyType extends Type
Expand Down

0 comments on commit 368e8a2

Please sign in to comment.