Skip to content

Commit

Permalink
Fix PostgreSQL re-adding of foreign keys when referenced table is a r…
Browse files Browse the repository at this point in the history
…eserved word and in a custom schema
  • Loading branch information
cristi-contiu committed Feb 14, 2025
1 parent 2b09bad commit ad1b24a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Schema/ForeignKeyConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ public function getUnqualifiedForeignTableName()
$name = substr($name, $position + 1);
}

if ($this->isIdentifierQuoted($name)) {
$name = $this->trimQuotes($name);
}

return strtolower($name);
}

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

/**
* @param callable(AbstractSchemaManager):Comparator $comparatorFactory
*
* @dataProvider dataEmptyDiffRegardlessOfForeignTableQuotes
*/
public function testEmptyDiffRegardlessOfForeignTableQuotes(
callable $comparatorFactory,
string $foreignTableName
): void {
$this->connection->executeStatement('DROP SCHEMA IF EXISTS other_schema CASCADE');
$this->connection->executeStatement('CREATE SCHEMA other_schema');

$tableForeign = new Table($foreignTableName);
$tableForeign->addColumn('id', 'integer');
$tableForeign->setPrimaryKey(['id']);
$this->dropAndCreateTable($tableForeign);

$tableTo = new Table('other_schema.other_table');
$tableTo->addColumn('id', 'integer');
$tableTo->addColumn('user_id', 'integer');
$tableTo->setPrimaryKey(['id']);
$tableTo->addForeignKeyConstraint($tableForeign, ['user_id'], ['id']);
$this->dropAndCreateTable($tableTo);

$schemaFrom = $this->schemaManager->introspectSchema();
$tableFrom = $schemaFrom->getTable('other_schema.other_table');

$diff = $comparatorFactory($this->schemaManager)->compareTables($tableFrom, $tableTo);
self::assertTrue($diff->isEmpty());
}

/** @return iterable<mixed[]> */
public static function dataEmptyDiffRegardlessOfForeignTableQuotes(): iterable
{
foreach (ComparatorTestUtils::comparatorProvider() as $comparatorArguments) {
foreach (
[
'unquoted' => ['other_schema.user'],
'partially quoted' => ['other_schema."user"'],
'fully quoted' => ['"other_schema"."user"'],
] as $testArguments
) {
yield array_merge($comparatorArguments, $testArguments);
}
}
}
}

class MoneyType extends Type
Expand Down
4 changes: 4 additions & 0 deletions tests/Schema/ForeignKeyConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ public static function getUnqualifiedForeignTableNameData(): iterable
{
return [
['schema.foreign_table', 'foreign_table'],
['schema."foreign_table"', 'foreign_table'],
['"schema"."foreign_table"', 'foreign_table'],
['foreign_table', 'foreign_table'],
[new Table('schema.foreign_table'), 'foreign_table'],
[new Table('schema."foreign_table"'), 'foreign_table'],
[new Table('"schema"."foreign_table"'), 'foreign_table'],
[new Table('foreign_table'), 'foreign_table'],
];
}
Expand Down

0 comments on commit ad1b24a

Please sign in to comment.