PostgresSchemaTest.php 19 KB

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