Skip to content

Commit

Permalink
Adding checks to cope for foreign keys that are defined twice on the …
Browse files Browse the repository at this point in the history
…same set of columns.

We now do deduplication for those.
  • Loading branch information
moufmouf committed May 31, 2017
1 parent 2591fae commit c96bebb
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/SchemaAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ private function buildSchemaGraph()

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

/**
* Remove duplicate foreign keys (assumes that all foreign yes are from the same local table).
*
* @param ForeignKeyConstraint[] $foreignKeys
* @return ForeignKeyConstraint[]
*/
private function removeDuplicates(array $foreignKeys)
{
$fks = [];
foreach ($foreignKeys as $foreignKey) {
$fks[implode('__`__', $foreignKey->getLocalColumns())] = $foreignKey;
}

return array_values($fks);
}

/**
* Returns the schema (from the schema manager or the cache if needed).
*
Expand Down Expand Up @@ -572,7 +589,8 @@ private function getChildrenRelationshipsWithoutCache($tableName)
if ($table->getName() === $tableName) {
continue;
}
foreach ($table->getForeignKeys() as $fk) {
$fks = $this->removeDuplicates($table->getForeignKeys());
foreach ($fks as $fk) {
if ($fk->getForeignTableName() === $tableName && $this->isInheritanceRelationship($fk)) {
$children[] = $fk;
}
Expand Down

0 comments on commit c96bebb

Please sign in to comment.