Beispiel #1
0
 /**
  * Returns the number of differences.
  *
  * Compares the columns of the fromTable and the toTable,
  * and modifies the inner tableDiff if necessary.
  *
  * @param  boolean $caseInsensitive
  * @return integer
  */
 public function compareColumns($caseInsensitive = false)
 {
     $fromTableColumns = $this->getFromTable()->getColumns();
     $toTableColumns = $this->getToTable()->getColumns();
     $columnDifferences = 0;
     // check for new columns in $toTable
     foreach ($toTableColumns as $column) {
         if (!$this->getFromTable()->hasColumn($column->getName(), $caseInsensitive)) {
             $this->tableDiff->addAddedColumn($column->getName(), $column);
             $columnDifferences++;
         }
     }
     // check for removed columns in $toTable
     foreach ($fromTableColumns as $column) {
         if (!$this->getToTable()->hasColumn($column->getName(), $caseInsensitive)) {
             $this->tableDiff->addRemovedColumn($column->getName(), $column);
             $columnDifferences++;
         }
     }
     // check for column differences
     foreach ($fromTableColumns as $fromColumn) {
         if ($this->getToTable()->hasColumn($fromColumn->getName(), $caseInsensitive)) {
             $toColumn = $this->getToTable()->getColumn($fromColumn->getName(), $caseInsensitive);
             $columnDiff = ColumnComparator::computeDiff($fromColumn, $toColumn, $caseInsensitive);
             if ($columnDiff) {
                 $this->tableDiff->addModifiedColumn($fromColumn->getName(), $columnDiff);
                 $columnDifferences++;
             }
         }
     }
     // check for column renamings
     foreach ($this->tableDiff->getAddedColumns() as $addedColumnName => $addedColumn) {
         foreach ($this->tableDiff->getRemovedColumns() as $removedColumnName => $removedColumn) {
             if (!ColumnComparator::computeDiff($addedColumn, $removedColumn, $caseInsensitive)) {
                 // no difference except the name, that's probably a renaming
                 $this->tableDiff->addRenamedColumn($removedColumn, $addedColumn);
                 $this->tableDiff->removeAddedColumn($addedColumnName);
                 $this->tableDiff->removeRemovedColumn($removedColumnName);
                 $columnDifferences--;
                 // skip to the next added column
                 break;
             }
         }
     }
     return $columnDifferences;
 }