Permalink
Browse files

Added reference to original schema or table in schema difference classes

  • Loading branch information...
1 parent 6876437 commit 95522656fde1c4a940f8ee22bc9b0c53790dea35 @hason hason committed Oct 17, 2012
@@ -24,7 +24,7 @@
/**
* Represent the change of a column
*
- *
+ *
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
@@ -44,11 +44,17 @@ class ColumnDiff
*/
public $changedProperties = array();
- public function __construct($oldColumnName, Column $column, array $changedProperties = array())
+ /**
+ * @var Column
+ */
+ public $fromColumn;
+
+ public function __construct($oldColumnName, Column $column, array $changedProperties = array(), Column $fromColumn = null)
{
$this->oldColumnName = $oldColumnName;
$this->column = $column;
$this->changedProperties = $changedProperties;
+ $this->fromColumn = $fromColumn;
}
public function hasChanged($propertyName)
@@ -58,6 +58,7 @@ static public function compareSchemas( Schema $fromSchema, Schema $toSchema )
public function compare(Schema $fromSchema, Schema $toSchema)
{
$diff = new SchemaDiff();
+ $diff->fromSchema = $fromSchema;
$foreignKeysToTable = array();
@@ -179,6 +180,7 @@ public function diffTable(Table $table1, Table $table2)
{
$changes = 0;
$tableDifferences = new TableDiff($table1->getName());
+ $tableDifferences->fromTable = $table1;
$table1Columns = $table1->getColumns();
$table2Columns = $table2->getColumns();
@@ -203,6 +205,7 @@ public function diffTable(Table $table1, Table $table2)
$changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) );
if (count($changedProperties) ) {
$columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
+ $columnDiff->fromColumn = $column;
$tableDifferences->changedColumns[$column->getName()] = $columnDiff;
$changes++;
}
@@ -24,7 +24,7 @@
/**
* Schema Diff
*
- *
+ *
* @link www.doctrine-project.org
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
@@ -35,6 +35,11 @@
class SchemaDiff
{
/**
+ * @var Schema
+ */
+ public $fromSchema;
+
+ /**
* All added tables
*
* @var array(string=>ezcDbSchemaTable)
@@ -81,12 +86,14 @@ class SchemaDiff
* @param array(string=>Table) $newTables
* @param array(string=>TableDiff) $changedTables
* @param array(string=>bool) $removedTables
+ * @param Schema $fromSchema
*/
- public function __construct($newTables = array(), $changedTables = array(), $removedTables = array())
+ public function __construct($newTables = array(), $changedTables = array(), $removedTables = array(), Schema $fromSchema = null)
{
$this->newTables = $newTables;
$this->changedTables = $changedTables;
$this->removedTables = $removedTables;
+ $this->fromSchema = $fromSchema;
}
/**
@@ -22,7 +22,7 @@
/**
* Table Diff
*
- *
+ *
* @link www.doctrine-project.org
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License
@@ -112,6 +112,11 @@ class TableDiff
public $removedForeignKeys = array();
/**
+ * @var Table
+ */
+ public $fromTable;
+
+ /**
* Constructs an TableDiff object.
*
* @param array(string=>Column) $addedColumns
@@ -120,10 +125,11 @@ class TableDiff
* @param array(string=>Index) $addedIndexes
* @param array(string=>Index) $changedIndexes
* @param array(string=>bool) $removedIndexes
+ * @param Table $fromTable
*/
public function __construct($tableName, $addedColumns = array(),
$changedColumns = array(), $removedColumns = array(), $addedIndexes = array(),
- $changedIndexes = array(), $removedIndexes = array())
+ $changedIndexes = array(), $removedIndexes = array(), Table $fromTable = null)
{
$this->name = $tableName;
$this->addedColumns = $addedColumns;
@@ -132,5 +138,6 @@ public function __construct($tableName, $addedColumns = array(),
$this->addedIndexes = $addedIndexes;
$this->changedIndexes = $changedIndexes;
$this->removedIndexes = $removedIndexes;
+ $this->fromTable = $fromTable;
}
}
@@ -29,6 +29,7 @@
Doctrine\DBAL\Schema\Sequence,
Doctrine\DBAL\Schema\SchemaDiff,
Doctrine\DBAL\Schema\TableDiff,
+ Doctrine\DBAL\Schema\ColumnDiff,
Doctrine\DBAL\Schema\Comparator,
Doctrine\DBAL\Types\Type,
Doctrine\DBAL\Schema\ForeignKeyConstraint;
@@ -61,7 +62,9 @@ public function testCompareSame1()
),
) );
- $this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) );
+ $expected = new SchemaDiff();
+ $expected->fromSchema = $schema1;
+ $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareSame2()
@@ -82,7 +85,10 @@ public function testCompareSame2()
)
),
) );
- $this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) );
+
+ $expected = new SchemaDiff();
+ $expected->fromSchema = $schema1;
+ $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
public function testCompareMissingTable()
@@ -94,7 +100,7 @@ public function testCompareMissingTable()
$schema1 = new Schema( array($table), array(), $schemaConfig );
$schema2 = new Schema( array(), array(), $schemaConfig );
- $expected = new SchemaDiff( array(), array(), array('bugdb' => $table) );
+ $expected = new SchemaDiff( array(), array(), array('bugdb' => $table), $schema1 );
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
@@ -108,7 +114,8 @@ public function testCompareNewTable()
$schema1 = new Schema( array(), array(), $schemaConfig );
$schema2 = new Schema( array($table), array(), $schemaConfig );
- $expected = new SchemaDiff( array('bugdb' => $table), array(), array() );
+ $expected = new SchemaDiff( array('bugdb' => $table), array(), array(), $schema1 );
+
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
@@ -151,6 +158,9 @@ public function testCompareMissingField()
)
)
);
+ $expected->fromSchema = $schema1;
+ $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
+
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
@@ -181,6 +191,9 @@ public function testCompareNewField()
),
)
);
+ $expected->fromSchema = $schema1;
+ $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
+
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
@@ -251,6 +264,9 @@ public function testCompareRemovedIndex()
),
)
);
+ $expected->fromSchema = $schema1;
+ $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
+
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
@@ -295,6 +311,9 @@ public function testCompareNewIndex()
),
)
);
+ $expected->fromSchema = $schema1;
+ $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
+
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}
@@ -346,8 +365,10 @@ public function testCompareChangedIndex()
),
)
);
- $actual = Comparator::compareSchemas( $schema1, $schema2 );
- $this->assertEquals($expected, $actual);
+ $expected->fromSchema = $schema1;
+ $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
+
+ $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
}
public function testCompareChangedIndexFieldPositions()
@@ -384,8 +405,10 @@ public function testCompareChangedIndexFieldPositions()
),
)
);
- $actual = Comparator::compareSchemas( $schema1, $schema2 );
- $this->assertEquals($expected, $actual);
+ $expected->fromSchema = $schema1;
+ $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
+
+ $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
}
public function testCompareSequences()
@@ -717,8 +740,10 @@ public function testFqnSchemaComparision()
$newSchema= new Schema(array(), array(), $config);
$newSchema->createTable('foo.bar');
- $c = new Comparator();
- $this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
+ $expected = new SchemaDiff();
+ $expected->fromSchema = $oldSchema;
+
+ $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
}
/**
@@ -735,9 +760,10 @@ public function testFqnSchemaComparisionDifferentSchemaNameButSameTableNoDiff()
$newSchema = new Schema();
$newSchema->createTable('bar');
- $c = new Comparator();
- $diff = $c->compare($oldSchema, $newSchema);
- $this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
+ $expected = new SchemaDiff();
+ $expected->fromSchema = $oldSchema;
+
+ $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
}
/**
@@ -753,10 +779,10 @@ public function testFqnSchemaComparisionNoSchemaSame()
$newSchema = new Schema();
$newSchema->createTable('bar');
- $c = new Comparator();
- $diff = $c->compare($oldSchema, $newSchema);
+ $expected = new SchemaDiff();
+ $expected->fromSchema = $oldSchema;
- $this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
+ $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
}
/**
@@ -810,6 +836,27 @@ public function testAvoidMultipleDropForeignKey()
$this->assertCount(1, $diff->orphanedForeignKeys);
}
+ public function testCompareChangedColumn()
+ {
+ $oldSchema = new Schema();
+
+ $tableFoo = $oldSchema->createTable('foo');
+ $tableFoo->addColumn('id', 'integer');
+
+ $newSchema = new Schema();
+ $table = $newSchema->createTable('foo');
+ $table->addColumn('id', 'string');
+
+ $expected = new SchemaDiff();
+ $expected->fromSchema = $oldSchema;
+ $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo');
+ $tableDiff->fromTable = $tableFoo;
+ $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
+ $columnDiff->fromColumn = $tableFoo->getColumn('id');
+ $columnDiff->changedProperties = array('type');
+
+ $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
+ }
/**
* @param SchemaDiff $diff

0 comments on commit 9552265

Please sign in to comment.