Skip to content

Commit 09ee3b1

Browse files
authored
Merge pull request #7166 from morozov/remove-dropped-foreign-key-constraints
Remove TableDiff::getDroppedForeignKeys()
2 parents 8549925 + 33b752f commit 09ee3b1

14 files changed

+119
-158
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: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,14 @@ public function getDropTablesSQL(array $tables): array
947947

948948
foreach ($tables as $table) {
949949
foreach ($table->getForeignKeys() as $foreignKey) {
950+
$constraintName = $foreignKey->getObjectName();
951+
952+
if ($constraintName === null) {
953+
throw UnspecifiedConstraintName::forForeignKeyConstraint();
954+
}
955+
950956
$sql[] = $this->getDropForeignKeySQL(
951-
$this->getConstraintName($foreignKey)->toSQL($this),
957+
$constraintName->toSQL($this),
952958
$table->getObjectName()->toSQL($this),
953959
);
954960
}
@@ -1220,8 +1226,8 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff): array
12201226

12211227
$sql = [];
12221228

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

12271233
foreach ($diff->getDroppedIndexes() as $index) {
@@ -2210,15 +2216,4 @@ abstract public function createMetadataProvider(Connection $connection): Metadat
22102216
* database schema according to the dialect of the platform.
22112217
*/
22122218
abstract public function createSchemaManager(Connection $connection): AbstractSchemaManager;
2213-
2214-
private function getConstraintName(ForeignKeyConstraint $constraint): UnqualifiedName
2215-
{
2216-
$name = $constraint->getObjectName();
2217-
2218-
if ($name === null) {
2219-
throw UnspecifiedConstraintName::new();
2220-
}
2221-
2222-
return $name;
2223-
}
22242219
}

src/Platforms/DB2Platform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function getAlterTableSQL(TableDiff $diff): array
273273
$constraintName = $droppedPrimaryKeyConstraint->getObjectName();
274274

275275
if ($constraintName === null) {
276-
throw UnspecifiedConstraintName::new();
276+
throw UnspecifiedConstraintName::forPrimaryKeyConstraint();
277277
}
278278

279279
$sql[] = $this->getDropConstraintSQL($constraintName->toSQL($this), $tableNameSQL);

src/Platforms/OraclePlatform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ public function getAlterTableSQL(TableDiff $diff): array
510510
$constraintName = $droppedPrimaryKeyConstraint->getObjectName();
511511

512512
if ($constraintName === null) {
513-
throw UnspecifiedConstraintName::new();
513+
throw UnspecifiedConstraintName::forPrimaryKeyConstraint();
514514
}
515515

516516
$sql[] = $this->getDropConstraintSQL($constraintName->toSQL($this), $tableNameSQL);

src/Platforms/PostgreSQLPlatform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function getAlterTableSQL(TableDiff $diff): array
185185
$constraintName = $droppedPrimaryKeyConstraint->getObjectName();
186186

187187
if ($constraintName === null) {
188-
throw UnspecifiedConstraintName::new();
188+
throw UnspecifiedConstraintName::forPrimaryKeyConstraint();
189189
}
190190

191191
$sql[] = $this->getDropConstraintSQL($constraintName->toSQL($this), $tableNameSQL);

src/Platforms/SQLServerPlatform.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public function getAlterTableSQL(TableDiff $diff): array
321321
$constraintName = $droppedPrimaryKeyConstraint->getObjectName();
322322

323323
if ($constraintName === null) {
324-
throw UnspecifiedConstraintName::new();
324+
throw UnspecifiedConstraintName::forPrimaryKeyConstraint();
325325
}
326326

327327
$sql[] = $this->getDropConstraintSQL($constraintName->toSQL($this), $table->getObjectName()->toSQL($this));

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/Exception/UnspecifiedConstraintName.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
final class UnspecifiedConstraintName extends InvalidArgumentException implements SchemaException
1111
{
12-
public static function new(): self
12+
public static function forPrimaryKeyConstraint(): self
1313
{
14-
return new self('Constraint name is not specified.');
14+
return new self('Primary key constraint name is not specified.');
15+
}
16+
17+
public static function forForeignKeyConstraint(): self
18+
{
19+
return new self('Foreign key constraint name is not specified.');
1520
}
1621
}

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 */

0 commit comments

Comments
 (0)