ComparatorTest.php 38.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21 22 23
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\Tests\DBAL\Schema;

require_once __DIR__ . '/../../TestInit.php';

jeroendedauw's avatar
jeroendedauw committed
24 25 26 27 28 29 30 31 32 33 34 35
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/**
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @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
 * @since   2.0
 * @version $Revision$
 * @author  Benjamin Eberlei <kontakt@beberlei.de>
 */
class ComparatorTest extends \PHPUnit_Framework_TestCase
{
    public function testCompareSame1()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer' ) ),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer') ),
                )
            ),
        ) );

65 66 67
        $expected = new SchemaDiff();
        $expected->fromSchema = $schema1;
        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    }

    public function testCompareSame2()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                )
            ),
        ) );
88 89 90 91

        $expected = new SchemaDiff();
        $expected->fromSchema = $schema1;
        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
92 93 94 95
    }

    public function testCompareMissingTable()
    {
96 97 98
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
        $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
        $table->setSchemaConfig($schemaConfig);
99

100 101
        $schema1 = new Schema( array($table), array(), $schemaConfig );
        $schema2 = new Schema( array(),       array(), $schemaConfig );
102

103
        $expected = new SchemaDiff( array(), array(), array('bugdb' => $table), $schema1 );
104

105 106 107 108 109
        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
    }

    public function testCompareNewTable()
    {
110 111 112
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
        $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
        $table->setSchemaConfig($schemaConfig);
113

114 115
        $schema1 = new Schema( array(),       array(), $schemaConfig );
        $schema2 = new Schema( array($table), array(), $schemaConfig );
116

117 118
        $expected = new SchemaDiff( array('bugdb' => $table), array(), array(), $schema1 );

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

122 123 124 125 126 127 128 129 130 131 132
    public function testCompareOnlyAutoincrementChanged()
    {
        $column1 = new Column('foo', Type::getType('integer'), array('autoincrement' => true));
        $column2 = new Column('foo', Type::getType('integer'), array('autoincrement' => false));

        $comparator = new Comparator();
        $changedProperties = $comparator->diffColumn($column1, $column2);

        $this->assertEquals(array('autoincrement'), $changedProperties);
    }

133 134
    public function testCompareMissingField()
    {
135
        $missingColumn = new Column('integerfield1', Type::getType('integer'));
136 137 138
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
139
                    'integerfield1' => $missingColumn,
140 141 142 143 144 145 146 147 148 149 150 151 152 153
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
154
                'bugdb' => new TableDiff( 'bugdb', array(), array(),
155
                    array (
156
                        'integerfield1' => $missingColumn,
157 158 159 160
                    )
                )
            )
        );
161 162 163
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
    }

    public function testCompareNewField()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
187
                'bugdb' => new TableDiff ('bugdb',
188 189 190 191 192 193
                    array (
                        'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                    )
                ),
            )
        );
194 195 196
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

197 198 199 200 201
        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
    }

    public function testCompareChangedColumns_ChangeType()
    {
202 203
        $column1 = new Column('charfield1', Type::getType('string'));
        $column2 = new Column('charfield1', Type::getType('integer'));
204

205
        $c = new Comparator();
206 207
        $this->assertEquals(array('type'), $c->diffColumn($column1, $column2));
        $this->assertEquals(array(), $c->diffColumn($column1, $column1));
208 209
    }

210 211 212 213 214 215 216
    public function testCompareChangedColumns_ChangeCustomSchemaOption()
    {
        $column1 = new Column('charfield1', Type::getType('string'));
        $column2 = new Column('charfield1', Type::getType('string'));

        $column1->setCustomSchemaOption('foo', 'bar');
        $column2->setCustomSchemaOption('foo', 'bar');
217

218 219 220 221 222 223 224 225
        $column1->setCustomSchemaOption('foo1', 'bar1');
        $column2->setCustomSchemaOption('foo2', 'bar2');

        $c = new Comparator();
        $this->assertEquals(array('foo1', 'foo2'), $c->diffColumn($column1, $column2));
        $this->assertEquals(array(), $c->diffColumn($column1, $column1));
    }

