Esempio n. 1
0
 /**
  * Returns the number of differences.
  *
  * Compare the foreign keys of the fromTable and the toTable,
  * and modifies the inner tableDiff if necessary.
  *
  * @param  boolean $caseInsensitive
  * @return integer
  */
 public function compareForeignKeys($caseInsensitive = false)
 {
     $fkDifferences = 0;
     $fromTableFks = $this->getFromTable()->getForeignKeys();
     $toTableFks = $this->getToTable()->getForeignKeys();
     foreach ($fromTableFks as $fromTableFkPos => $fromTableFk) {
         foreach ($toTableFks as $toTableFkPos => $toTableFk) {
             $sameName = $caseInsensitive ? strtolower($fromTableFk->getName()) == strtolower($toTableFk->getName()) : $fromTableFk->getName() == $toTableFk->getName();
             if ($sameName && !$toTableFk->isPolymorphic()) {
                 if (false === ForeignKeyComparator::computeDiff($fromTableFk, $toTableFk, $caseInsensitive)) {
                     unset($fromTableFks[$fromTableFkPos]);
                     unset($toTableFks[$toTableFkPos]);
                 } else {
                     // same name, but different columns
                     $this->tableDiff->addModifiedFk($fromTableFk->getName(), $fromTableFk, $toTableFk);
                     unset($fromTableFks[$fromTableFkPos]);
                     unset($toTableFks[$toTableFkPos]);
                     $fkDifferences++;
                 }
             }
         }
     }
     foreach ($fromTableFks as $fromTableFk) {
         if (!$fromTableFk->isSkipSql() && !$fromTableFk->isPolymorphic() && !in_array($fromTableFk, $toTableFks)) {
             $this->tableDiff->addRemovedFk($fromTableFk->getName(), $fromTableFk);
             $fkDifferences++;
         }
     }
     foreach ($toTableFks as $toTableFk) {
         if (!$toTableFk->isSkipSql() && !$toTableFk->isPolymorphic() && !in_array($toTableFk, $fromTableFks)) {
             $this->tableDiff->addAddedFk($toTableFk->getName(), $toTableFk);
             $fkDifferences++;
         }
     }
     return $fkDifferences;
 }
 public function testCompareSort()
 {
     $c1 = new Column('Foo');
     $c2 = new Column('Bar');
     $c3 = new Column('Baz');
     $c4 = new Column('Faz');
     $fk1 = new ForeignKey();
     $fk1->addReference($c1, $c3);
     $fk1->addReference($c2, $c4);
     $t1 = new Table('Baz');
     $t1->addForeignKey($fk1);
     $fk2 = new ForeignKey();
     $fk2->addReference($c2, $c4);
     $fk2->addReference($c1, $c3);
     $t2 = new Table('Baz');
     $t2->addForeignKey($fk2);
     $this->assertFalse(ForeignKeyComparator::computeDiff($fk1, $fk2));
 }