PostgresSchemaTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Schema;
  16. use Cake\Core\Configure;
  17. use Cake\Database\Schema\Collection as SchemaCollection;
  18. use Cake\Database\Schema\PostgresSchema;
  19. use Cake\Database\Schema\Table;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Postgres schema test case.
  24. */
  25. class PostgresSchemaTest extends TestCase {
  26. /**
  27. * Helper method for skipping tests that need a real connection.
  28. *
  29. * @return void
  30. */
  31. protected function _needsConnection() {
  32. $config = ConnectionManager::config('test');
  33. $this->skipIf(strpos($config['driver'], 'Postgres') === false, 'Not using Postgres for test config');
  34. }
  35. /**
  36. * Helper method for testing methods.
  37. *
  38. * @param \Cake\Database\Connection $connection
  39. * @return void
  40. */
  41. protected function _createTables($connection) {
  42. $this->_needsConnection();
  43. $connection->execute('DROP TABLE IF EXISTS schema_articles');
  44. $connection->execute('DROP TABLE IF EXISTS schema_authors');
  45. $table = <<<SQL
  46. CREATE TABLE schema_authors (
  47. id SERIAL,
  48. name VARCHAR(50),
  49. bio DATE,
  50. position INT,
  51. created TIMESTAMP,
  52. PRIMARY KEY (id),
  53. CONSTRAINT "unique_position" UNIQUE ("position")
  54. )
  55. SQL;
  56. $connection->execute($table);
  57. $table = <<<SQL
  58. CREATE TABLE schema_articles (
  59. id BIGINT PRIMARY KEY,
  60. title VARCHAR(20),
  61. body TEXT,
  62. author_id INTEGER NOT NULL,
  63. published BOOLEAN DEFAULT false,
  64. views SMALLINT DEFAULT 0,
  65. created TIMESTAMP,
  66. CONSTRAINT "content_idx" UNIQUE ("title", "body"),
  67. CONSTRAINT "author_idx" FOREIGN KEY ("author_id") REFERENCES "schema_authors" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
  68. )
  69. SQL;
  70. $connection->execute($table);
  71. $connection->execute('COMMENT ON COLUMN "schema_articles"."title" IS \'a title\'');
  72. $connection->execute('CREATE INDEX "author_idx" ON "schema_articles" ("author_id")');
  73. }
  74. /**
  75. * Data provider for convert column testing
  76. *
  77. * @return array
  78. */
  79. public static function convertColumnProvider() {
  80. return [
  81. // Timestamp
  82. [
  83. 'TIMESTAMP',
  84. ['type' => 'timestamp', 'length' => null]
  85. ],
  86. [
  87. 'TIMESTAMP WITHOUT TIME ZONE',
  88. ['type' => 'timestamp', 'length' => null]
  89. ],
  90. // Date & time
  91. [
  92. 'DATE',
  93. ['type' => 'date', 'length' => null]
  94. ],
  95. [
  96. 'TIME',
  97. ['type' => 'time', 'length' => null]
  98. ],
  99. // Integer
  100. [
  101. 'SMALLINT',
  102. ['type' => 'integer', 'length' => 5]
  103. ],
  104. [
  105. 'INTEGER',
  106. ['type' => 'integer', 'length' => 10]
  107. ],
  108. [
  109. 'SERIAL',
  110. ['type' => 'integer', 'length' => 10]
  111. ],
  112. [
  113. 'BIGINT',
  114. ['type' => 'biginteger', 'length' => 20]
  115. ],
  116. [
  117. 'BIGSERIAL',
  118. ['type' => 'biginteger', 'length' => 20]
  119. ],
  120. // Decimal
  121. [
  122. 'NUMERIC',
  123. ['type' => 'decimal', 'length' => null]
  124. ],
  125. [
  126. 'DECIMAL(10,2)',
  127. ['type' => 'decimal', 'length' => null]
  128. ],
  129. [
  130. 'MONEY',
  131. ['type' => 'decimal', 'length' => null]
  132. ],
  133. // String
  134. [
  135. 'VARCHAR',
  136. ['type' => 'string', 'length' => null]
  137. ],
  138. [
  139. 'VARCHAR(10)',
  140. ['type' => 'string', 'length' => 10]
  141. ],
  142. [
  143. 'CHARACTER VARYING',
  144. ['type' => 'string', 'length' => null]
  145. ],
  146. [
  147. 'CHARACTER VARYING(10)',
  148. ['type' => 'string', 'length' => 10]
  149. ],
  150. [
  151. 'CHAR(10)',
  152. ['type' => 'string', 'fixed' => true, 'length' => 10]
  153. ],
  154. [
  155. 'CHARACTER(10)',
  156. ['type' => 'string', 'fixed' => true, 'length' => 10]
  157. ],
  158. // UUID
  159. [
  160. 'UUID',
  161. ['type' => 'uuid', 'length' => null]
  162. ],
  163. [
  164. 'INET',
  165. ['type' => 'string', 'length' => 39]
  166. ],
  167. // Text
  168. [
  169. 'TEXT',
  170. ['type' => 'text', 'length' => null]
  171. ],
  172. // Blob
  173. [
  174. 'BYTEA',
  175. ['type' => 'binary', 'length' => null]
  176. ],
  177. // Float
  178. [
  179. 'REAL',
  180. ['type' => 'float', 'length' => null]
  181. ],
  182. [
  183. 'DOUBLE PRECISION',
  184. ['type' => 'float', 'length' => null]
  185. ],
  186. ];
  187. }
  188. /**
  189. * Test parsing Postgres column types from field description.
  190. *
  191. * @dataProvider convertColumnProvider
  192. * @return void
  193. */
  194. public function testConvertColumn($type, $expected) {
  195. $field = [
  196. 'name' => 'field',
  197. 'type' => $type,
  198. 'null' => 'YES',
  199. 'default' => 'Default value',
  200. 'comment' => 'Comment section',
  201. 'char_length' => null,
  202. ];
  203. $expected += [
  204. 'null' => true,
  205. 'default' => 'Default value',
  206. 'comment' => 'Comment section',
  207. ];
  208. $driver = $this->getMock('Cake\Database\Driver\Postgres');
  209. $dialect = new PostgresSchema($driver);
  210. $table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
  211. $table->expects($this->at(0))->method('addColumn')->with('field', $expected);
  212. $dialect->convertColumnDescription($table, $field);
  213. }
  214. /**
  215. * Test listing tables with Postgres
  216. *
  217. * @return void
  218. */
  219. public function testListTables() {
  220. $connection = ConnectionManager::get('test');
  221. $this->_createTables($connection);
  222. $schema = new SchemaCollection($connection);
  223. $result = $schema->listTables();
  224. $this->assertInternalType('array', $result);
  225. $this->assertContains('schema_articles', $result);
  226. $this->assertContains('schema_authors', $result);
  227. }
  228. /**
  229. * Test that describe accepts tablenames containing `schema.table`.
  230. *
  231. * @return void
  232. */
  233. public function testDescribeWithSchemaName() {
  234. $connection = ConnectionManager::get('test');
  235. $this->_createTables($connection);
  236. $schema = new SchemaCollection($connection);
  237. $result = $schema->describe('public.schema_articles');
  238. $this->assertEquals(['id'], $result->primaryKey());
  239. $this->assertEquals('schema_articles', $result->name());
  240. }
  241. /**
  242. * Test describing a table with Postgres
  243. *
  244. * @return void
  245. */
  246. public function testDescribeTable() {
  247. $connection = ConnectionManager::get('test');
  248. $this->_createTables($connection);
  249. $schema = new SchemaCollection($connection);
  250. $result = $schema->describe('schema_articles');
  251. $expected = [
  252. 'id' => [
  253. 'type' => 'biginteger',
  254. 'null' => false,
  255. 'default' => null,
  256. 'length' => 20,
  257. 'precision' => null,
  258. 'unsigned' => null,
  259. 'comment' => null,
  260. 'autoIncrement' => true,
  261. ],
  262. 'title' => [
  263. 'type' => 'string',
  264. 'null' => true,
  265. 'default' => null,
  266. 'length' => 20,
  267. 'precision' => null,
  268. 'comment' => 'a title',
  269. 'fixed' => null,
  270. ],
  271. 'body' => [
  272. 'type' => 'text',
  273. 'null' => true,
  274. 'default' => null,
  275. 'length' => null,
  276. 'precision' => null,
  277. 'comment' => null,
  278. ],
  279. 'author_id' => [
  280. 'type' => 'integer',
  281. 'null' => false,
  282. 'default' => null,
  283. 'length' => 10,
  284. 'precision' => null,
  285. 'unsigned' => null,
  286. 'comment' => null,
  287. 'autoIncrement' => null,
  288. ],
  289. 'published' => [
  290. 'type' => 'boolean',
  291. 'null' => true,
  292. 'default' => 0,
  293. 'length' => null,
  294. 'precision' => null,
  295. 'comment' => null,
  296. ],
  297. 'views' => [
  298. 'type' => 'integer',
  299. 'null' => true,
  300. 'default' => 0,
  301. 'length' => 5,
  302. 'precision' => null,
  303. 'unsigned' => null,
  304. 'comment' => null,
  305. 'autoIncrement' => null,
  306. ],
  307. 'created' => [
  308. 'type' => 'timestamp',
  309. 'null' => true,
  310. 'default' => null,
  311. 'length' => null,
  312. 'precision' => null,
  313. 'comment' => null,
  314. ],
  315. ];
  316. $this->assertEquals(['id'], $result->primaryKey());
  317. foreach ($expected as $field => $definition) {
  318. $this->assertEquals($definition, $result->column($field));
  319. }
  320. }
  321. /**
  322. * Test describing a table with containing keywords
  323. *
  324. * @return void
  325. */
  326. public function testDescribeTableConstraintsWithKeywords() {
  327. $connection = ConnectionManager::get('test');
  328. $this->_createTables($connection);
  329. $schema = new SchemaCollection($connection);
  330. $result = $schema->describe('schema_authors');
  331. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  332. $expected = [
  333. 'primary' => [
  334. 'type' => 'primary',
  335. 'columns' => ['id'],
  336. 'length' => []
  337. ],
  338. 'unique_position' => [
  339. 'type' => 'unique',
  340. 'columns' => ['position'],
  341. 'length' => []
  342. ]
  343. ];
  344. $this->assertCount(2, $result->constraints());
  345. $this->assertEquals($expected['primary'], $result->constraint('primary'));
  346. $this->assertEquals($expected['unique_position'], $result->constraint('unique_position'));
  347. }
  348. /**
  349. * Test describing a table with indexes
  350. *
  351. * @return void
  352. */
  353. public function testDescribeTableIndexes() {
  354. $connection = ConnectionManager::get('test');
  355. $this->_createTables($connection);
  356. $schema = new SchemaCollection($connection);
  357. $result = $schema->describe('schema_articles');
  358. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  359. $expected = [
  360. 'primary' => [
  361. 'type' => 'primary',
  362. 'columns' => ['id'],
  363. 'length' => []
  364. ],
  365. 'content_idx' => [
  366. 'type' => 'unique',
  367. 'columns' => ['title', 'body'],
  368. 'length' => []
  369. ]
  370. ];
  371. $this->assertCount(3, $result->constraints());
  372. $expected = [
  373. 'primary' => [
  374. 'type' => 'primary',
  375. 'columns' => ['id'],
  376. 'length' => []
  377. ],
  378. 'content_idx' => [
  379. 'type' => 'unique',
  380. 'columns' => ['title', 'body'],
  381. 'length' => []
  382. ],
  383. 'author_idx' => [
  384. 'type' => 'foreign',
  385. 'columns' => ['author_id'],
  386. 'references' => ['schema_authors', 'id'],
  387. 'length' => [],
  388. 'update' => 'cascade',
  389. 'delete' => 'restrict',
  390. ]
  391. ];
  392. $this->assertEquals($expected['primary'], $result->constraint('primary'));
  393. $this->assertEquals($expected['content_idx'], $result->constraint('content_idx'));
  394. $this->assertEquals($expected['author_idx'], $result->constraint('author_idx'));
  395. $this->assertCount(1, $result->indexes());
  396. $expected = [
  397. 'type' => 'index',
  398. 'columns' => ['author_id'],
  399. 'length' => []
  400. ];
  401. $this->assertEquals($expected, $result->index('author_idx'));
  402. }
  403. /**
  404. * Column provider for creating column sql
  405. *
  406. * @return array
  407. */
  408. public static function columnSqlProvider() {
  409. return [
  410. // strings
  411. [
  412. 'title',
  413. ['type' => 'string', 'length' => 25, 'null' => false],
  414. '"title" VARCHAR(25) NOT NULL'
  415. ],
  416. [
  417. 'title',
  418. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  419. '"title" VARCHAR(25) DEFAULT NULL'
  420. ],
  421. [
  422. 'id',
  423. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  424. '"id" CHAR(32) NOT NULL'
  425. ],
  426. [
  427. 'id',
  428. ['type' => 'uuid', 'length' => 36, 'null' => false],
  429. '"id" UUID NOT NULL'
  430. ],
  431. [
  432. 'role',
  433. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  434. '"role" VARCHAR(10) NOT NULL DEFAULT "admin"'
  435. ],
  436. [
  437. 'title',
  438. ['type' => 'string'],
  439. '"title" VARCHAR'
  440. ],
  441. // Text
  442. [
  443. 'body',
  444. ['type' => 'text', 'null' => false],
  445. '"body" TEXT NOT NULL'
  446. ],
  447. // Integers
  448. [
  449. 'post_id',
  450. ['type' => 'integer', 'length' => 11],
  451. '"post_id" INTEGER'
  452. ],
  453. [
  454. 'post_id',
  455. ['type' => 'biginteger', 'length' => 20],
  456. '"post_id" BIGINT'
  457. ],
  458. [
  459. 'post_id',
  460. ['type' => 'integer', 'autoIncrement' => true, 'length' => 11],
  461. '"post_id" SERIAL'
  462. ],
  463. [
  464. 'post_id',
  465. ['type' => 'biginteger', 'autoIncrement' => true, 'length' => 20],
  466. '"post_id" BIGSERIAL'
  467. ],
  468. // Decimal
  469. [
  470. 'value',
  471. ['type' => 'decimal'],
  472. '"value" DECIMAL'
  473. ],
  474. [
  475. 'value',
  476. ['type' => 'decimal', 'length' => 11],
  477. '"value" DECIMAL(11,0)'
  478. ],
  479. [
  480. 'value',
  481. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  482. '"value" DECIMAL(12,5)'
  483. ],
  484. // Float
  485. [
  486. 'value',
  487. ['type' => 'float'],
  488. '"value" FLOAT'
  489. ],
  490. [
  491. 'value',
  492. ['type' => 'float', 'length' => 11, 'precision' => 3],
  493. '"value" FLOAT(3)'
  494. ],
  495. // Binary
  496. [
  497. 'img',
  498. ['type' => 'binary'],
  499. '"img" BYTEA'
  500. ],
  501. // Boolean
  502. [
  503. 'checked',
  504. ['type' => 'boolean', 'default' => false],
  505. '"checked" BOOLEAN DEFAULT FALSE'
  506. ],
  507. [
  508. 'checked',
  509. ['type' => 'boolean', 'default' => true, 'null' => false],
  510. '"checked" BOOLEAN NOT NULL DEFAULT TRUE'
  511. ],
  512. // datetimes
  513. [
  514. 'created',
  515. ['type' => 'datetime'],
  516. '"created" TIMESTAMP'
  517. ],
  518. // Date & Time
  519. [
  520. 'start_date',
  521. ['type' => 'date'],
  522. '"start_date" DATE'
  523. ],
  524. [
  525. 'start_time',
  526. ['type' => 'time'],
  527. '"start_time" TIME'
  528. ],
  529. // timestamps
  530. [
  531. 'created',
  532. ['type' => 'timestamp', 'null' => true],
  533. '"created" TIMESTAMP DEFAULT NULL'
  534. ],
  535. ];
  536. }
  537. /**
  538. * Test generating column definitions
  539. *
  540. * @dataProvider columnSqlProvider
  541. * @return void
  542. */
  543. public function testColumnSql($name, $data, $expected) {
  544. $driver = $this->_getMockedDriver();
  545. $schema = new PostgresSchema($driver);
  546. $table = (new Table('schema_articles'))->addColumn($name, $data);
  547. $this->assertEquals($expected, $schema->columnSql($table, $name));
  548. }
  549. /**
  550. * Test generating a column that is a primary key.
  551. *
  552. * @return void
  553. */
  554. public function testColumnSqlPrimaryKey() {
  555. $driver = $this->_getMockedDriver();
  556. $schema = new PostgresSchema($driver);
  557. $table = new Table('schema_articles');
  558. $table->addColumn('id', [
  559. 'type' => 'integer',
  560. 'null' => false
  561. ])
  562. ->addConstraint('primary', [
  563. 'type' => 'primary',
  564. 'columns' => ['id']
  565. ]);
  566. $result = $schema->columnSql($table, 'id');
  567. $this->assertEquals($result, '"id" SERIAL');
  568. }
  569. /**
  570. * Provide data for testing constraintSql
  571. *
  572. * @return array
  573. */
  574. public static function constraintSqlProvider() {
  575. return [
  576. [
  577. 'primary',
  578. ['type' => 'primary', 'columns' => ['title']],
  579. 'PRIMARY KEY ("title")'
  580. ],
  581. [
  582. 'unique_idx',
  583. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  584. 'CONSTRAINT "unique_idx" UNIQUE ("title", "author_id")'
  585. ],
  586. [
  587. 'author_id_idx',
  588. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  589. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  590. 'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
  591. ],
  592. [
  593. 'author_id_idx',
  594. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  595. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  596. 'REFERENCES "authors" ("id") ON UPDATE CASCADE ON DELETE RESTRICT'
  597. ],
  598. [
  599. 'author_id_idx',
  600. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  601. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  602. 'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
  603. ],
  604. [
  605. 'author_id_idx',
  606. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  607. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  608. 'REFERENCES "authors" ("id") ON UPDATE SET NULL ON DELETE RESTRICT'
  609. ],
  610. [
  611. 'author_id_idx',
  612. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  613. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  614. 'REFERENCES "authors" ("id") ON UPDATE NO ACTION ON DELETE RESTRICT'
  615. ],
  616. ];
  617. }
  618. /**
  619. * Test the constraintSql method.
  620. *
  621. * @dataProvider constraintSqlProvider
  622. */
  623. public function testConstraintSql($name, $data, $expected) {
  624. $driver = $this->_getMockedDriver();
  625. $schema = new PostgresSchema($driver);
  626. $table = (new Table('schema_articles'))->addColumn('title', [
  627. 'type' => 'string',
  628. 'length' => 255
  629. ])->addColumn('author_id', [
  630. 'type' => 'integer',
  631. ])->addConstraint($name, $data);
  632. $this->assertTextEquals($expected, $schema->constraintSql($table, $name));
  633. }
  634. /**
  635. * Integration test for converting a Schema\Table into MySQL table creates.
  636. *
  637. * @return void
  638. */
  639. public function testCreateSql() {
  640. $driver = $this->_getMockedDriver();
  641. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  642. $connection->expects($this->any())->method('driver')
  643. ->will($this->returnValue($driver));
  644. $table = (new Table('schema_articles'))->addColumn('id', [
  645. 'type' => 'integer',
  646. 'null' => false
  647. ])
  648. ->addColumn('title', [
  649. 'type' => 'string',
  650. 'null' => false,
  651. 'comment' => 'This is the title',
  652. ])
  653. ->addColumn('body', ['type' => 'text'])
  654. ->addColumn('created', 'datetime')
  655. ->addConstraint('primary', [
  656. 'type' => 'primary',
  657. 'columns' => ['id'],
  658. ])
  659. ->addIndex('title_idx', [
  660. 'type' => 'index',
  661. 'columns' => ['title'],
  662. ]);
  663. $expected = <<<SQL
  664. CREATE TABLE "schema_articles" (
  665. "id" SERIAL,
  666. "title" VARCHAR NOT NULL,
  667. "body" TEXT,
  668. "created" TIMESTAMP,
  669. PRIMARY KEY ("id")
  670. )
  671. SQL;
  672. $result = $table->createSql($connection);
  673. $this->assertCount(3, $result);
  674. $this->assertTextEquals($expected, $result[0]);
  675. $this->assertEquals(
  676. 'CREATE INDEX "title_idx" ON "schema_articles" ("title")',
  677. $result[1]
  678. );
  679. $this->assertEquals(
  680. 'COMMENT ON COLUMN "schema_articles"."title" IS "This is the title"',
  681. $result[2]
  682. );
  683. }
  684. /**
  685. * Tests creating temporary tables
  686. *
  687. * @return void
  688. */
  689. public function testCreateTemporary() {
  690. $driver = $this->_getMockedDriver();
  691. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  692. $connection->expects($this->any())->method('driver')
  693. ->will($this->returnValue($driver));
  694. $table = (new Table('schema_articles'))->addColumn('id', [
  695. 'type' => 'integer',
  696. 'null' => false
  697. ]);
  698. $table->temporary(true);
  699. $sql = $table->createSql($connection);
  700. $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]);
  701. }
  702. /**
  703. * Test primary key generation & auto-increment.
  704. *
  705. * @return void
  706. */
  707. public function testCreateSqlCompositeIntegerKey() {
  708. $driver = $this->_getMockedDriver();
  709. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  710. $connection->expects($this->any())->method('driver')
  711. ->will($this->returnValue($driver));
  712. $table = (new Table('articles_tags'))
  713. ->addColumn('article_id', [
  714. 'type' => 'integer',
  715. 'null' => false
  716. ])
  717. ->addColumn('tag_id', [
  718. 'type' => 'integer',
  719. 'null' => false,
  720. ])
  721. ->addConstraint('primary', [
  722. 'type' => 'primary',
  723. 'columns' => ['article_id', 'tag_id']
  724. ]);
  725. $expected = <<<SQL
  726. CREATE TABLE "articles_tags" (
  727. "article_id" INTEGER NOT NULL,
  728. "tag_id" INTEGER NOT NULL,
  729. PRIMARY KEY ("article_id", "tag_id")
  730. )
  731. SQL;
  732. $result = $table->createSql($connection);
  733. $this->assertCount(1, $result);
  734. $this->assertTextEquals($expected, $result[0]);
  735. $table = (new Table('composite_key'))
  736. ->addColumn('id', [
  737. 'type' => 'integer',
  738. 'null' => false,
  739. 'autoIncrement' => true
  740. ])
  741. ->addColumn('account_id', [
  742. 'type' => 'integer',
  743. 'null' => false,
  744. ])
  745. ->addConstraint('primary', [
  746. 'type' => 'primary',
  747. 'columns' => ['id', 'account_id']
  748. ]);
  749. $expected = <<<SQL
  750. CREATE TABLE "composite_key" (
  751. "id" SERIAL,
  752. "account_id" INTEGER NOT NULL,
  753. PRIMARY KEY ("id", "account_id")
  754. )
  755. SQL;
  756. $result = $table->createSql($connection);
  757. $this->assertCount(1, $result);
  758. $this->assertTextEquals($expected, $result[0]);
  759. }
  760. /**
  761. * test dropSql
  762. *
  763. * @return void
  764. */
  765. public function testDropSql() {
  766. $driver = $this->_getMockedDriver();
  767. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  768. $connection->expects($this->any())->method('driver')
  769. ->will($this->returnValue($driver));
  770. $table = new Table('schema_articles');
  771. $result = $table->dropSql($connection);
  772. $this->assertCount(1, $result);
  773. $this->assertEquals('DROP TABLE "schema_articles"', $result[0]);
  774. }
  775. /**
  776. * Test truncateSql()
  777. *
  778. * @return void
  779. */
  780. public function testTruncateSql() {
  781. $driver = $this->_getMockedDriver();
  782. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  783. $connection->expects($this->any())->method('driver')
  784. ->will($this->returnValue($driver));
  785. $table = new Table('schema_articles');
  786. $table->addColumn('id', 'integer')
  787. ->addConstraint('primary', [
  788. 'type' => 'primary',
  789. 'columns' => ['id']
  790. ]);
  791. $result = $table->truncateSql($connection);
  792. $this->assertCount(1, $result);
  793. $this->assertEquals('TRUNCATE "schema_articles" RESTART IDENTITY', $result[0]);
  794. }
  795. /**
  796. * Get a schema instance with a mocked driver/pdo instances
  797. *
  798. * @return Driver
  799. */
  800. protected function _getMockedDriver() {
  801. $driver = new \Cake\Database\Driver\Postgres();
  802. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  803. $mock->expects($this->any())
  804. ->method('quote')
  805. ->will($this->returnCallback(function ($value) {
  806. return '"' . $value . '"';
  807. }));
  808. $mock->expects($this->any())
  809. ->method('quoteIdentifier')
  810. ->will($this->returnCallback(function ($value) {
  811. return '"' . $value . '"';
  812. }));
  813. $driver->connection($mock);
  814. return $driver;
  815. }
  816. }