226 227 228 229 230 231 232 233 234 235 236 237
    public function testCompareChangeColumns_MultipleNewColumnsRename()
    {
        $tableA = new Table("foo");
        $tableA->addColumn('datefield1', 'datetime');

        $tableB = new Table("foo");
        $tableB->addColumn('new_datefield1', 'datetime');
        $tableB->addColumn('new_datefield2', 'datetime');

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

238
        $this->assertCount(1, $tableDiff->renamedColumns, "we should have one rename datefield1 => new_datefield1.");
239
        $this->assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1");
240
        $this->assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added");
241
        $this->assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!");
242 243
        $this->assertCount(0, $tableDiff->removedColumns, "Nothing should be removed.");
        $this->assertCount(0, $tableDiff->changedColumns, "Nothing should be changed as all fields old & new have diff names.");
244
    }
Lee Davis's avatar
Lee Davis committed
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    public function testCompareRemovedIndex()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
275
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(), array(),
276
                    array (
277 278 279 280 281 282
                        'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
283 284 285 286
                    )
                ),
            )
        );
287 288 289
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
    }

    public function testCompareNewIndex()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
322
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(),
323 324 325 326 327 328 329 330 331 332 333
                    array (
                        'primary' => new Index('primary',
                            array(
                                'integerfield1'
                            ),
                            true
                        )
                    )
                ),
            )
        );
334 335 336
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
    }

    public function testCompareChangedIndex()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array('integerfield1', 'integerfield2'),
                        true
                    )
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
375
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(),
376 377 378 379 380 381 382 383 384 385 386 387
                    array (
                        'primary' => new Index('primary',
                            array(
                                'integerfield1',
                                'integerfield2'
                            ),
                            true
                        )
                    )
                ),
            )
        );
388 389 390 391
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
392
    }
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420

    public function testCompareChangedIndexFieldPositions()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary', array('integerfield1', 'integerfield2'), true)
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
421
                'bugdb' => new TableDiff('bugdb', array(), array(), array(), array(),
422 423 424 425 426 427
                    array (
                        'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
                    )
                ),
            )
        );
428 429 430 431
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

        $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    }

    public function testCompareSequences()
    {
        $seq1 = new Sequence('foo', 1, 1);
        $seq2 = new Sequence('foo', 1, 2);
        $seq3 = new Sequence('foo', 2, 1);

        $c = new Comparator();

        $this->assertTrue($c->diffSequence($seq1, $seq2));
        $this->assertTrue($c->diffSequence($seq1, $seq3));
    }

    public function testRemovedSequence()
    {
        $schema1 = new Schema();
        $seq = $schema1->createSequence('foo');

        $schema2 = new Schema();

        $c = new Comparator();
        $diffSchema = $c->compare($schema1, $schema2);

        $this->assertEquals(1, count($diffSchema->removedSequences));
        $this->assertSame($seq, $diffSchema->removedSequences[0]);
    }

    public function testAddedSequence()
    {
        $schema1 = new Schema();

        $schema2 = new Schema();
        $seq = $schema2->createSequence('foo');

        $c = new Comparator();
        $diffSchema = $c->compare($schema1, $schema2);

        $this->assertEquals(1, count($diffSchema->newSequences));
        $this->assertSame($seq, $diffSchema->newSequences[0]);
    }

    public function testTableAddForeignKey()
    {
        $tableForeign = new Table("bar");
477
        $tableForeign->addColumn('id', 'integer');
478 479

        $table1 = new Table("foo");
480
        $table1->addColumn('fk', 'integer');
481 482

        $table2 = new Table("foo");
483
        $table2->addColumn('fk', 'integer');
484 485 486 487 488
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table1, $table2);

489
        $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
490 491 492 493 494 495
        $this->assertEquals(1, count($tableDiff->addedForeignKeys));
    }

    public function testTableRemoveForeignKey()
    {
        $tableForeign = new Table("bar");
496
        $tableForeign->addColumn('id', 'integer');
497 498

        $table1 = new Table("foo");
499
        $table1->addColumn('fk', 'integer');
500 501

        $table2 = new Table("foo");
502
        $table2->addColumn('fk', 'integer');
503 504 505 506 507
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table2, $table1);

508
        $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
