Skip to content

Commit 33b752f

Browse files
committed
Remove TableDiff::getDroppedForeignKeys()
1 parent 7d54ef6 commit 33b752f

File tree

9 files changed

+101
-140
lines changed

9 files changed

+101
-140
lines changed

UPGRADE.md

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

99
# Upgrade to 5.0
1010

11+
## BC BREAK: Removed `TableDiff::getDroppedForeignKeys()`
12+
13+
The `TableDiff::getDroppedForeignKeys()` method has been removed.
14+
15+
An attempt to drop an unnamed foreign key constraint on SQLite will result in an exception.
16+
1117
## BC BREAK: Removed `AbstractSchemaManager` methods
1218

1319
The following `AbstractSchemaManager` methods have been removed:

src/Platforms/AbstractPlatform.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,8 +1226,8 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
12261226

12271227
$sql = [];
12281228

1229-
foreach ($diff->getDroppedForeignKeys() as $foreignKey) {
1230-
$sql[] = $this->getDropForeignKeySQL($this->getConstraintName($foreignKey)->toSQL($this), $tableNameSQL);
1229+
foreach ($diff->getDroppedForeignKeyConstraintNames() as $constraintName) {
1230+
$sql[] = $this->getDropForeignKeySQL($constraintName->toSQL($this), $tableNameSQL);
12311231
}
12321232

12331233
foreach ($diff->getDroppedIndexes() as $index) {

src/Platforms/SQLitePlatform.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ private function getSimpleAlterTableSQL(TableDiff $diff): array|false
719719
|| count($diff->getDroppedIndexes()) > 0
720720
|| count($diff->getRenamedIndexes()) > 0
721721
|| count($diff->getAddedForeignKeys()) > 0
722-
|| count($diff->getDroppedForeignKeys()) > 0
722+
|| count($diff->getDroppedForeignKeyConstraintNames()) > 0
723723
|| $diff->getDroppedPrimaryKeyConstraint() !== null
724724
|| $diff->getAddedPrimaryKeyConstraint() !== null
725725
) {
@@ -901,13 +901,7 @@ private function getForeignKeysInAlteredTable(TableDiff $diff): array
901901
->create();
902902
}
903903

904-
foreach ($diff->getDroppedForeignKeys() as $constraint) {
905-
$constraintName = $constraint->getObjectName();
906-
907-
if ($constraintName === null) {
908-
continue;
909-
}
910-
904+
foreach ($diff->getDroppedForeignKeyConstraintNames() as $constraintName) {
911905
$constraintKey = strtolower(
912906
$constraintName->getIdentifier()
913907
->getValue(),

src/Schema/Comparator.php

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

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Schema\Exception\UnspecifiedConstraintName;
89
use Doctrine\DBAL\Schema\Name\UnquotedIdentifierFolding;
910

1011
use function count;
@@ -127,16 +128,16 @@ public function diffSequence(Sequence $sequence1, Sequence $sequence2): bool
127128
*/
128129
public function compareTables(Table $oldTable, Table $newTable): TableDiff
129130
{
130-
$addedColumns = [];
131-
$modifiedColumns = [];
132-
$droppedColumns = [];
133-
$addedIndexes = [];
134-
$droppedIndexes = [];
135-
$renamedIndexes = [];
136-
$addedForeignKeys = [];
137-
$droppedForeignKeys = [];
138-
$addedPrimaryKeyConstraint = null;
139-
$droppedPrimaryKeyConstraint = null;
131+
$addedColumns = [];
132+
$modifiedColumns = [];
133+
$droppedColumns = [];
134+
$addedIndexes = [];
135+
$droppedIndexes = [];
136+
$renamedIndexes = [];
137+
$addedForeignKeys = [];
138+
$droppedForeignKeyConstraintNames = [];
139+
$addedPrimaryKeyConstraint = null;
140+
$droppedPrimaryKeyConstraint = null;
140141

141142
$oldColumns = $oldTable->getColumns();
142143
$newColumns = $newTable->getColumns();
@@ -279,8 +280,8 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
279280
&& strtolower($oldForeignKeyName->getIdentifier()->getValue())
280281
=== strtolower($newForeignKeyName->getIdentifier()->getValue())
281282
) {
282-
$droppedForeignKeys[$oldKey] = $oldForeignKey;
283-
$addedForeignKeys[$newKey] = $newForeignKey;
283+
$droppedForeignKeyConstraintNames[$oldKey] = $oldForeignKeyName;
284+
$addedForeignKeys[$newKey] = $newForeignKey;
284285

285286
unset($oldForeignKeys[$oldKey], $newForeignKeys[$newKey]);
286287
}
@@ -289,7 +290,12 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
289290
}
290291

291292
foreach ($oldForeignKeys as $oldForeignKey) {
292-
$droppedForeignKeys[] = $oldForeignKey;
293+
$constraintName = $oldForeignKey->getObjectName();
294+
if ($constraintName === null) {
295+
throw UnspecifiedConstraintName::forForeignKeyConstraint();
296+
}
297+
298+
$droppedForeignKeyConstraintNames[] = $constraintName;
293299
}
294300

295301
foreach ($newForeignKeys as $newForeignKey) {
@@ -305,7 +311,7 @@ public function compareTables(Table $oldTable, Table $newTable): TableDiff
305311
droppedIndexes: $droppedIndexes,
306312
renamedIndexes: $renamedIndexes,
307313
addedForeignKeys: $addedForeignKeys,
308-
droppedForeignKeys: $droppedForeignKeys,
314+
droppedForeignKeyConstraintNames: $droppedForeignKeyConstraintNames,
309315
addedPrimaryKeyConstraint: $addedPrimaryKeyConstraint,
310316
droppedPrimaryKeyConstraint: $droppedPrimaryKeyConstraint,
311317
);

src/Schema/SQLiteSchemaManager.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,17 @@ public function createForeignKey(ForeignKeyConstraint $foreignKey, string $table
2727

2828
public function dropForeignKey(string $name, string $table): void
2929
{
30-
$table = $this->introspectTableByStringName($table);
30+
$parser = Parsers::getUnqualifiedNameParser();
31+
32+
try {
33+
$parsedName = $parser->parse($name);
34+
} catch (Parser\Exception $e) {
35+
throw InvalidName::fromParserException($name, $e);
36+
}
3137

32-
$foreignKey = $table->getForeignKey($name);
38+
$table = $this->introspectTableByStringName($table);
3339

34-
$this->alterTable(new TableDiff($table, droppedForeignKeys: [$foreignKey]));
40+
$this->alterTable(new TableDiff($table, droppedForeignKeyConstraintNames: [$parsedName]));
3541
}
3642

3743
/** @throws Exception */

src/Schema/TableDiff.php

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Doctrine\DBAL\Schema;
66

7-
use Doctrine\DBAL\Exception\InvalidArgumentException;
8-
use Doctrine\DBAL\Schema\Exception\InvalidState;
97
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
108
use Doctrine\Deprecations\Deprecation;
119

@@ -23,14 +21,14 @@ final class TableDiff
2321
*
2422
* @internal The diff can be only instantiated by a {@see Comparator}.
2523
*
26-
* @param array<ForeignKeyConstraint> $droppedForeignKeys
2724
* @param array<Column> $addedColumns
2825
* @param array<string, ColumnDiff> $changedColumns
2926
* @param array<Column> $droppedColumns
3027
* @param array<Index> $addedIndexes
3128
* @param array<Index> $droppedIndexes
3229
* @param array<string, Index> $renamedIndexes
3330
* @param array<ForeignKeyConstraint> $addedForeignKeys
31+
* @param array<UnqualifiedName> $droppedForeignKeyConstraintNames
3432
*/
3533
public function __construct(
3634
private readonly Table $oldTable,
@@ -41,17 +39,10 @@ public function __construct(
4139
private array $droppedIndexes = [],
4240
private readonly array $renamedIndexes = [],
4341
private readonly array $addedForeignKeys = [],
44-
private readonly array $droppedForeignKeys = [],
42+
private readonly array $droppedForeignKeyConstraintNames = [],
4543
private readonly ?PrimaryKeyConstraint $addedPrimaryKeyConstraint = null,
4644
private readonly ?PrimaryKeyConstraint $droppedPrimaryKeyConstraint = null,
4745
) {
48-
foreach ($droppedForeignKeys as $droppedForeignKey) {
49-
if ($droppedForeignKey->getObjectName() === null) {
50-
throw new InvalidArgumentException(
51-
'Dropping a foreign key constraints without specifying its name is not allowed.',
52-
);
53-
}
54-
}
5546
}
5647

5748
public function getOldTable(): Table
@@ -179,31 +170,10 @@ public function getAddedForeignKeys(): array
179170
return $this->addedForeignKeys;
180171
}
181172

182-
/**
183-
* @deprecated Use {@see getDroppedForeignKeyConstraintNames()}.
184-
*
185-
* @return array<ForeignKeyConstraint>
186-
*/
187-
public function getDroppedForeignKeys(): array
188-
{
189-
return $this->droppedForeignKeys;
190-
}
191-
192173
/** @return array<UnqualifiedName> */
193174
public function getDroppedForeignKeyConstraintNames(): array
194175
{
195-
$names = [];
196-
foreach ($this->droppedForeignKeys as $constraint) {
197-
$name = $constraint->getObjectName();
198-
199-
if ($name === null) {
200-
throw InvalidState::tableDiffContainsUnnamedDroppedForeignKeyConstraints();
201-
}
202-
203-
$names[] = $name;
204-
}
205-
206-
return $names;
176+
return $this->droppedForeignKeyConstraintNames;
207177
}
208178

209179
public function getAddedPrimaryKeyConstraint(): ?PrimaryKeyConstraint
@@ -228,7 +198,7 @@ public function isEmpty(): bool
228198
&& count($this->droppedIndexes) === 0
229199
&& count($this->renamedIndexes) === 0
230200
&& count($this->addedForeignKeys) === 0
231-
&& count($this->droppedForeignKeys) === 0
201+
&& count($this->droppedForeignKeyConstraintNames) === 0
232202
&& $this->addedPrimaryKeyConstraint === null
233203
&& $this->droppedPrimaryKeyConstraint === null;
234204
}

tests/Functional/Schema/ComparatorTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
88
use Doctrine\DBAL\Platforms\MariaDBPlatform;
9+
use Doctrine\DBAL\Platforms\SQLitePlatform;
910
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1011
use Doctrine\DBAL\Schema\Column;
1112
use Doctrine\DBAL\Schema\Comparator;
1213
use Doctrine\DBAL\Schema\ComparatorConfig;
14+
use Doctrine\DBAL\Schema\Exception\UnspecifiedConstraintName;
15+
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1316
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
17+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1418
use Doctrine\DBAL\Schema\Table;
1519
use Doctrine\DBAL\Tests\Functional\Platform\RenameColumnTest;
1620
use Doctrine\DBAL\Tests\FunctionalTestCase;
@@ -151,4 +155,58 @@ public static function defaultValueProvider(): iterable
151155
[Types::TEXT, 'Doctrine'],
152156
];
153157
}
158+
159+
public function testDropUnnamedForeignKeyConstraint(): void
160+
{
161+
$this->dropTableIfExists('tree');
162+
163+
$table1 = Table::editor()
164+
->setUnquotedName('tree')
165+
->setColumns(
166+
Column::editor()
167+
->setUnquotedName('id')
168+
->setTypeName(Types::INTEGER)
169+
->create(),
170+
Column::editor()
171+
->setUnquotedName('parent_id')
172+
->setTypeName(Types::INTEGER)
173+
->create(),
174+
)
175+
->setPrimaryKeyConstraint(
176+
PrimaryKeyConstraint::editor()
177+
->setUnquotedColumnNames('id')
178+
->create(),
179+
)
180+
->setForeignKeyConstraints(
181+
ForeignKeyConstraint::editor()
182+
->setUnquotedReferencingColumnNames('parent_id')
183+
->setUnquotedReferencedTableName('tree')
184+
->setUnquotedReferencedColumnNames('id')
185+
->create(),
186+
)
187+
->create();
188+
189+
$this->schemaManager->createTable($table1);
190+
191+
$table1 = $this->schemaManager->introspectTableByUnquotedName('tree');
192+
193+
$table2 = $table1->edit()
194+
->setForeignKeyConstraints()
195+
->create();
196+
197+
$comparator = $this->schemaManager->createComparator();
198+
199+
// SQLite does not automatically generate names for unnamed constraints
200+
if ($this->connection->getDatabasePlatform() instanceof SQLitePlatform) {
201+
$this->expectException(UnspecifiedConstraintName::class);
202+
}
203+
204+
$diff = $comparator->compareTables($table1, $table2);
205+
206+
$this->schemaManager->alterTable($diff);
207+
208+
$table2 = $this->schemaManager->introspectTableByUnquotedName('tree');
209+
210+
self::assertEmpty($table2->getForeignKeys());
211+
}
154212
}

