Skip to content

Commit c96bebb

Browse files
committed
Adding checks to cope for foreign keys that are defined twice on the same set of columns.
We now do deduplication for those.
1 parent 2591fae commit c96bebb

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/SchemaAnalyzer.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ private function buildSchemaGraph()
302302

303303
// Then, let's create all the edges
304304
foreach ($this->getSchema()->getTables() as $table) {
305-
foreach ($table->getForeignKeys() as $fk) {
305+
$fks = $this->removeDuplicates($table->getForeignKeys());
306+
foreach ($fks as $fk) {
306307
// Create an undirected edge, with weight = 1
307308
$edge = $graph->getVertex($table->getName())->createEdge($graph->getVertex($fk->getForeignTableName()));
308309
if (isset($this->alteredCosts[$fk->getLocalTable()->getName()][implode(',', $fk->getLocalColumns())])) {
@@ -340,6 +341,22 @@ private function buildSchemaGraph()
340341
return $graph;
341342
}
342343

344+
/**
345+
* Remove duplicate foreign keys (assumes that all foreign yes are from the same local table).
346+
*
347+
* @param ForeignKeyConstraint[] $foreignKeys
348+
* @return ForeignKeyConstraint[]
349+
*/
350+
private function removeDuplicates(array $foreignKeys)
351+
{
352+
$fks = [];
353+
foreach ($foreignKeys as $foreignKey) {
354+
$fks[implode('__`__', $foreignKey->getLocalColumns())] = $foreignKey;
355+
}
356+
357+
return array_values($fks);
358+
}
359+
343360
/**
344361
* Returns the schema (from the schema manager or the cache if needed).
345362
*
@@ -572,7 +589,8 @@ private function getChildrenRelationshipsWithoutCache($tableName)
572589
if ($table->getName() === $tableName) {
573590
continue;
574591
}
575-
foreach ($table->getForeignKeys() as $fk) {
592+
$fks = $this->removeDuplicates($table->getForeignKeys());
593+
foreach ($fks as $fk) {
576594
if ($fk->getForeignTableName() === $tableName && $this->isInheritanceRelationship($fk)) {
577595
$children[] = $fk;
578596
}

0 commit comments

Comments
 (0)