Skip to content

Commit

Permalink
Deprecate modified foreign keys in TableDiff
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Mar 9, 2025
1 parent b2ac865 commit d965128
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 16 deletions.
9 changes: 9 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ awareness about deprecated code.

# Upgrade to 4.3

## Deprecated handling of modified foreign keys in `TableDiff`

Passing a non-empty `$modifiedForeignKeys` value to the `TableDiff` constructor is deprecated. Instead, pass dropped
constraints via `$droppedForeignKeys` and added constraints via `$addedForeignKeys`.

The `TableDiff::getModifiedForeignKeys()` method has been deprecated. The old version of the foreign key constraint is
included in the return value of `TableDiff::getDroppedForeignKeys()`, the new version is included in the return value of
`TableDiff::getModifiedForeignKeys()`.

## Deprecated not passing `$options` to `AbstractPlatform::_getCreateTableSQL()`

Not passing the `$options` argument or any of its following keys to the `AbstractPlatform::_getCreateTableSQL()` method
Expand Down
23 changes: 11 additions & 12 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,15 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2): bool
*/
public function compareTables(Table $oldTable, Table $newTable): TableDiff
{
$addedColumns = [];
$modifiedColumns = [];
$droppedColumns = [];
$addedIndexes = [];
$modifiedIndexes = [];
$droppedIndexes = [];
$renamedIndexes = [];
$addedForeignKeys = [];
$modifiedForeignKeys = [];
$droppedForeignKeys = [];
$addedColumns = [];
$modifiedColumns = [];
$droppedColumns = [];
$addedIndexes = [];
$modifiedIndexes = [];
$droppedIndexes = [];
$renamedIndexes = [];
$addedForeignKeys = [];
$droppedForeignKeys = [];

$oldColumns = $oldTable->getColumns();
$newColumns = $newTable->getColumns();
Expand Down Expand Up @@ -262,7 +261,8 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]);
} else {
if (strtolower($oldForeignKey->getName()) === strtolower($newForeignKey->getName())) {
$modifiedForeignKeys[] = $newForeignKey;
$droppedForeignKeys[$oldKey] = $oldForeignKey;
$addedForeignKeys[$newKey] = $newForeignKey;

unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]);
}
Expand All @@ -288,7 +288,6 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
droppedIndexes: $droppedIndexes,
renamedIndexes: $renamedIndexes,
addedForeignKeys: $addedForeignKeys,
modifiedForeignKeys: $modifiedForeignKeys,
droppedForeignKeys: $droppedForeignKeys,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/SQLiteSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function createForeignKey(ForeignKeyConstraint $foreignKey, string $table
{
$table = $this->introspectTable($table);

$this->alterTable(new TableDiff($table, modifiedForeignKeys: [$foreignKey]));
$this->alterTable(new TableDiff($table, addedForeignKeys: [$foreignKey]));
}

public function dropForeignKey(string $name, string $table): void
Expand Down
24 changes: 23 additions & 1 deletion src/Schema/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public function __construct(
private readonly array $modifiedForeignKeys = [],
private readonly array $droppedForeignKeys = [],
) {
if (count($modifiedForeignKeys) === 0) {
return;
}

Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6827',
'Passing a non-empty $modifiedForeignKeys value to %s() is deprecated. Instead, pass dropped'
. ' constraints via $droppedForeignKeys and added constraints via $addedForeignKeys.',
__METHOD__,
);

Check warning on line 57 in src/Schema/TableDiff.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/TableDiff.php#L51-L57

Added lines #L51 - L57 were not covered by tests
}

public function getOldTable(): Table
Expand Down Expand Up @@ -173,9 +184,20 @@ public function getAddedForeignKeys(): array
return $this->addedForeignKeys;
}

/** @return array<ForeignKeyConstraint> */
/**
* @deprecated Use {@see getAddedForeignKeys()} and {@see getDroppedForeignKeys()} instead.
*
* @return array<ForeignKeyConstraint>
*/
public function getModifiedForeignKeys(): array
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6827',
'%s() is deprecated, use getDroppedForeignKeys() and getAddedForeignKeys() instead.',
__METHOD__,
);

return $this->modifiedForeignKeys;
}

Expand Down
6 changes: 4 additions & 2 deletions tests/Schema/AbstractComparatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ public function testTableUpdateForeignKey(): void

$tableDiff = $this->comparator->compareTables($table1, $table2);

self::assertCount(1, $tableDiff->getModifiedForeignKeys());
self::assertCount(1, $tableDiff->getDroppedForeignKeys());
self::assertCount(1, $tableDiff->getAddedForeignKeys());
}

public function testMovedForeignKeyForeignTable(): void
Expand All @@ -300,7 +301,8 @@ public function testMovedForeignKeyForeignTable(): void

$tableDiff = $this->comparator->compareTables($table1, $table2);

self::assertCount(1, $tableDiff->getModifiedForeignKeys());
self::assertCount(1, $tableDiff->getDroppedForeignKeys());
self::assertCount(1, $tableDiff->getAddedForeignKeys());
}

public function testTablesCaseInsensitive(): void
Expand Down

0 comments on commit d965128

Please sign in to comment.