/** * 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; }
public function testToString() { $tableA = new Table('A'); $tableB = new Table('B'); $diff = new TableDiff($tableA, $tableB); $diff->addAddedColumn('id', new Column('id', 'integer')); $diff->addRemovedColumn('category_id', new Column('category_id', 'integer')); $colFoo = new Column('foo', 'integer'); $colBar = new Column('bar', 'integer'); $tableA->addColumn($colFoo); $tableA->addColumn($colBar); $diff->addRenamedColumn($colFoo, $colBar); $columnDiff = new ColumnDiff($colFoo, $colBar); $diff->addModifiedColumn('foo', $columnDiff); $fk = new ForeignKey('category'); $fk->setTable($tableA); $fk->setForeignTableCommonName('B'); $fk->addReference('category_id', 'id'); $fkChanged = clone $fk; $fkChanged->setForeignTableCommonName('C'); $fkChanged->addReference('bla', 'id2'); $fkChanged->setOnDelete('cascade'); $fkChanged->setOnUpdate('cascade'); $diff->addAddedFk('category', $fk); $diff->addModifiedFk('category', $fk, $fkChanged); $diff->addRemovedFk('category', $fk); $index = new Index('test_index'); $index->setTable($tableA); $index->setColumns([$colFoo]); $indexChanged = clone $index; $indexChanged->setColumns([$colBar]); $diff->addAddedIndex('test_index', $index); $diff->addModifiedIndex('test_index', $index, $indexChanged); $diff->addRemovedIndex('test_index', $index); $string = (string) $diff; $expected = ' A: addedColumns: - id removedColumns: - category_id modifiedColumns: A.FOO: modifiedProperties: renamedColumns: foo: bar addedIndices: - test_index removedIndices: - test_index modifiedIndices: - test_index addedFks: - category removedFks: - category modifiedFks: category: localColumns: from ["category_id"] to ["category_id","bla"] foreignColumns: from ["id"] to ["id","id2"] onUpdate: from to CASCADE onDelete: from to CASCADE '; $this->assertEquals($expected, $string); }