Commit 63393727 authored by beberlei's avatar beberlei

[2.0] DDC-169 - Add functionality to detect that an add + drop column diff is...

[2.0] DDC-169 - Add functionality to detect that an add + drop column diff is actually just a rename column.
parent 35e0121b
...@@ -144,30 +144,44 @@ class Comparator ...@@ -144,30 +144,44 @@ class Comparator
$changes = 0; $changes = 0;
$tableDifferences = new TableDiff(); $tableDifferences = new TableDiff();
$table1Columns = $table1->getColumns();
$table2Columns = $table2->getColumns();
/* See if all the fields in table 1 exist in table 2 */ /* See if all the fields in table 1 exist in table 2 */
foreach ( $table2->getColumns() as $columnName => $column ) { foreach ( $table2Columns as $columnName => $column ) {
if ( !$table1->hasColumn($columnName) ) { if ( !$table1->hasColumn($columnName) ) {
$tableDifferences->addedColumns[$columnName] = $column; $tableDifferences->addedColumns[$columnName] = $column;
$changes++; $changes++;
} }
} }
/* See if there are any removed fields in table 2 */ /* See if there are any removed fields in table 2 */
foreach ( $table1->getColumns() as $columnName => $column ) { foreach ( $table1Columns as $columnName => $column ) {
if ( !$table2->hasColumn($columnName) ) { if ( !$table2->hasColumn($columnName) ) {
$tableDifferences->removedColumns[$columnName] = true; $tableDifferences->removedColumns[$columnName] = $column;
$changes++; $changes++;
} }
} }
/* See if there are any changed fieldDefinitioninitions */ /* See if there are any changed fieldDefinitioninitions */
foreach ( $table1->getColumns() as $columnName => $column ) { foreach ( $table1Columns as $columnName => $column ) {
if ( $table2->hasColumn($columnName) ) { if ( $table2->hasColumn($columnName) ) {
if ( $this->diffColumn( $column, $table2->getColumn($columnName) ) ) { if ( $this->diffColumn( $column, $table2->getColumn($columnName) ) ) {
$tableDifferences->changedColumns[$columnName] = $table2->getColumn($columnName); $tableDifferences->changedColumns[$column->getName()] = $table2->getColumn($columnName);
$changes++; $changes++;
} }
} }
} }
// Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
foreach ($tableDifferences->addedColumns AS $addedColumnName => $addedColumn) {
foreach ($tableDifferences->removedColumns AS $removedColumnName => $removedColumn) {
if ($this->diffColumn($addedColumn, $removedColumn) === false) {
$tableDifferences->renamedColumns[$removedColumn->getName()] = $addedColumn;
unset($tableDifferences->addedColumns[$addedColumnName]);
unset($tableDifferences->removedColumns[$removedColumnName]);
}
}
}
$table1Indexes = $table1->getIndexes(); $table1Indexes = $table1->getIndexes();
$table2Indexes = $table2->getIndexes(); $table2Indexes = $table2->getIndexes();
......
...@@ -55,6 +55,13 @@ class TableDiff ...@@ -55,6 +55,13 @@ class TableDiff
*/ */
public $removedColumns = array(); public $removedColumns = array();
/**
* Columns that are only renamed from key to column instance name.
*
* @var array(string=>Column)
*/
public $renamedColumns = array();
/** /**
* All added indexes * All added indexes
* *
......
...@@ -133,10 +133,11 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -133,10 +133,11 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
public function testCompareMissingField() public function testCompareMissingField()
{ {
$missingColumn = new Column('integerfield1', Type::getType('integer'));
$schema1 = new Schema( array( $schema1 = new Schema( array(
'bugdb' => new Table('bugdb', 'bugdb' => new Table('bugdb',
array ( array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')), 'integerfield1' => $missingColumn,
'integerfield2' => new Column('integerfield2', Type::getType('integer')), 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
) )
), ),
...@@ -153,7 +154,7 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -153,7 +154,7 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
array ( array (
'bugdb' => new TableDiff( array(), array(), 'bugdb' => new TableDiff( array(), array(),
array ( array (
'integerfield1' => true, 'integerfield1' => $missingColumn,
) )
) )
) )
...@@ -558,6 +559,23 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -558,6 +559,23 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($tableDiff); $this->assertFalse($tableDiff);
} }
public function testDetectRenameColumn()
{
$tableA = new Table("foo");
$tableA->createColumn('foo', 'integer');
$tableB = new Table("foo");
$tableB->createColumn('bar', 'integer');
$c = new Comparator();
$tableDiff = $c->diffTable($tableA, $tableB);
$this->assertEquals(0, count($tableDiff->addedColumns));
$this->assertEquals(0, count($tableDiff->removedColumns));
$this->assertArrayHasKey('foo', $tableDiff->renamedColumns);
$this->assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
}
/** /**
* @param SchemaDiff $diff * @param SchemaDiff $diff
* @param int $newTableCount * @param int $newTableCount
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment