Skip to content

Commit

Permalink
Remove handling orphaned foreign keys
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Oct 15, 2022
1 parent 73fa2c8 commit 9d49104
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 58 deletions.
7 changes: 4 additions & 3 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ForeignKeyConstraint>`)
to map of referencing tables to their list of foreign keys (`array<string,list<ForeignKeyConstraint>>`).

## BC BREAK: Removed redundant `AbstractPlatform` methods.

The following redundant `AbstractPlatform` methods have been removed:
Expand Down
16 changes: 1 addition & 15 deletions src/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff
$alteredSequences = [];
$droppedSequences = [];

$orphanedForeignKeys = [];

/** @var array<string,list<array{ForeignKeyConstraint,string}>> $foreignKeysToTable */
$foreignKeysToTable = [];

Expand Down Expand Up @@ -99,14 +97,6 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff
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 => $_) {
Expand Down Expand Up @@ -152,7 +142,7 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff
$droppedSequences[] = $sequence;
}

$diff = new SchemaDiff(
return new SchemaDiff(
$createdTables,
$alteredTables,
$droppedTables,
Expand All @@ -162,10 +152,6 @@ public function compareSchemas(Schema $fromSchema, Schema $toSchema): SchemaDiff
$alteredSequences,
$droppedSequences,
);

$diff->orphanedForeignKeys = $orphanedForeignKeys;

return $diff;
}

private function isAutoIncrementSequenceInSchema(Schema $schema, Sequence $sequence): bool
Expand Down
18 changes: 0 additions & 18 deletions src/Schema/SchemaDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ class SchemaDiff
*/
public array $removedSequences = [];

/**
* @deprecated
*
* @var array<string,list<ForeignKeyConstraint>>
*/
public array $orphanedForeignKeys = [];

/**
* Constructs an SchemaDiff object.
*
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 0 additions & 22 deletions tests/Schema/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,6 @@ public function testAvoidMultipleDropForeignKey(): void
$schemaDiff = $this->comparator->compareSchemas($oldSchema, $newSchema);

self::assertCount(1, $schemaDiff->changedTables['table_c']->getDroppedForeignKeys());
self::assertCount(1, $schemaDiff->orphanedForeignKeys);
}

public function assertSchemaTableChangeCount(
Expand Down Expand Up @@ -790,8 +789,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'];

Expand Down Expand Up @@ -837,25 +834,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<AbstractAsset> $assets
*
Expand Down

0 comments on commit 9d49104

Please sign in to comment.