Skip to content

Commit

Permalink
Added reference to original schema or table in schema difference classes
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Nov 17, 2012
1 parent 6876437 commit 9552265
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
10 changes: 8 additions & 2 deletions lib/Doctrine/DBAL/Schema/ColumnDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Represent the change of a column
*
*
*
* @link www.doctrine-project.org
* @since 2.0
* @version $Revision$
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lib/Doctrine/DBAL/Schema/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
Expand All @@ -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++;
}
Expand Down
11 changes: 9 additions & 2 deletions lib/Doctrine/DBAL/Schema/SchemaDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,6 +34,11 @@
*/
class SchemaDiff
{
/**
* @var Schema
*/
public $fromSchema;

/**
* All added tables
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions lib/Doctrine/DBAL/Schema/TableDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -111,6 +111,11 @@ class TableDiff
*/
public $removedForeignKeys = array();

/**
* @var Table
*/
public $fromTable;

/**
* Constructs an TableDiff object.
*
Expand All @@ -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;
Expand All @@ -132,5 +138,6 @@ public function __construct($tableName, $addedColumns = array(),
$this->addedIndexes = $addedIndexes;
$this->changedIndexes = $changedIndexes;
$this->removedIndexes = $removedIndexes;
$this->fromTable = $fromTable;
}
}
79 changes: 63 additions & 16 deletions tests/Doctrine/Tests/DBAL/Schema/ComparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 ) );
}
Expand All @@ -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 ) );
}

Expand Down Expand Up @@ -151,6 +158,9 @@ public function testCompareMissingField()
)
)
);
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}

Expand Down Expand Up @@ -181,6 +191,9 @@ public function testCompareNewField()
),
)
);
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}

Expand Down Expand Up @@ -251,6 +264,9 @@ public function testCompareRemovedIndex()
),
)
);
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}

Expand Down Expand Up @@ -295,6 +311,9 @@ public function testCompareNewIndex()
),
)
);
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9552265

Please sign in to comment.