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

Deprecate modified foreign keys in TableDiff #6827

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@
private readonly array $modifiedForeignKeys = [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this property be deprecated, BTW?

Copy link
Member Author

@morozov morozov Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to deprecate a promoted property? In #6728, in order to deprecate ForeignKeyConstraint#$options, I had to replace a promoted constructor parameter with a parameter and property.

But in this case, the property is private, nobody can use it outside of the class anyways.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, forget it then. And to answer your question, I thought it was possible to annotate them, but can't check right now.

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 @@
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
Loading