Skip to content

Commit 729b0a1

Browse files
committed
Deprecate TableDiff::getDroppedForeignKeys()
1 parent 1eccb57 commit 729b0a1

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

UPGRADE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ awareness about deprecated code.
88

99
# Upgrade to 4.4
1010

11+
## Deprecated `TableDiff::getDroppedForeignKeys()`
12+
13+
The `TableDiff::getDroppedForeignKeys()` method has been deprecated. Use
14+
`TableDiff::getDroppedForeignKeyConstraintNames()` instead.
15+
1116
## Deprecated `AbstractSchemaManager` methods
1217

1318
The following `AbstractSchemaManager` methods have been deprecated:

src/Schema/Exception/InvalidState.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,9 @@ public static function tableHasInvalidPrimaryKeyConstraint(string $tableName): s
8989
{
9090
return new self(sprintf('Table "%s" has invalid primary key constraint.', $tableName));
9191
}
92+
93+
public static function tableDiffContainsUnnamedDroppedForeignKeyConstraints(): self
94+
{
95+
return new self('Table diff contains unnamed dropped foreign key constraints');
96+
}
9297
}

src/Schema/TableDiff.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Doctrine\DBAL\Schema;
66

7+
use Doctrine\DBAL\Schema\Exception\InvalidState;
8+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
79
use Doctrine\Deprecations\Deprecation;
810

911
use function array_filter;
@@ -235,12 +237,33 @@ public function getModifiedForeignKeys(): array
235237
return $this->modifiedForeignKeys;
236238
}
237239

238-
/** @return array<ForeignKeyConstraint> */
240+
/**
241+
* @deprecated Use {@see getDroppedForeignKeyConstraintNames()}.
242+
*
243+
* @return array<ForeignKeyConstraint>
244+
*/
239245
public function getDroppedForeignKeys(): array
240246
{
241247
return $this->droppedForeignKeys;
242248
}
243249

250+
/** @return array<UnqualifiedName> */
251+
public function getDroppedForeignKeyConstraintNames(): array
252+
{
253+
$names = [];
254+
foreach ($this->droppedForeignKeys as $constraint) {
255+
$name = $constraint->getObjectName();
256+
257+
if ($name === null) {
258+
throw InvalidState::tableDiffContainsUnnamedDroppedForeignKeyConstraints();
259+
}
260+
261+
$names[] = $name;
262+
}
263+
264+
return $names;
265+
}
266+
244267
/**
245268
* Returns whether the diff is empty (contains no changes).
246269
*/

tests/Schema/AbstractComparatorTestCase.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,6 @@ public function testTableAddForeignKey(): void
366366

367367
public function testTableRemoveForeignKey(): void
368368
{
369-
$tableForeign = Table::editor()
370-
->setUnquotedName('bar')
371-
->setColumns(
372-
Column::editor()
373-
->setUnquotedName('id')
374-
->setTypeName(Types::INTEGER)
375-
->create(),
376-
)
377-
->create();
378-
379369
$table1 = Table::editor()
380370
->setUnquotedName('foo')
381371
->setColumns(
@@ -396,6 +386,7 @@ public function testTableRemoveForeignKey(): void
396386
)
397387
->setForeignKeyConstraints(
398388
ForeignKeyConstraint::editor()
389+
->setUnquotedName('fk_bar')
399390
->setUnquotedReferencingColumnNames('fk')
400391
->setUnquotedReferencedTableName('bar')
401392
->setUnquotedReferencedColumnNames('id')
@@ -405,7 +396,7 @@ public function testTableRemoveForeignKey(): void
405396

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

408-
self::assertCount(1, $tableDiff->getDroppedForeignKeys());
399+
self::assertCount(1, $tableDiff->getDroppedForeignKeyConstraintNames());
409400
}
410401

411402
public function testTableUpdateForeignKey(): void
@@ -420,6 +411,7 @@ public function testTableUpdateForeignKey(): void
420411
)
421412
->setForeignKeyConstraints(
422413
ForeignKeyConstraint::editor()
414+
->setUnquotedName('fk_bar')
423415
->setUnquotedReferencingColumnNames('fk')
424416
->setUnquotedReferencedTableName('bar')
425417
->setUnquotedReferencedColumnNames('id')
@@ -447,7 +439,7 @@ public function testTableUpdateForeignKey(): void
447439

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

450-
self::assertCount(1, $tableDiff->getDroppedForeignKeys());
442+
self::assertCount(1, $tableDiff->getDroppedForeignKeyConstraintNames());
451443
self::assertCount(1, $tableDiff->getAddedForeignKeys());
452444
}
453445

@@ -463,6 +455,7 @@ public function testMovedForeignKeyForeignTable(): void
463455
)
464456
->setForeignKeyConstraints(
465457
ForeignKeyConstraint::editor()
458+
->setUnquotedName('fk_bar')
466459
->setUnquotedReferencingColumnNames('fk')
467460
->setUnquotedReferencedTableName('bar')
468461
->setUnquotedReferencedColumnNames('id')
@@ -489,7 +482,7 @@ public function testMovedForeignKeyForeignTable(): void
489482

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

492-
self::assertCount(1, $tableDiff->getDroppedForeignKeys());
485+
self::assertCount(1, $tableDiff->getDroppedForeignKeyConstraintNames());
493486
self::assertCount(1, $tableDiff->getAddedForeignKeys());
494487
}
495488

tests/Schema/TableDiffTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\DBAL\Tests\Schema;
66

77
use Doctrine\DBAL\Schema\Column;
8+
use Doctrine\DBAL\Schema\Exception\InvalidState;
89
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
910
use Doctrine\DBAL\Schema\Table;
1011
use Doctrine\DBAL\Schema\TableDiff;
@@ -35,6 +36,9 @@ public function testCreateWithInvalidDroppedForeignKeyName(): void
3536
->create();
3637

3738
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/7143');
38-
new TableDiff($table, droppedForeignKeys: [$droppedForeignKeys]);
39+
$diff = new TableDiff($table, droppedForeignKeys: [$droppedForeignKeys]);
40+
41+
$this->expectException(InvalidState::class);
42+
$diff->getDroppedForeignKeyConstraintNames();
3943
}
4044
}

0 commit comments

Comments
 (0)