Commit 95522656 authored by Martin Hasoň's avatar Martin Hasoň

Added reference to original schema or table in schema difference classes

parent 68764374
...@@ -24,7 +24,7 @@ namespace Doctrine\DBAL\Schema; ...@@ -24,7 +24,7 @@ namespace Doctrine\DBAL\Schema;
/** /**
* Represent the change of a column * Represent the change of a column
* *
* *
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @since 2.0 * @since 2.0
* @version $Revision$ * @version $Revision$
...@@ -44,11 +44,17 @@ class ColumnDiff ...@@ -44,11 +44,17 @@ class ColumnDiff
*/ */
public $changedProperties = array(); 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->oldColumnName = $oldColumnName;
$this->column = $column; $this->column = $column;
$this->changedProperties = $changedProperties; $this->changedProperties = $changedProperties;
$this->fromColumn = $fromColumn;
} }
public function hasChanged($propertyName) public function hasChanged($propertyName)
......
...@@ -58,6 +58,7 @@ class Comparator ...@@ -58,6 +58,7 @@ class Comparator
public function compare(Schema $fromSchema, Schema $toSchema) public function compare(Schema $fromSchema, Schema $toSchema)
{ {
$diff = new SchemaDiff(); $diff = new SchemaDiff();
$diff->fromSchema = $fromSchema;
$foreignKeysToTable = array(); $foreignKeysToTable = array();
...@@ -179,6 +180,7 @@ class Comparator ...@@ -179,6 +180,7 @@ class Comparator
{ {
$changes = 0; $changes = 0;
$tableDifferences = new TableDiff($table1->getName()); $tableDifferences = new TableDiff($table1->getName());
$tableDifferences->fromTable = $table1;
$table1Columns = $table1->getColumns(); $table1Columns = $table1->getColumns();
$table2Columns = $table2->getColumns(); $table2Columns = $table2->getColumns();
...@@ -203,6 +205,7 @@ class Comparator ...@@ -203,6 +205,7 @@ class Comparator
$changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) ); $changedProperties = $this->diffColumn( $column, $table2->getColumn($columnName) );
if (count($changedProperties) ) { if (count($changedProperties) ) {
$columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties);
$columnDiff->fromColumn = $column;
$tableDifferences->changedColumns[$column->getName()] = $columnDiff; $tableDifferences->changedColumns[$column->getName()] = $columnDiff;
$changes++; $changes++;
} }
......
...@@ -24,7 +24,7 @@ use \Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -24,7 +24,7 @@ use \Doctrine\DBAL\Platforms\AbstractPlatform;
/** /**
* Schema Diff * Schema Diff
* *
* *
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License * @license http://ez.no/licenses/new_bsd New BSD License
...@@ -34,6 +34,11 @@ use \Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -34,6 +34,11 @@ use \Doctrine\DBAL\Platforms\AbstractPlatform;
*/ */
class SchemaDiff class SchemaDiff
{ {
/**
* @var Schema
*/
public $fromSchema;
/** /**
* All added tables * All added tables
* *
...@@ -81,12 +86,14 @@ class SchemaDiff ...@@ -81,12 +86,14 @@ class SchemaDiff
* @param array(string=>Table) $newTables * @param array(string=>Table) $newTables
* @param array(string=>TableDiff) $changedTables * @param array(string=>TableDiff) $changedTables
* @param array(string=>bool) $removedTables * @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->newTables = $newTables;
$this->changedTables = $changedTables; $this->changedTables = $changedTables;
$this->removedTables = $removedTables; $this->removedTables = $removedTables;
$this->fromSchema = $fromSchema;
} }
/** /**
......
...@@ -22,7 +22,7 @@ namespace Doctrine\DBAL\Schema; ...@@ -22,7 +22,7 @@ namespace Doctrine\DBAL\Schema;
/** /**
* Table Diff * Table Diff
* *
* *
* @link www.doctrine-project.org * @link www.doctrine-project.org
* @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
* @license http://ez.no/licenses/new_bsd New BSD License * @license http://ez.no/licenses/new_bsd New BSD License
...@@ -111,6 +111,11 @@ class TableDiff ...@@ -111,6 +111,11 @@ class TableDiff
*/ */
public $removedForeignKeys = array(); public $removedForeignKeys = array();
/**
* @var Table
*/
public $fromTable;
/** /**
* Constructs an TableDiff object. * Constructs an TableDiff object.
* *
...@@ -120,10 +125,11 @@ class TableDiff ...@@ -120,10 +125,11 @@ class TableDiff
* @param array(string=>Index) $addedIndexes * @param array(string=>Index) $addedIndexes
* @param array(string=>Index) $changedIndexes * @param array(string=>Index) $changedIndexes
* @param array(string=>bool) $removedIndexes * @param array(string=>bool) $removedIndexes
* @param Table $fromTable
*/ */
public function __construct($tableName, $addedColumns = array(), public function __construct($tableName, $addedColumns = array(),
$changedColumns = array(), $removedColumns = array(), $addedIndexes = array(), $changedColumns = array(), $removedColumns = array(), $addedIndexes = array(),
$changedIndexes = array(), $removedIndexes = array()) $changedIndexes = array(), $removedIndexes = array(), Table $fromTable = null)
{ {
$this->name = $tableName; $this->name = $tableName;
$this->addedColumns = $addedColumns; $this->addedColumns = $addedColumns;
...@@ -132,5 +138,6 @@ class TableDiff ...@@ -132,5 +138,6 @@ class TableDiff
$this->addedIndexes = $addedIndexes; $this->addedIndexes = $addedIndexes;
$this->changedIndexes = $changedIndexes; $this->changedIndexes = $changedIndexes;
$this->removedIndexes = $removedIndexes; $this->removedIndexes = $removedIndexes;
$this->fromTable = $fromTable;
} }
} }
...@@ -29,6 +29,7 @@ use Doctrine\DBAL\Schema\Schema, ...@@ -29,6 +29,7 @@ use Doctrine\DBAL\Schema\Schema,
Doctrine\DBAL\Schema\Sequence, Doctrine\DBAL\Schema\Sequence,
Doctrine\DBAL\Schema\SchemaDiff, Doctrine\DBAL\Schema\SchemaDiff,
Doctrine\DBAL\Schema\TableDiff, Doctrine\DBAL\Schema\TableDiff,
Doctrine\DBAL\Schema\ColumnDiff,
Doctrine\DBAL\Schema\Comparator, Doctrine\DBAL\Schema\Comparator,
Doctrine\DBAL\Types\Type, Doctrine\DBAL\Types\Type,
Doctrine\DBAL\Schema\ForeignKeyConstraint; Doctrine\DBAL\Schema\ForeignKeyConstraint;
...@@ -61,7 +62,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -61,7 +62,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
), ),
) ); ) );
$this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) ); $expected = new SchemaDiff();
$expected->fromSchema = $schema1;
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
public function testCompareSame2() public function testCompareSame2()
...@@ -82,7 +85,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -82,7 +85,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
) )
), ),
) ); ) );
$this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) );
$expected = new SchemaDiff();
$expected->fromSchema = $schema1;
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
public function testCompareMissingTable() public function testCompareMissingTable()
...@@ -94,7 +100,7 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -94,7 +100,7 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$schema1 = new Schema( array($table), array(), $schemaConfig ); $schema1 = new Schema( array($table), array(), $schemaConfig );
$schema2 = new Schema( array(), 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 ) ); $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
...@@ -108,7 +114,8 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -108,7 +114,8 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$schema1 = new Schema( array(), array(), $schemaConfig ); $schema1 = new Schema( array(), array(), $schemaConfig );
$schema2 = new Schema( array($table), 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 ) ); $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
...@@ -151,6 +158,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -151,6 +158,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
) )
) )
); );
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
...@@ -181,6 +191,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -181,6 +191,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
), ),
) )
); );
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
...@@ -251,6 +264,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -251,6 +264,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
), ),
) )
); );
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
...@@ -295,6 +311,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -295,6 +311,9 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
), ),
) )
); );
$expected->fromSchema = $schema1;
$expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) ); $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
} }
...@@ -346,8 +365,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -346,8 +365,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
), ),
) )
); );
$actual = Comparator::compareSchemas( $schema1, $schema2 ); $expected->fromSchema = $schema1;
$this->assertEquals($expected, $actual); $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
} }
public function testCompareChangedIndexFieldPositions() public function testCompareChangedIndexFieldPositions()
...@@ -384,8 +405,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -384,8 +405,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
), ),
) )
); );
$actual = Comparator::compareSchemas( $schema1, $schema2 ); $expected->fromSchema = $schema1;
$this->assertEquals($expected, $actual); $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
$this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
} }
public function testCompareSequences() public function testCompareSequences()
...@@ -717,8 +740,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -717,8 +740,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$newSchema= new Schema(array(), array(), $config); $newSchema= new Schema(array(), array(), $config);
$newSchema->createTable('foo.bar'); $newSchema->createTable('foo.bar');
$c = new Comparator(); $expected = new SchemaDiff();
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema)); $expected->fromSchema = $oldSchema;
$this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
} }
/** /**
...@@ -735,9 +760,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -735,9 +760,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$newSchema = new Schema(); $newSchema = new Schema();
$newSchema->createTable('bar'); $newSchema->createTable('bar');
$c = new Comparator(); $expected = new SchemaDiff();
$diff = $c->compare($oldSchema, $newSchema); $expected->fromSchema = $oldSchema;
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema));
$this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
} }
/** /**
...@@ -753,10 +779,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -753,10 +779,10 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$newSchema = new Schema(); $newSchema = new Schema();
$newSchema->createTable('bar'); $newSchema->createTable('bar');
$c = new Comparator(); $expected = new SchemaDiff();
$diff = $c->compare($oldSchema, $newSchema); $expected->fromSchema = $oldSchema;
$this->assertEquals(new SchemaDiff(), $c->compare($oldSchema, $newSchema)); $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
} }
/** /**
...@@ -810,6 +836,27 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase ...@@ -810,6 +836,27 @@ class ComparatorTest extends \PHPUnit_Framework_TestCase
$this->assertCount(1, $diff->orphanedForeignKeys); $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 * @param SchemaDiff $diff
......
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