509 510 511 512 513 514
        $this->assertEquals(1, count($tableDiff->removedForeignKeys));
    }

    public function testTableUpdateForeignKey()
    {
        $tableForeign = new Table("bar");
515
        $tableForeign->addColumn('id', 'integer');
516 517

        $table1 = new Table("foo");
518
        $table1->addColumn('fk', 'integer');
519 520 521
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $table2 = new Table("foo");
522
        $table2->addColumn('fk', 'integer');
523 524 525 526 527
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table1, $table2);

528
        $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
        $this->assertEquals(1, count($tableDiff->changedForeignKeys));
    }

    public function testMovedForeignKeyForeignTable()
    {
        $tableForeign = new Table("bar");
        $tableForeign->addColumn('id', 'integer');

        $tableForeign2 = new Table("bar2");
        $tableForeign2->addColumn('id', 'integer');

        $table1 = new Table("foo");
        $table1->addColumn('fk', 'integer');
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $table2 = new Table("foo");
        $table2->addColumn('fk', 'integer');
        $table2->addForeignKeyConstraint($tableForeign2, array('fk'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table1, $table2);

        $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
552 553
        $this->assertEquals(1, count($tableDiff->changedForeignKeys));
    }
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

    public function testTablesCaseInsensitive()
    {
        $schemaA = new Schema();
        $schemaA->createTable('foo');
        $schemaA->createTable('bAr');
        $schemaA->createTable('BAZ');
        $schemaA->createTable('new');

        $schemaB = new Schema();
        $schemaB->createTable('FOO');
        $schemaB->createTable('bar');
        $schemaB->createTable('Baz');
        $schemaB->createTable('old');

        $c = new Comparator();
        $diff = $c->compare($schemaA, $schemaB);

        $this->assertSchemaTableChangeCount($diff, 1, 0, 1);
    }

    public function testSequencesCaseInsenstive()
    {
577 578 579 580 581 582 583 584 585 586 587
        $schemaA = new Schema();
        $schemaA->createSequence('foo');
        $schemaA->createSequence('BAR');
        $schemaA->createSequence('Baz');
        $schemaA->createSequence('new');

        $schemaB = new Schema();
        $schemaB->createSequence('FOO');
        $schemaB->createSequence('Bar');
        $schemaB->createSequence('baz');
        $schemaB->createSequence('old');
588

589 590 591 592 593 594 595 596 597
        $c = new Comparator();
        $diff = $c->compare($schemaA, $schemaB);

        $this->assertSchemaSequenceChangeCount($diff, 1, 0, 1);
    }

    public function testCompareColumnCompareCaseInsensitive()
    {
        $tableA = new Table("foo");
598
        $tableA->addColumn('id', 'integer');
599 600

        $tableB = new Table("foo");
601
        $tableB->addColumn('ID', 'integer');
602 603 604 605 606 607 608 609 610 611

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

        $this->assertFalse($tableDiff);
    }

    public function testCompareIndexBasedOnPropertiesNotName()
    {
        $tableA = new Table("foo");
612
        $tableA->addColumn('id', 'integer');
613 614 615
        $tableA->addIndex(array("id"), "foo_bar_idx");

        $tableB = new Table("foo");
616
        $tableB->addColumn('ID', 'integer');
617 618 619
        $tableB->addIndex(array("id"), "bar_foo_idx");

        $c = new Comparator();
620 621 622
        $tableDiff = new TableDiff('foo');
        $tableDiff->fromTable = $tableA;
        $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', array('id'));
623

624 625 626 627
        $this->assertEquals(
            $tableDiff,
            $c->diffTable($tableA, $tableB)
        );
628 629 630 631 632
    }

    public function testCompareForeignKeyBasedOnPropertiesNotName()
    {
        $tableA = new Table("foo");
633
        $tableA->addColumn('id', 'integer');
634 635 636
        $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('id'));

        $tableB = new Table("foo");
637
        $tableB->addColumn('ID', 'integer');
638 639 640 641 642 643
        $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

        $this->assertFalse($tableDiff);
644 645
    }

646 647 648 649 650 651 652 653 654
    public function testCompareForeignKey_RestrictNoAction_AreTheSame()
    {
        $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
        $fk2 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'RESTRICT'));

        $c = new Comparator();
        $this->assertFalse($c->diffForeignKey($fk1, $fk2));
    }

