1
0
mirror of synced 2024-12-12 22:36:02 +03:00

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

This commit is contained in:
beberlei 2009-12-05 11:01:11 +00:00
parent 35e0121b49
commit 6339372718
3 changed files with 46 additions and 7 deletions

View File

@ -144,30 +144,44 @@ class Comparator
$changes = 0;
$tableDifferences = new TableDiff();
$table1Columns = $table1->getColumns();
$table2Columns = $table2->getColumns();
/* 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) ) {
$tableDifferences->addedColumns[$columnName] = $column;
$changes++;
}
}
/* 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) ) {
$tableDifferences->removedColumns[$columnName] = true;
$tableDifferences->removedColumns[$columnName] = $column;
$changes++;
}
}
/* See if there are any changed fieldDefinitioninitions */
foreach ( $table1->getColumns() as $columnName => $column ) {
foreach ( $table1Columns as $columnName => $column ) {
if ( $table2->hasColumn($columnName) ) {
if ( $this->diffColumn( $column, $table2->getColumn($columnName) ) ) {
$tableDifferences->changedColumns[$columnName] = $table2->getColumn($columnName);
$tableDifferences->changedColumns[$column->getName()] = $table2->getColumn($columnName);
$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();
$table2Indexes = $table2->getIndexes();

View File

@ -55,6 +55,13 @@ class TableDiff
*/
public $removedColumns = array();
/**
* Columns that are only renamed from key to column instance name.
*
* @var array(string=>Column)
*/
public $renamedColumns = array();
/**
* All added indexes
*

View File

@ -133,10 +133,11 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
public function testCompareMissingField()
{
$missingColumn = new Column('integerfield1', Type::getType('integer'));
$schema1 = new Schema( array(
'bugdb' => new Table('bugdb',
array (
'integerfield1' => new Column('integerfield1', Type::getType('integer')),
'integerfield1' => $missingColumn,
'integerfield2' => new Column('integerfield2', Type::getType('integer')),
)
),
@ -153,7 +154,7 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
array (
'bugdb' => new TableDiff( array(), array(),
array (
'integerfield1' => true,
'integerfield1' => $missingColumn,
)
)
)
@ -558,6 +559,23 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$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 int $newTableCount