ComparatorTest.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Tests\DBAL\Schema;
  20. require_once __DIR__ . '/../../TestInit.php';
  21. use Doctrine\DBAL\Schema\Schema,
  22. Doctrine\DBAL\Schema\Table,
  23. Doctrine\DBAL\Schema\Column,
  24. Doctrine\DBAL\Schema\Index,
  25. Doctrine\DBAL\Schema\Sequence,
  26. Doctrine\DBAL\Schema\SchemaDiff,
  27. Doctrine\DBAL\Schema\TableDiff,
  28. Doctrine\DBAL\Schema\Comparator,
  29. Doctrine\DBAL\Types\Type,
  30. Doctrine\DBAL\Schema\ForeignKeyConstraint;
  31. /**
  32. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  33. * @link www.doctrine-project.org
  34. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  35. * @license http://ez.no/licenses/new_bsd New BSD License
  36. * @since 2.0
  37. * @version $Revision$
  38. * @author Benjamin Eberlei <kontakt@beberlei.de>
  39. */
  40. class ComparatorTest extends \PHPUnit_Framework_TestCase
  41. {
  42. public function testCompareSame1()
  43. {
  44. $schema1 = new Schema( array(
  45. 'bugdb' => new Table('bugdb',
  46. array (
  47. 'integerfield1' => new Column('integerfield1', Type::getType('integer' ) ),
  48. )
  49. ),
  50. ) );
  51. $schema2 = new Schema( array(
  52. 'bugdb' => new Table('bugdb',
  53. array (
  54. 'integerfield1' => new Column('integerfield1', Type::getType('integer') ),
  55. )
  56. ),
  57. ) );
  58. $this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) );
  59. }
  60. public function testCompareSame2()
  61. {
  62. $schema1 = new Schema( array(
  63. 'bugdb' => new Table('bugdb',
  64. array (
  65. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  66. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  67. )
  68. ),
  69. ) );
  70. $schema2 = new Schema( array(
  71. 'bugdb' => new Table('bugdb',
  72. array (
  73. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  74. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  75. )
  76. ),
  77. ) );
  78. $this->assertEquals(new SchemaDiff(), Comparator::compareSchemas( $schema1, $schema2 ) );
  79. }
  80. public function testCompareMissingTable()
  81. {
  82. $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
  83. $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
  84. $table->setSchemaConfig($schemaConfig);
  85. $schema1 = new Schema( array($table), array(), $schemaConfig );
  86. $schema2 = new Schema( array(), array(), $schemaConfig );
  87. $expected = new SchemaDiff( array(), array(), array('bugdb' => $table) );
  88. $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
  89. }
  90. public function testCompareNewTable()
  91. {
  92. $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
  93. $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
  94. $table->setSchemaConfig($schemaConfig);
  95. $schema1 = new Schema( array(), array(), $schemaConfig );
  96. $schema2 = new Schema( array($table), array(), $schemaConfig );
  97. $expected = new SchemaDiff( array('bugdb' => $table), array(), array() );
  98. $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
  99. }
  100. public function testCompareOnlyAutoincrementChanged()
  101. {
  102. $column1 = new Column('foo', Type::getType('integer'), array('autoincrement' => true));
  103. $column2 = new Column('foo', Type::getType('integer'), array('autoincrement' => false));
  104. $comparator = new Comparator();
  105. $changedProperties = $comparator->diffColumn($column1, $column2);
  106. $this->assertEquals(array('autoincrement'), $changedProperties);
  107. }
  108. public function testCompareMissingField()
  109. {
  110. $missingColumn = new Column('integerfield1', Type::getType('integer'));
  111. $schema1 = new Schema( array(
  112. 'bugdb' => new Table('bugdb',
  113. array (
  114. 'integerfield1' => $missingColumn,
  115. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  116. )
  117. ),
  118. ) );
  119. $schema2 = new Schema( array(
  120. 'bugdb' => new Table('bugdb',
  121. array (
  122. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  123. )
  124. ),
  125. ) );
  126. $expected = new SchemaDiff ( array(),
  127. array (
  128. 'bugdb' => new TableDiff( 'bugdb', array(), array(),
  129. array (
  130. 'integerfield1' => $missingColumn,
  131. )
  132. )
  133. )
  134. );
  135. $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
  136. }
  137. public function testCompareNewField()
  138. {
  139. $schema1 = new Schema( array(
  140. 'bugdb' => new Table('bugdb',
  141. array (
  142. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  143. )
  144. ),
  145. ) );
  146. $schema2 = new Schema( array(
  147. 'bugdb' => new Table('bugdb',
  148. array (
  149. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  150. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  151. )
  152. ),
  153. ) );
  154. $expected = new SchemaDiff ( array(),
  155. array (
  156. 'bugdb' => new TableDiff ('bugdb',
  157. array (
  158. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  159. )
  160. ),
  161. )
  162. );
  163. $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
  164. }
  165. public function testCompareChangedColumns_ChangeType()
  166. {
  167. $column1 = new Column('charfield1', Type::getType('string'));
  168. $column2 = new Column('charfield1', Type::getType('integer'));
  169. $c = new Comparator();
  170. $this->assertEquals(array('type'), $c->diffColumn($column1, $column2));
  171. $this->assertEquals(array(), $c->diffColumn($column1, $column1));
  172. }
  173. public function testCompareRemovedIndex()
  174. {
  175. $schema1 = new Schema( array(
  176. 'bugdb' => new Table('bugdb',
  177. array (
  178. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  179. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  180. ),
  181. array (
  182. 'primary' => new Index('primary',
  183. array(
  184. 'integerfield1'
  185. ),
  186. true
  187. )
  188. )
  189. ),
  190. ) );
  191. $schema2 = new Schema( array(
  192. 'bugdb' => new Table('bugdb',
  193. array (
  194. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  195. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  196. )
  197. ),
  198. ) );
  199. $expected = new SchemaDiff ( array(),
  200. array (
  201. 'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(), array(),
  202. array (
  203. 'primary' => new Index('primary',
  204. array(
  205. 'integerfield1'
  206. ),
  207. true
  208. )
  209. )
  210. ),
  211. )
  212. );
  213. $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
  214. }
  215. public function testCompareNewIndex()
  216. {
  217. $schema1 = new Schema( array(
  218. 'bugdb' => new Table('bugdb',
  219. array (
  220. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  221. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  222. )
  223. ),
  224. ) );
  225. $schema2 = new Schema( array(
  226. 'bugdb' => new Table('bugdb',
  227. array (
  228. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  229. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  230. ),
  231. array (
  232. 'primary' => new Index('primary',
  233. array(
  234. 'integerfield1'
  235. ),
  236. true
  237. )
  238. )
  239. ),
  240. ) );
  241. $expected = new SchemaDiff ( array(),
  242. array (
  243. 'bugdb' => new TableDiff( 'bugdb', array(), array(), array(),
  244. array (
  245. 'primary' => new Index('primary',
  246. array(
  247. 'integerfield1'
  248. ),
  249. true
  250. )
  251. )
  252. ),
  253. )
  254. );
  255. $this->assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
  256. }
  257. public function testCompareChangedIndex()
  258. {
  259. $schema1 = new Schema( array(
  260. 'bugdb' => new Table('bugdb',
  261. array (
  262. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  263. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  264. ),
  265. array (
  266. 'primary' => new Index('primary',
  267. array(
  268. 'integerfield1'
  269. ),
  270. true
  271. )
  272. )
  273. ),
  274. ) );
  275. $schema2 = new Schema( array(
  276. 'bugdb' => new Table('bugdb',
  277. array (
  278. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  279. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  280. ),
  281. array (
  282. 'primary' => new Index('primary',
  283. array('integerfield1', 'integerfield2'),
  284. true
  285. )
  286. )
  287. ),
  288. ) );
  289. $expected = new SchemaDiff ( array(),
  290. array (
  291. 'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(),
  292. array (
  293. 'primary' => new Index('primary',
  294. array(
  295. 'integerfield1',
  296. 'integerfield2'
  297. ),
  298. true
  299. )
  300. )
  301. ),
  302. )
  303. );
  304. $actual = Comparator::compareSchemas( $schema1, $schema2 );
  305. $this->assertEquals($expected, $actual);
  306. }
  307. public function testCompareChangedIndexFieldPositions()
  308. {
  309. $schema1 = new Schema( array(
  310. 'bugdb' => new Table('bugdb',
  311. array (
  312. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  313. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  314. ),
  315. array (
  316. 'primary' => new Index('primary', array('integerfield1', 'integerfield2'), true)
  317. )
  318. ),
  319. ) );
  320. $schema2 = new Schema( array(
  321. 'bugdb' => new Table('bugdb',
  322. array (
  323. 'integerfield1' => new Column('integerfield1', Type::getType('integer')),
  324. 'integerfield2' => new Column('integerfield2', Type::getType('integer')),
  325. ),
  326. array (
  327. 'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
  328. )
  329. ),
  330. ) );
  331. $expected = new SchemaDiff ( array(),
  332. array (
  333. 'bugdb' => new TableDiff('bugdb', array(), array(), array(), array(),
  334. array (
  335. 'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
  336. )
  337. ),
  338. )
  339. );
  340. $actual = Comparator::compareSchemas( $schema1, $schema2 );
  341. $this->assertEquals($expected, $actual);
  342. }
  343. public function testCompareSequences()
  344. {
  345. $seq1 = new Sequence('foo', 1, 1);
  346. $seq2 = new Sequence('foo', 1, 2);
  347. $seq3 = new Sequence('foo', 2, 1);
  348. $c = new Comparator();
  349. $this->assertTrue($c->diffSequence($seq1, $seq2));
  350. $this->assertTrue($c->diffSequence($seq1, $seq3));
  351. }
  352. public function testRemovedSequence()
  353. {
  354. $schema1 = new Schema();
  355. $seq = $schema1->createSequence('foo');
  356. $schema2 = new Schema();
  357. $c = new Comparator();
  358. $diffSchema = $c->compare($schema1, $schema2);
  359. $this->assertEquals(1, count($diffSchema->removedSequences));
  360. $this->assertSame($seq, $diffSchema->removedSequences[0]);
  361. }
  362. public function testAddedSequence()
  363. {
  364. $schema1 = new Schema();
  365. $schema2 = new Schema();
  366. $seq = $schema2->createSequence('foo');
  367. $c = new Comparator();
  368. $diffSchema = $c->compare($schema1, $schema2);
  369. $this->assertEquals(1, count($diffSchema->newSequences));
  370. $this->assertSame($seq, $diffSchema->newSequences[0]);
  371. }
  372. public function testTableAddForeignKey()
  373. {
  374. $tableForeign = new Table("bar");
  375. $tableForeign->addColumn('id', 'integer');
  376. $table1 = new Table("foo");
  377. $table1->addColumn('fk', 'integer');
  378. $table2 = new Table("foo");
  379. $table2->addColumn('fk', 'integer');
  380. $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
  381. $c = new Comparator();
  382. $tableDiff = $c->diffTable($table1, $table2);
  383. $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
  384. $this->assertEquals(1, count($tableDiff->addedForeignKeys));
  385. }
  386. public function testTableRemoveForeignKey()
  387. {
  388. $tableForeign = new Table("bar");
  389. $tableForeign->addColumn('id', 'integer');
  390. $table1 = new Table("foo");
  391. $table1->addColumn('fk', 'integer');
  392. $table2 = new Table("foo");
  393. $table2->addColumn('fk', 'integer');
  394. $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
  395. $c = new Comparator();
  396. $tableDiff = $c->diffTable($table2, $table1);
  397. $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
  398. $this->assertEquals(1, count($tableDiff->removedForeignKeys));
  399. }
  400. public function testTableUpdateForeignKey()
  401. {
  402. $tableForeign = new Table("bar");
  403. $tableForeign->addColumn('id', 'integer');
  404. $table1 = new Table("foo");
  405. $table1->addColumn('fk', 'integer');
  406. $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
  407. $table2 = new Table("foo");
  408. $table2->addColumn('fk', 'integer');
  409. $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE'));
  410. $c = new Comparator();
  411. $tableDiff = $c->diffTable($table1, $table2);
  412. $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
  413. $this->assertEquals(1, count($tableDiff->changedForeignKeys));
  414. }
  415. public function testTablesCaseInsensitive()
  416. {
  417. $schemaA = new Schema();
  418. $schemaA->createTable('foo');
  419. $schemaA->createTable('bAr');
  420. $schemaA->createTable('BAZ');
  421. $schemaA->createTable('new');
  422. $schemaB = new Schema();
  423. $schemaB->createTable('FOO');
  424. $schemaB->createTable('bar');
  425. $schemaB->createTable('Baz');
  426. $schemaB->createTable('old');
  427. $c = new Comparator();
  428. $diff = $c->compare($schemaA, $schemaB);
  429. $this->assertSchemaTableChangeCount($diff, 1, 0, 1);
  430. }
  431. public function testSequencesCaseInsenstive()
  432. {
  433. $schemaA = new Schema();
  434. $schemaA->createSequence('foo');
  435. $schemaA->createSequence('BAR');
  436. $schemaA->createSequence('Baz');
  437. $schemaA->createSequence('new');
  438. $schemaB = new Schema();
  439. $schemaB->createSequence('FOO');
  440. $schemaB->createSequence('Bar');
  441. $schemaB->createSequence('baz');
  442. $schemaB->createSequence('old');
  443. $c = new Comparator();
  444. $diff = $c->compare($schemaA, $schemaB);
  445. $this->assertSchemaSequenceChangeCount($diff, 1, 0, 1);
  446. }
  447. public function testCompareColumnCompareCaseInsensitive()
  448. {
  449. $tableA = new Table("foo");
  450. $tableA->addColumn('id', 'integer');
  451. $tableB = new Table("foo");
  452. $tableB->addColumn('ID', 'integer');
  453. $c = new Comparator();
  454. $tableDiff = $c->diffTable($tableA, $tableB);
  455. $this->assertFalse($tableDiff);
  456. }
  457. public function testCompareIndexBasedOnPropertiesNotName()
  458. {
  459. $tableA = new Table("foo");
  460. $tableA->addColumn('id', 'integer');
  461. $tableA->addIndex(array("id"), "foo_bar_idx");
  462. $tableB = new Table("foo");
  463. $tableB->addColumn('ID', 'integer');
  464. $tableB->addIndex(array("id"), "bar_foo_idx");
  465. $c = new Comparator();
  466. $tableDiff = $c->diffTable($tableA, $tableB);
  467. $this->assertFalse($tableDiff);
  468. }
  469. public function testCompareForeignKeyBasedOnPropertiesNotName()
  470. {
  471. $tableA = new Table("foo");
  472. $tableA->addColumn('id', 'integer');
  473. $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('id'));
  474. $tableB = new Table("foo");
  475. $tableB->addColumn('ID', 'integer');
  476. $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('id'));
  477. $c = new Comparator();
  478. $tableDiff = $c->diffTable($tableA, $tableB);
  479. $this->assertFalse($tableDiff);
  480. }
  481. public function testCompareForeignKey_RestrictNoAction_AreTheSame()
  482. {
  483. $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
  484. $fk2 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'RESTRICT'));
  485. $c = new Comparator();
  486. $this->assertFalse($c->diffForeignKey($fk1, $fk2));
  487. }
  488. public function testDetectRenameColumn()
  489. {
  490. $tableA = new Table("foo");
  491. $tableA->addColumn('foo', 'integer');
  492. $tableB = new Table("foo");
  493. $tableB->addColumn('bar', 'integer');
  494. $c = new Comparator();
  495. $tableDiff = $c->diffTable($tableA, $tableB);
  496. $this->assertEquals(0, count($tableDiff->addedColumns));
  497. $this->assertEquals(0, count($tableDiff->removedColumns));
  498. $this->assertArrayHasKey('foo', $tableDiff->renamedColumns);
  499. $this->assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
  500. }
  501. /**
  502. * You can easily have ambiguouties in the column renaming. If these
  503. * are detected no renaming should take place, instead adding and dropping
  504. * should be used exclusively.
  505. *
  506. * @group DBAL-24
  507. */
  508. public function testDetectRenameColumnAmbiguous()
  509. {
  510. $tableA = new Table("foo");
  511. $tableA->addColumn('foo', 'integer');
  512. $tableA->addColumn('bar', 'integer');
  513. $tableB = new Table("foo");
  514. $tableB->addColumn('baz', 'integer');
  515. $c = new Comparator();
  516. $tableDiff = $c->diffTable($tableA, $tableB);
  517. $this->assertEquals(1, count($tableDiff->addedColumns), "'baz' should be added, not created through renaming!");
  518. $this->assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
  519. $this->assertEquals(2, count($tableDiff->removedColumns), "'foo' and 'bar' should both be dropped, an ambigouty exists which one could be renamed to 'baz'.");
  520. $this->assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed.");
  521. $this->assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed.");
  522. $this->assertEquals(0, count($tableDiff->renamedColumns), "no renamings should take place.");
  523. }
  524. public function testDetectChangeIdentifierType()
  525. {
  526. $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');
  527. $tableA = new Table("foo");
  528. $tableA->addColumn('id', 'integer', array('autoincrement' => false));
  529. $tableB = new Table("foo");
  530. $tableB->addColumn('id', 'integer', array('autoincrement' => true));
  531. $c = new Comparator();
  532. $tableDiff = $c->diffTable($tableA, $tableB);
  533. $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
  534. $this->assertArrayHasKey('id', $tableDiff->changedColumns);
  535. }
  536. /**
  537. * @group DBAL-105
  538. */
  539. public function testDiff()
  540. {
  541. $table = new \Doctrine\DBAL\Schema\Table('twitter_users');
  542. $table->addColumn('id', 'integer', array('autoincrement' => true));
  543. $table->addColumn('twitterId', 'integer', array('nullable' => false));
  544. $table->addColumn('displayName', 'string', array('nullable' => false));
  545. $table->setPrimaryKey(array('id'));
  546. $newtable = new \Doctrine\DBAL\Schema\Table('twitter_users');
  547. $newtable->addColumn('id', 'integer', array('autoincrement' => true));
  548. $newtable->addColumn('twitter_id', 'integer', array('nullable' => false));
  549. $newtable->addColumn('display_name', 'string', array('nullable' => false));
  550. $newtable->addColumn('logged_in_at', 'datetime', array('nullable' => true));
  551. $newtable->setPrimaryKey(array('id'));
  552. $c = new Comparator();
  553. $tableDiff = $c->diffTable($table, $newtable);
  554. $this->assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
  555. $this->assertEquals(array('twitterid', 'displayname'), array_keys($tableDiff->renamedColumns));
  556. $this->assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
  557. $this->assertEquals(0, count($tableDiff->removedColumns));
  558. }
  559. /**
  560. * @group DBAL-112
  561. */
  562. public function testChangedSequence()
  563. {
  564. $schema = new Schema();
  565. $sequence = $schema->createSequence('baz');
  566. $schemaNew = clone $schema;
  567. /* @var $schemaNew Schema */
  568. $schemaNew->getSequence('baz')->setAllocationSize(20);
  569. $c = new \Doctrine\DBAL\Schema\Comparator;
  570. $diff = $c->compare($schema, $schemaNew);
  571. $this->assertSame($diff->changedSequences[0] , $schemaNew->getSequence('baz'));
  572. }
  573. /**
  574. * @group DBAL-106
  575. */
  576. public function testDiffDecimalWithNullPrecision()
  577. {
  578. $column = new Column('foo', Type::getType('decimal'));
  579. $column->setPrecision(null);
  580. $column2 = new Column('foo', Type::getType('decimal'));
  581. $c = new Comparator();
  582. $this->assertEquals(array(), $c->diffColumn($column, $column2));
  583. }
  584. /**
  585. * @param SchemaDiff $diff
  586. * @param int $newTableCount
  587. * @param int $changeTableCount
  588. * @param int $removeTableCount
  589. */
  590. public function assertSchemaTableChangeCount($diff, $newTableCount=0, $changeTableCount=0, $removeTableCount=0)
  591. {
  592. $this->assertEquals($newTableCount, count($diff->newTables));
  593. $this->assertEquals($changeTableCount, count($diff->changedTables));
  594. $this->assertEquals($removeTableCount, count($diff->removedTables));
  595. }
  596. /**
  597. * @param SchemaDiff $diff
  598. * @param int $newSequenceCount
  599. * @param int $changeSequenceCount
  600. * @param int $changeSequenceCount
  601. */
  602. public function assertSchemaSequenceChangeCount($diff, $newSequenceCount=0, $changeSequenceCount=0, $removeSequenceCount=0)
  603. {
  604. $this->assertEquals($newSequenceCount, count($diff->newSequences), "Expected number of new sequences is wrong.");
  605. $this->assertEquals($changeSequenceCount, count($diff->changedSequences), "Expected number of changed sequences is wrong.");
  606. $this->assertEquals($removeSequenceCount, count($diff->removedSequences), "Expected number of removed sequences is wrong.");
  607. }
  608. }