655 656 657 658 659 660 661 662 663 664 665 666
    /**
     * @group DBAL-492
     */
    public function testCompareForeignKeyNamesUnqualified_AsNoSchemaInformationIsAvailable()
    {
        $fk1 = new ForeignKeyConstraint(array("foo"), "foo.bar", array("baz"), "fk1");
        $fk2 = new ForeignKeyConstraint(array("foo"), "baz.bar", array("baz"), "fk1");

        $c = new Comparator();
        $this->assertFalse($c->diffForeignKey($fk1, $fk2));
    }

667 668 669
    public function testDetectRenameColumn()
    {
        $tableA = new Table("foo");
670
        $tableA->addColumn('foo', 'integer');
671 672

        $tableB = new Table("foo");
673
        $tableB->addColumn('bar', 'integer');
674 675 676 677 678 679 680 681 682 683

        $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());
    }

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
    /**
     * You can easily have ambiguouties in the column renaming. If these
     * are detected no renaming should take place, instead adding and dropping
     * should be used exclusively.
     *
     * @group DBAL-24
     */
    public function testDetectRenameColumnAmbiguous()
    {
        $tableA = new Table("foo");
        $tableA->addColumn('foo', 'integer');
        $tableA->addColumn('bar', 'integer');

        $tableB = new Table("foo");
        $tableB->addColumn('baz', 'integer');
699

700 701 702 703 704 705 706 707 708 709 710
        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

        $this->assertEquals(1, count($tableDiff->addedColumns), "'baz' should be added, not created through renaming!");
        $this->assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
        $this->assertEquals(2, count($tableDiff->removedColumns), "'foo' and 'bar' should both be dropped, an ambigouty exists which one could be renamed to 'baz'.");
        $this->assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed.");
        $this->assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed.");
        $this->assertEquals(0, count($tableDiff->renamedColumns), "no renamings should take place.");
    }

beberlei's avatar
beberlei committed
711 712
    public function testDetectChangeIdentifierType()
    {
713 714
        $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');

beberlei's avatar
beberlei committed
715
        $tableA = new Table("foo");
716
        $tableA->addColumn('id', 'integer', array('autoincrement' => false));
beberlei's avatar
beberlei committed
717 718

        $tableB = new Table("foo");
719
        $tableB->addColumn('id', 'integer', array('autoincrement' => true));
beberlei's avatar
beberlei committed
720 721 722 723

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

724
        $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
beberlei's avatar
beberlei committed
725 726 727
        $this->assertArrayHasKey('id', $tableDiff->changedColumns);
    }

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

    /**
     * @group DBAL-105
     */
    public function testDiff()
    {
        $table = new \Doctrine\DBAL\Schema\Table('twitter_users');
        $table->addColumn('id', 'integer', array('autoincrement' => true));
        $table->addColumn('twitterId', 'integer', array('nullable' => false));
        $table->addColumn('displayName', 'string', array('nullable' => false));
        $table->setPrimaryKey(array('id'));

        $newtable = new \Doctrine\DBAL\Schema\Table('twitter_users');
        $newtable->addColumn('id', 'integer', array('autoincrement' => true));
        $newtable->addColumn('twitter_id', 'integer', array('nullable' => false));
        $newtable->addColumn('display_name', 'string', array('nullable' => false));
        $newtable->addColumn('logged_in_at', 'datetime', array('nullable' => true));
        $newtable->setPrimaryKey(array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table, $newtable);

        $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
        $this->assertEquals(array('twitterid', 'displayname'), array_keys($tableDiff->renamedColumns));
        $this->assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
        $this->assertEquals(0, count($tableDiff->removedColumns));
    }
755 756


757 758 759 760 761 762 763
    /**
     * @group DBAL-112
     */
    public function testChangedSequence()
    {
        $schema = new Schema();
        $sequence = $schema->createSequence('baz');
764

765 766 767
        $schemaNew = clone $schema;
        /* @var $schemaNew Schema */
        $schemaNew->getSequence('baz')->setAllocationSize(20);
768

769 770
        $c = new \Doctrine\DBAL\Schema\Comparator;
        $diff = $c->compare($schema, $schemaNew);
771

772 773
        $this->assertSame($diff->changedSequences[0] , $schemaNew->getSequence('baz'));
    }
