From 0c08553e6ab6aaac2e2a939380bd47b4b667cb7a Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Wed, 12 Oct 2022 07:32:21 -0700 Subject: [PATCH] Remove handling orphaned foreign keys --- UPGRADE.md | 7 ++-- src/Schema/Comparator.php | 55 ++---------------------------- src/Schema/SchemaDiff.php | 18 ---------- src/Schema/TableDiff.php | 13 +------- tests/Schema/ComparatorTest.php | 59 --------------------------------- 5 files changed, 8 insertions(+), 144 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 112442d6ec6..a8a00cd955c 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,10 @@ awareness about deprecated code. # Upgrade to 4.0 +## Removed `SchemaDiff::$orphanedForeignKeys` + +The functionality of automatically dropping the foreign keys referencing the tables being dropped has been removed. + ## BC Break: Removed registration of user defined functions for SQLite DBAL does not register functions for SQLite anymore. The following functions @@ -571,9 +575,6 @@ Reference from `ForeignKeyConstraint` to its local (referencing) `Table` is remo - `getLocalTable()`, - `getLocalTableName()`. -The type of `SchemaDiff::$orphanedForeignKeys` has changed from list of foreign keys (`list`) -to map of referencing tables to their list of foreign keys (`array>`). - ## BC BREAK: Removed redundant `AbstractPlatform` methods. The following redundant `AbstractPlatform` methods have been removed: diff --git a/src/Schema/Comparator.php b/src/Schema/Comparator.php index e31d38a68e9..81e632b82eb 100644 --- a/src/Schema/Comparator.php +++ b/src/Schema/Comparator.php @@ -36,14 +36,6 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff $alteredSequences = []; $droppedSequences = []; - $orphanedForeignKeys = []; - - /** @var array> $foreignKeysToTable */ - $foreignKeysToTable = []; - - /** @var array> $localTablesByForeignTable */ - $localTablesByForeignTable = []; - foreach ($toSchema->getNamespaces() as $namespace) { if ($fromSchema->hasNamespace($namespace)) { continue; @@ -81,48 +73,11 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff $tableName = $table->getShortestName($fromSchema->getName()); $table = $fromSchema->getTable($tableName); - if (! $toSchema->hasTable($tableName)) { - $droppedTables[$tableName] = $table; - } - - // also remember all foreign keys that point to a specific table - foreach ($table->getForeignKeys() as $foreignKey) { - $foreignTable = strtolower($foreignKey->getForeignTableName()); - - $foreignKeysToTable[$foreignTable][] = [$foreignKey, $table->getName()]; - $localTablesByForeignTable[$foreignTable][$tableName] = null; - } - } - - foreach ($droppedTables as $tableName => $table) { - if (! isset($foreignKeysToTable[$tableName])) { + if ($toSchema->hasTable($tableName)) { continue; } - foreach ($foreignKeysToTable[$tableName] as [$foreignKey, $localTableName]) { - if (isset($droppedTables[strtolower($localTableName)])) { - continue; - } - - $orphanedForeignKeys[$tableName][] = $foreignKey; - } - - // deleting duplicated foreign keys present on both on the orphanedForeignKey - // and the removedForeignKeys from changedTables - foreach ($localTablesByForeignTable[$tableName] as $localTableName => $_) { - if (! isset($alteredTables[$localTableName])) { - continue; - } - - foreach ($alteredTables[$localTableName]->getDroppedForeignKeys() as $droppedForeignKey) { - // We check if the key is from the removed table if not we skip. - if ($tableName !== strtolower($droppedForeignKey->getForeignTableName())) { - continue; - } - - $alteredTables[$localTableName]->unsetDroppedForeignKey($droppedForeignKey); - } - } + $droppedTables[$tableName] = $table; } foreach ($toSchema->getSequences() as $sequence) { @@ -152,7 +107,7 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff $droppedSequences[] = $sequence; } - $diff = new SchemaDiff( + return new SchemaDiff( $createdTables, $alteredTables, $droppedTables, @@ -162,10 +117,6 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff $alteredSequences, $droppedSequences, ); - - $diff->orphanedForeignKeys = $orphanedForeignKeys; - - return $diff; } private function isAutoIncrementSequenceInSchema(Schema $schema, Sequence $sequence): bool diff --git a/src/Schema/SchemaDiff.php b/src/Schema/SchemaDiff.php index cdd4791dad5..75fe15b3bbc 100644 --- a/src/Schema/SchemaDiff.php +++ b/src/Schema/SchemaDiff.php @@ -53,13 +53,6 @@ class SchemaDiff */ public array $removedSequences = []; - /** - * @deprecated - * - * @var array> - */ - public array $orphanedForeignKeys = []; - /** * Constructs an SchemaDiff object. * @@ -175,17 +168,6 @@ protected function _toSql(AbstractPlatform $platform, bool $saveMode = false): a } } - if ($saveMode === false) { - foreach ($this->orphanedForeignKeys as $localTableName => $tableOrphanedForeignKey) { - foreach ($tableOrphanedForeignKey as $orphanedForeignKey) { - $sql[] = $platform->getDropForeignKeySQL( - $orphanedForeignKey->getQuotedName($platform), - $localTableName, - ); - } - } - } - if ($platform->supportsSequences()) { foreach ($this->getAlteredSequences() as $sequence) { $sql[] = $platform->getAlterSequenceSQL($sequence); diff --git a/src/Schema/TableDiff.php b/src/Schema/TableDiff.php index e836562e8ff..a825328986c 100644 --- a/src/Schema/TableDiff.php +++ b/src/Schema/TableDiff.php @@ -40,7 +40,7 @@ public function __construct( private readonly array $renamedIndexes, private readonly array $addedForeignKeys, private readonly array $modifiedForeignKeys, - private array $droppedForeignKeys, + private readonly array $droppedForeignKeys, ) { } @@ -142,15 +142,4 @@ public function getDroppedForeignKeys(): array { return $this->droppedForeignKeys; } - - /** @internal This method exists only for compatibility with the current implementation of the schema comparator. */ - public function unsetDroppedForeignKey(ForeignKeyConstraint $foreignKey): void - { - $this->droppedForeignKeys = array_filter( - $this->droppedForeignKeys, - static function (ForeignKeyConstraint $droppedForeignKey) use ($foreignKey): bool { - return $droppedForeignKey !== $foreignKey; - }, - ); - } } diff --git a/tests/Schema/ComparatorTest.php b/tests/Schema/ComparatorTest.php index 42ccd5b39dc..0d4bdeda39c 100644 --- a/tests/Schema/ComparatorTest.php +++ b/tests/Schema/ComparatorTest.php @@ -619,44 +619,6 @@ public function testAutoIncrementNoSequences(): void self::assertCount(0, $diff->newSequences); } - /** - * You can get multiple drops for a FK when a table referenced by a foreign - * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys - * array because of the dropped table, and once on changedTables array. We - * now check that the key is present once. - */ - public function testAvoidMultipleDropForeignKey(): void - { - $oldSchema = new Schema(); - - $tableA = $oldSchema->createTable('table_a'); - $tableA->addColumn('id', 'integer'); - - $tableB = $oldSchema->createTable('table_b'); - $tableB->addColumn('id', 'integer'); - - $tableC = $oldSchema->createTable('table_c'); - $tableC->addColumn('id', 'integer'); - $tableC->addColumn('table_a_id', 'integer'); - $tableC->addColumn('table_b_id', 'integer'); - - $tableC->addForeignKeyConstraint($tableA->getName(), ['table_a_id'], ['id']); - $tableC->addForeignKeyConstraint($tableB->getName(), ['table_b_id'], ['id']); - - $newSchema = new Schema(); - - $tableB = $newSchema->createTable('table_b'); - $tableB->addColumn('id', 'integer'); - - $tableC = $newSchema->createTable('table_c'); - $tableC->addColumn('id', 'integer'); - - $schemaDiff = $this->comparator->compareSchemas($oldSchema, $newSchema); - - self::assertCount(1, $schemaDiff->changedTables['table_c']->getDroppedForeignKeys()); - self::assertCount(1, $schemaDiff->orphanedForeignKeys); - } - public function assertSchemaTableChangeCount( SchemaDiff $diff, int $newTableCount = 0, @@ -790,8 +752,6 @@ public function testForeignKeyRemovalWithRenamedLocalColumn(): void $schemaDiff = $this->comparator->compareSchemas($fromSchema, $toSchema); self::assertArrayHasKey('table2', $schemaDiff->changedTables); - self::assertCount(1, $schemaDiff->orphanedForeignKeys); - self::assertEquals('fk_table2_table1', $schemaDiff->orphanedForeignKeys['table1'][0]->getName()); $tableDiff = $schemaDiff->changedTables['table2']; @@ -837,25 +797,6 @@ public function testWillNotProduceSchemaDiffOnTableWithAddedCustomSchemaDefiniti ); } - public function testNoOrphanedForeignKeyIfReferencingTableIsDropped(): void - { - $schema1 = new Schema(); - - $parent = $schema1->createTable('parent'); - $parent->addColumn('id', 'integer'); - - $child = $schema1->createTable('child'); - $child->addColumn('id', 'integer'); - $child->addColumn('parent_id', 'integer'); - $child->addForeignKeyConstraint('parent', ['parent_id'], ['id']); - - $schema2 = new Schema(); - - $diff = $this->comparator->compareSchemas($schema1, $schema2); - - self::assertEmpty($diff->orphanedForeignKeys); - } - /** * @param array $assets *