tests/Schema/AbstractComparatorTestCase.php

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -361,41 +361,6 @@ public function testTableAddForeignKey(): void
361361
self::assertCount(1, $tableDiff->getAddedForeignKeys());
362362
}
363363

364-
public function testTableDropForeignKey(): void
365-
{
366-
$table1 = Table::editor()
367-
->setUnquotedName('foo')
368-
->setColumns(
369-
Column::editor()
370-
->setUnquotedName('fk')
371-
->setTypeName(Types::INTEGER)
372-
->create(),
373-
)
374-
->create();
375-
376-
$table2 = Table::editor()
377-
->setUnquotedName('foo')
378-
->setColumns(
379-
Column::editor()
380-
->setUnquotedName('fk')
381-
->setTypeName(Types::INTEGER)
382-
->create(),
383-
)
384-
->setForeignKeyConstraints(
385-
ForeignKeyConstraint::editor()
386-
->setUnquotedName('fk_foo')
387-
->setUnquotedReferencingColumnNames('fk')
388-
->setUnquotedReferencedTableName('bar')
389-
->setUnquotedReferencedColumnNames('id')
390-
->create(),
391-
)
392-
->create();
393-
394-
$tableDiff = $this->comparator->compareTables($table2, $table1);
395-
396-
self::assertCount(1, $tableDiff->getDroppedForeignKeyConstraintNames());
397-
}
398-
399364
public function testTableUpdateForeignKey(): void
400365
{
401366
$table1 = Table::editor()

tests/Schema/TableDiffTest.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)