774

775 776 777 778 779 780 781 782 783 784 785 786 787 788
    /**
     * @group DBAL-106
     */
    public function testDiffDecimalWithNullPrecision()
    {
        $column = new Column('foo', Type::getType('decimal'));
        $column->setPrecision(null);

        $column2 = new Column('foo', Type::getType('decimal'));

        $c = new Comparator();
        $this->assertEquals(array(), $c->diffColumn($column, $column2));
    }

789 790 791 792 793 794
    /**
     * @group DBAL-204
     */
    public function testFqnSchemaComparision()
    {
        $config = new SchemaConfig();
795 796
        $config->setName("foo");

797 798 799
        $oldSchema = new Schema(array(), array(), $config);
        $oldSchema->createTable('bar');

800
        $newSchema= new Schema(array(), array(), $config);
801 802
        $newSchema->createTable('foo.bar');

803 804
        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;
Marco Pivetta's avatar
Marco Pivetta committed
805
        $expected->newNamespaces['foo'] = 'foo';
806 807

        $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
808 809
    }

Marco Pivetta's avatar
Marco Pivetta committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
    /**
     * @group DBAL-669
     */
    public function testNamespacesComparison()
    {
        $config = new SchemaConfig();
        $config->setName("schemaName");

        $oldSchema = new Schema(array(), array(), $config);
        $oldSchema->createTable('taz');
        $oldSchema->createTable('war.tab');

        $newSchema= new Schema(array(), array(), $config);
        $newSchema->createTable('bar.tab');
        $newSchema->createTable('baz.tab');
        $newSchema->createTable('war.tab');

        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;
        $expected->newNamespaces = array('bar' => 'bar', 'baz' => 'baz');

        $diff = Comparator::compareSchemas($oldSchema, $newSchema);

        $this->assertEquals(array('bar' => 'bar', 'baz' => 'baz'), $diff->newNamespaces);
        $this->assertCount(2, $diff->newTables);
    }

837 838 839
    /**
     * @group DBAL-204
     */
840
    public function testFqnSchemaComparisionDifferentSchemaNameButSameTableNoDiff()
841 842
    {
        $config = new SchemaConfig();
843 844
        $config->setName("foo");

845
        $oldSchema = new Schema(array(), array(), $config);
846
        $oldSchema->createTable('foo.bar');
847 848

        $newSchema = new Schema();
849
        $newSchema->createTable('bar');
850

851 852 853 854
        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;

        $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
855 856 857 858 859 860 861 862
    }

    /**
     * @group DBAL-204
     */
    public function testFqnSchemaComparisionNoSchemaSame()
    {
        $config = new SchemaConfig();
863
        $config->setName("foo");
864 865 866 867 868 869
        $oldSchema = new Schema(array(), array(), $config);
        $oldSchema->createTable('bar');

        $newSchema = new Schema();
        $newSchema->createTable('bar');

870 871
        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;
872

873
        $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
874 875
    }

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
    /**
     * @group DDC-1657
     */
    public function testAutoIncremenetSequences()
    {
        $oldSchema = new Schema();
        $table = $oldSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));
        $oldSchema->createSequence("foo_id_seq");

        $newSchema = new Schema();
        $table = $newSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));

        $c = new Comparator();
        $diff = $c->compare($oldSchema, $newSchema);

        $this->assertCount(0, $diff->removedSequences);
    }

898

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    /**
     * Check that added autoincrement sequence is not populated in newSequences
     * @group DBAL-562
     */
    public function testAutoIncremenetNoSequences()
    {
        $oldSchema = new Schema();
        $table = $oldSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));

        $newSchema = new Schema();
        $table = $newSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));
        $newSchema->createSequence("foo_id_seq");

        $c = new Comparator();
        $diff = $c->compare($oldSchema, $newSchema);

        $this->assertCount(0, $diff->newSequences);
    }
