public function testCompareNotSamePks()
 {
     $t1 = new Table();
     $c1 = new Column('Foo');
     $c1->setPrimaryKey(true);
     $t1->addColumn($c1);
     $t2 = new Table();
     $c2 = new Column('Foo');
     $t2->addColumn($c2);
     $diff = PropelTableComparator::computeDiff($t1, $t2);
     $this->assertTrue($diff instanceof PropelTableDiff);
 }
 public function testCompareCaseInsensitive()
 {
     $t1 = new Table();
     $c1 = new Column('Foo');
     $t1->addColumn($c1);
     $t2 = new Table();
     $c2 = new Column('fOO');
     $t2->addColumn($c2);
     $diff = PropelTableComparator::computeDiff($t1, $t2);
     $this->assertTrue($diff instanceof PropelTableDiff);
     $this->assertFalse(PropelTableComparator::computeDiff($t1, $t2, true));
 }
 public function testCaseInsensitive()
 {
     $t1 = new Table('Baz');
     $c1 = new Column('Foo');
     $c2 = new Column('Bar');
     $fk1 = new ForeignKey();
     $fk1->addReference($c1, $c2);
     $t1->addForeignKey($fk1);
     $t2 = new Table('bAZ');
     $c3 = new Column('fOO');
     $c4 = new Column('bAR');
     $fk2 = new ForeignKey();
     $fk2->addReference($c3, $c4);
     $t2->addForeignKey($fk2);
     $this->assertFalse(PropelTableComparator::computeDiff($t1, $t2, true));
 }
    public function providerForTestGetModifyTableDDL()
    {
        $schema1 = <<<EOF
<database name="test">
\t<table name="foo">
\t\t<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
\t\t<column name="bar" type="INTEGER" />
\t\t<column name="baz" type="VARCHAR" size="12" required="true" />
\t\t<foreign-key name="foo1_FK_1" foreignTable="foo2">
\t\t\t<reference local="bar" foreign="bar" />
\t\t</foreign-key>
\t\t<foreign-key name="foo1_FK_2" foreignTable="foo2">
\t\t\t<reference local="baz" foreign="baz" />
\t\t</foreign-key>
\t\t<index name="bar_FK">
\t\t\t<index-column name="bar"/>
\t\t</index>
\t\t<index name="bar_baz_FK">
\t\t\t<index-column name="bar"/>
\t\t\t<index-column name="baz"/>
\t\t</index>
\t</table>
\t<table name="foo2">
\t\t<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
\t\t<column name="bar" type="INTEGER" />
\t\t<column name="baz" type="VARCHAR" size="12" required="true" />
\t</table>
</database>
EOF;
        $schema2 = <<<EOF
<database name="test">
\t<table name="foo">
\t\t<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
\t\t<column name="bar1" type="INTEGER" />
\t\t<column name="baz" type="VARCHAR" size="12" required="false" />
\t\t<column name="baz3" type="LONGVARCHAR" />
\t\t<foreign-key name="foo1_FK_1" foreignTable="foo2">
\t\t\t<reference local="bar1" foreign="bar" />
\t\t</foreign-key>
\t\t<index name="bar_FK">
\t\t\t<index-column name="bar1"/>
\t\t</index>
\t\t<index name="baz_FK">
\t\t\t<index-column name="baz3"/>
\t\t</index>
\t</table>
\t<table name="foo2">
\t\t<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
\t\t<column name="bar" type="INTEGER" />
\t\t<column name="baz" type="VARCHAR" size="12" required="true" />
\t</table>
</database>
EOF;
        $t1 = $this->getDatabaseFromSchema($schema1)->getTable('foo');
        $t2 = $this->getDatabaseFromSchema($schema2)->getTable('foo');
        return array(array(PropelTableComparator::computeDiff($t1, $t2)));
    }
 /**
  * Compare the tables of the fromDatabase and the toDatabase,
  * and modifies the inner databaseDiff if necessary.
  * Returns the number of differences.
  *
  * @param  boolean $caseInsensitive Whether the comparison is case insensitive.
  *                                  False by default.
  *
  * @return integer The number of table differences
  */
 public function compareTables($caseInsensitive = false)
 {
     $fromDatabaseTables = $this->fromDatabase->getTables();
     $toDatabaseTables = $this->toDatabase->getTables();
     $databaseDifferences = 0;
     // check for new tables in $toDatabase
     foreach ($toDatabaseTables as $table) {
         if (!$this->fromDatabase->hasTable($table->getName(), $caseInsensitive) && !$table->isSkipSql()) {
             $this->databaseDiff->addAddedTable($table->getName(), $table);
             $databaseDifferences++;
         }
     }
     // check for removed tables in $toDatabase
     foreach ($fromDatabaseTables as $table) {
         if (!$this->toDatabase->hasTable($table->getName(), $caseInsensitive) && !$table->isSkipSql()) {
             $this->databaseDiff->addRemovedTable($table->getName(), $table);
             $databaseDifferences++;
         }
     }
     // check for table differences
     foreach ($fromDatabaseTables as $fromTable) {
         if ($this->toDatabase->hasTable($fromTable->getName(), $caseInsensitive)) {
             $toTable = $this->toDatabase->getTable($fromTable->getName(), $caseInsensitive);
             $databaseDiff = PropelTableComparator::computeDiff($fromTable, $toTable, $caseInsensitive);
             if ($databaseDiff) {
                 $this->databaseDiff->addModifiedTable($fromTable->getName(), $databaseDiff);
                 $databaseDifferences++;
             }
         }
     }
     // check for table renamings
     foreach ($this->databaseDiff->getAddedTables() as $addedTableName => $addedTable) {
         foreach ($this->databaseDiff->getRemovedTables() as $removedTableName => $removedTable) {
             if (!PropelTableComparator::computeDiff($addedTable, $removedTable, $caseInsensitive)) {
                 // no difference except the name, that's probably a renaming
                 $this->databaseDiff->addRenamedTable($removedTableName, $addedTableName);
                 $this->databaseDiff->removeAddedTable($addedTableName);
                 $this->databaseDiff->removeRemovedTable($removedTableName);
                 $databaseDifferences--;
             }
         }
     }
     return $databaseDifferences;
 }
 public function testCompareCaseInsensitive()
 {
     $t1 = new Table();
     $c1 = new Column('Foo');
     $c1->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $c1->getDomain()->replaceScale(2);
     $c1->getDomain()->replaceSize(3);
     $c1->setNotNull(true);
     $c1->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
     $t1->addColumn($c1);
     $i1 = new Index('Foo_Index');
     $i1->addColumn($c1);
     $t1->addIndex($i1);
     $t2 = new Table();
     $c2 = new Column('fOO');
     $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $c2->getDomain()->replaceScale(2);
     $c2->getDomain()->replaceSize(3);
     $c2->setNotNull(true);
     $c2->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
     $t2->addColumn($c2);
     $i2 = new Index('fOO_iNDEX');
     $i2->addColumn($c2);
     $t2->addIndex($i2);
     $this->assertFalse(PropelTableComparator::computeDiff($t1, $t2, true));
 }
 public function testCompareSeveralTableDifferences()
 {
     $d1 = new Database();
     $t1 = new Table('Foo_Table');
     $c1 = new Column('Foo');
     $c1->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $c1->getDomain()->replaceScale(2);
     $c1->getDomain()->replaceSize(3);
     $c1->setNotNull(true);
     $c1->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
     $t1->addColumn($c1);
     $d1->addTable($t1);
     $t2 = new Table('Bar');
     $c2 = new Column('Bar_Column');
     $c2->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $t2->addColumn($c2);
     $d1->addTable($t2);
     $t11 = new Table('Baz');
     $d1->addTable($t11);
     $d2 = new Database();
     $t3 = new Table('Foo_Table');
     $c3 = new Column('Foo1');
     $c3->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $c3->getDomain()->replaceScale(2);
     $c3->getDomain()->replaceSize(3);
     $c3->setNotNull(true);
     $c3->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE));
     $t3->addColumn($c3);
     $d2->addTable($t3);
     $t4 = new Table('Bar2');
     $c4 = new Column('Bar_Column');
     $c4->getDomain()->copy($this->platform->getDomainForType('DOUBLE'));
     $t4->addColumn($c4);
     $d2->addTable($t4);
     $t5 = new Table('Biz');
     $c5 = new Column('Biz_Column');
     $c5->getDomain()->copy($this->platform->getDomainForType('INTEGER'));
     $t5->addColumn($c5);
     $d2->addTable($t5);
     // Foo_Table was modified, Bar was renamed, Baz was removed, Biz was added
     $dc = new PropelDatabaseComparator();
     $dc->setFromDatabase($d1);
     $dc->setToDatabase($d2);
     $nbDiffs = $dc->compareTables();
     $databaseDiff = $dc->getDatabaseDiff();
     $this->assertEquals(4, $nbDiffs);
     $this->assertEquals(array('Bar' => 'Bar2'), $databaseDiff->getRenamedTables());
     $this->assertEquals(array('Biz' => $t5), $databaseDiff->getAddedTables());
     $this->assertEquals(array('Baz' => $t11), $databaseDiff->getRemovedTables());
     $tableDiff = PropelTableComparator::computeDiff($t1, $t3);
     $this->assertEquals(array('Foo_Table' => $tableDiff), $databaseDiff->getModifiedTables());
 }