921
    /**
922 923 924 925
     * You can get multiple drops for a FK when a table referenced by a foreign
     * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys
     * array because of the dropped table, and once on changedTables array. We
     * now check that the key is present once.
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
     */
    public function testAvoidMultipleDropForeignKey()
    {
        $oldSchema = new Schema();

        $tableForeign = $oldSchema->createTable('foreign');
        $tableForeign->addColumn('id', 'integer');

        $table = $oldSchema->createTable('foo');
        $table->addColumn('fk', 'integer');
        $table->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));


        $newSchema = new Schema();
        $table = $newSchema->createTable('foo');

        $c = new Comparator();
        $diff = $c->compare($oldSchema, $newSchema);

        $this->assertCount(0, $diff->changedTables['foo']->removedForeignKeys);
946
        $this->assertCount(1, $diff->orphanedForeignKeys);
947 948
    }

949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    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));
    }
970

Steve Müller's avatar
Steve Müller committed
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    public function testCompareChangedBinaryColumn()
    {
        $oldSchema = new Schema();

        $tableFoo = $oldSchema->createTable('foo');
        $tableFoo->addColumn('id', 'binary');

        $newSchema = new Schema();
        $table = $newSchema->createTable('foo');
        $table->addColumn('id', 'binary', array('length' => 42, 'fixed' => true));

        $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('length', 'fixed');

        $this->assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
    }

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    /**
     * @group DBAL-617
     */
    public function testCompareQuotedAndUnquotedForeignKeyColumns()
    {
        $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
        $fk2 = new ForeignKeyConstraint(array("`foo`"), "bar", array("`baz`"), "fk1", array('onDelete' => 'NO ACTION'));

        $comparator = new Comparator();
        $diff = $comparator->diffForeignKey($fk1, $fk2);

        $this->assertFalse($diff);
    }

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
    /**
     * @param SchemaDiff $diff
     * @param int $newTableCount
     * @param int $changeTableCount
     * @param int $removeTableCount
     */
    public function assertSchemaTableChangeCount($diff, $newTableCount=0, $changeTableCount=0, $removeTableCount=0)
    {
        $this->assertEquals($newTableCount, count($diff->newTables));
        $this->assertEquals($changeTableCount, count($diff->changedTables));
        $this->assertEquals($removeTableCount, count($diff->removedTables));
    }
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031

    /**
     * @param SchemaDiff $diff
     * @param int $newSequenceCount
     * @param int $changeSequenceCount
     * @param int $changeSequenceCount
     */
    public function assertSchemaSequenceChangeCount($diff, $newSequenceCount=0, $changeSequenceCount=0, $removeSequenceCount=0)
    {
        $this->assertEquals($newSequenceCount, count($diff->newSequences), "Expected number of new sequences is wrong.");
        $this->assertEquals($changeSequenceCount, count($diff->changedSequences), "Expected number of changed sequences is wrong.");
        $this->assertEquals($removeSequenceCount, count($diff->removedSequences), "Expected number of removed sequences is wrong.");
    }
1032 1033 1034

    public function testDiffColumnPlatformOptions()
    {
1035 1036 1037
        $column1 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'bar')));
        $column2 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'foobar' => 'foobar')));
        $column3 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'rab')));
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        $column4 = new Column('foo', Type::getType('string'));

        $comparator = new Comparator();

        $this->assertEquals(array(), $comparator->diffColumn($column1, $column2));
        $this->assertEquals(array(), $comparator->diffColumn($column2, $column1));
        $this->assertEquals(array('bar'), $comparator->diffColumn($column1, $column3));
        $this->assertEquals(array('bar'), $comparator->diffColumn($column3, $column1));
        $this->assertEquals(array(), $comparator->diffColumn($column1, $column4));
        $this->assertEquals(array(), $comparator->diffColumn($column4, $column1));
    }
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

    public function testComplexDiffColumn()
    {
        $column1 = new Column('foo', Type::getType('string'), array(
            'platformOptions' => array('foo' => 'foo'),
            'customSchemaOptions' => array('foo' => 'bar'),
        ));

        $column2 = new Column('foo', Type::getType('string'), array(
            'platformOptions' => array('foo' => 'bar'),
        ));

        $comparator = new Comparator();

        $this->assertEquals(array(), $comparator->diffColumn($column1, $column2));
        $this->assertEquals(array(), $comparator->diffColumn($column2, $column1));
    }
1066
}