PostgresSchemaTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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['className'], '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' => 'datetime', 'length' => null]
  83. ],
  84. [
  85. 'TIMESTAMP WITHOUT TIME ZONE',
  86. ['type' => 'datetime', '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->assertCount(2, $result);
  224. $this->assertEquals('schema_articles', $result[0]);
  225. $this->assertEquals('schema_authors', $result[1]);
  226. }
  227. /**
  228. * Test describing a table with Postgres
  229. *
  230. * @return void
  231. */
  232. public function testDescribeTable() {
  233. $connection = ConnectionManager::get('test');
  234. $this->_createTables($connection);
  235. $schema = new SchemaCollection($connection);
  236. $result = $schema->describe('schema_articles');
  237. $expected = [
  238. 'id' => [
  239. 'type' => 'biginteger',
  240. 'null' => false,
  241. 'default' => null,
  242. 'length' => 20,
  243. 'precision' => null,
  244. 'unsigned' => null,
  245. 'comment' => null,
  246. 'autoIncrement' => true,
  247. ],
  248. 'title' => [
  249. 'type' => 'string',
  250. 'null' => true,
  251. 'default' => null,
  252. 'length' => 20,
  253. 'precision' => null,
  254. 'comment' => 'a title',
  255. 'fixed' => null,
  256. ],
  257. 'body' => [
  258. 'type' => 'text',
  259. 'null' => true,
  260. 'default' => null,
  261. 'length' => null,
  262. 'precision' => null,
  263. 'comment' => null,
  264. ],
  265. 'author_id' => [
  266. 'type' => 'integer',
  267. 'null' => false,
  268. 'default' => null,
  269. 'length' => 10,
  270. 'precision' => null,
  271. 'unsigned' => null,
  272. 'comment' => null,
  273. 'autoIncrement' => null,
  274. ],
  275. 'published' => [
  276. 'type' => 'boolean',
  277. 'null' => true,
  278. 'default' => 0,
  279. 'length' => null,
  280. 'precision' => null,
  281. 'comment' => null,
  282. ],
  283. 'views' => [
  284. 'type' => 'integer',
  285. 'null' => true,
  286. 'default' => 0,
  287. 'length' => 5,
  288. 'precision' => null,
  289. 'unsigned' => null,
  290. 'comment' => null,
  291. 'autoIncrement' => null,
  292. ],
  293. 'created' => [
  294. 'type' => 'datetime',
  295. 'null' => true,
  296. 'default' => null,
  297. 'length' => null,
  298. 'precision' => null,
  299. 'comment' => null,
  300. ],
  301. ];
  302. $this->assertEquals(['id'], $result->primaryKey());
  303. foreach ($expected as $field => $definition) {
  304. $this->assertEquals($definition, $result->column($field));
  305. }
  306. }
  307. /**
  308. * Test describing a table with indexes
  309. *
  310. * @return void
  311. */
  312. public function testDescribeTableIndexes() {
  313. $connection = ConnectionManager::get('test');
  314. $this->_createTables($connection);
  315. $schema = new SchemaCollection($connection);
  316. $result = $schema->describe('schema_articles');
  317. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  318. $expected = [
  319. 'primary' => [
  320. 'type' => 'primary',
  321. 'columns' => ['id'],
  322. 'length' => []
  323. ],
  324. 'content_idx' => [
  325. 'type' => 'unique',
  326. 'columns' => ['title', 'body'],
  327. 'length' => []
  328. ]
  329. ];
  330. $this->assertCount(3, $result->constraints());
  331. $expected = [
  332. 'primary' => [
  333. 'type' => 'primary',
  334. 'columns' => ['id'],
  335. 'length' => []
  336. ],
  337. 'content_idx' => [
  338. 'type' => 'unique',
  339. 'columns' => ['title', 'body'],
  340. 'length' => []
  341. ],
  342. 'author_idx' => [
  343. 'type' => 'foreign',
  344. 'columns' => ['author_id'],
  345. 'references' => ['schema_authors', 'id'],
  346. 'length' => [],
  347. 'update' => 'cascade',
  348. 'delete' => 'restrict',
  349. ]
  350. ];
  351. $this->assertEquals($expected['primary'], $result->constraint('primary'));
  352. $this->assertEquals($expected['content_idx'], $result->constraint('content_idx'));
  353. $this->assertEquals($expected['author_idx'], $result->constraint('author_idx'));
  354. $this->assertCount(1, $result->indexes());
  355. $expected = [
  356. 'type' => 'index',
  357. 'columns' => ['author_id'],
  358. 'length' => []
  359. ];
  360. $this->assertEquals($expected, $result->index('author_idx'));
  361. }
  362. /**
  363. * Column provider for creating column sql
  364. *
  365. * @return array
  366. */
  367. public static function columnSqlProvider() {
  368. return [
  369. // strings
  370. [
  371. 'title',
  372. ['type' => 'string', 'length' => 25, 'null' => false],
  373. '"title" VARCHAR(25) NOT NULL'
  374. ],
  375. [
  376. 'title',
  377. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  378. '"title" VARCHAR(25) DEFAULT NULL'
  379. ],
  380. [
  381. 'id',
  382. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  383. '"id" CHAR(32) NOT NULL'
  384. ],
  385. [
  386. 'id',
  387. ['type' => 'uuid', 'length' => 36, 'null' => false],
  388. '"id" UUID NOT NULL'
  389. ],
  390. [
  391. 'role',
  392. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  393. '"role" VARCHAR(10) NOT NULL DEFAULT "admin"'
  394. ],
  395. [
  396. 'title',
  397. ['type' => 'string'],
  398. '"title" VARCHAR'
  399. ],
  400. // Text
  401. [
  402. 'body',
  403. ['type' => 'text', 'null' => false],
  404. '"body" TEXT NOT NULL'
  405. ],
  406. // Integers
  407. [
  408. 'post_id',
  409. ['type' => 'integer', 'length' => 11],
  410. '"post_id" INTEGER'
  411. ],
  412. [
  413. 'post_id',
  414. ['type' => 'biginteger', 'length' => 20],
  415. '"post_id" BIGINT'
  416. ],
  417. [
  418. 'post_id',
  419. ['type' => 'integer', 'autoIncrement' => true, 'length' => 11],
  420. '"post_id" SERIAL'
  421. ],
  422. [
  423. 'post_id',
  424. ['type' => 'biginteger', 'autoIncrement' => true, 'length' => 20],
  425. '"post_id" BIGSERIAL'
  426. ],
  427. // Decimal
  428. [
  429. 'value',
  430. ['type' => 'decimal'],
  431. '"value" DECIMAL'
  432. ],
  433. [
  434. 'value',
  435. ['type' => 'decimal', 'length' => 11],
  436. '"value" DECIMAL(11,0)'
  437. ],
  438. [
  439. 'value',
  440. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  441. '"value" DECIMAL(12,5)'
  442. ],
  443. // Float
  444. [
  445. 'value',
  446. ['type' => 'float'],
  447. '"value" FLOAT'
  448. ],
  449. [
  450. 'value',
  451. ['type' => 'float', 'length' => 11, 'precision' => 3],
  452. '"value" FLOAT(3)'
  453. ],
  454. // Binary
  455. [
  456. 'img',
  457. ['type' => 'binary'],
  458. '"img" BYTEA'
  459. ],
  460. // Boolean
  461. [
  462. 'checked',
  463. ['type' => 'boolean', 'default' => false],
  464. '"checked" BOOLEAN DEFAULT FALSE'
  465. ],
  466. [
  467. 'checked',
  468. ['type' => 'boolean', 'default' => true, 'null' => false],
  469. '"checked" BOOLEAN NOT NULL DEFAULT TRUE'
  470. ],
  471. // datetimes
  472. [
  473. 'created',
  474. ['type' => 'datetime'],
  475. '"created" TIMESTAMP'
  476. ],
  477. // Date & Time
  478. [
  479. 'start_date',
  480. ['type' => 'date'],
  481. '"start_date" DATE'
  482. ],
  483. [
  484. 'start_time',
  485. ['type' => 'time'],
  486. '"start_time" TIME'
  487. ],
  488. // timestamps
  489. [
  490. 'created',
  491. ['type' => 'timestamp', 'null' => true],
  492. '"created" TIMESTAMP DEFAULT NULL'
  493. ],
  494. ];
  495. }
  496. /**
  497. * Test generating column definitions
  498. *
  499. * @dataProvider columnSqlProvider
  500. * @return void
  501. */
  502. public function testColumnSql($name, $data, $expected) {
  503. $driver = $this->_getMockedDriver();
  504. $schema = new PostgresSchema($driver);
  505. $table = (new Table('schema_articles'))->addColumn($name, $data);
  506. $this->assertEquals($expected, $schema->columnSql($table, $name));
  507. }
  508. /**
  509. * Test generating a column that is a primary key.
  510. *
  511. * @return void
  512. */
  513. public function testColumnSqlPrimaryKey() {
  514. $driver = $this->_getMockedDriver();
  515. $schema = new PostgresSchema($driver);
  516. $table = new Table('schema_articles');
  517. $table->addColumn('id', [
  518. 'type' => 'integer',
  519. 'null' => false
  520. ])
  521. ->addConstraint('primary', [
  522. 'type' => 'primary',
  523. 'columns' => ['id']
  524. ]);
  525. $result = $schema->columnSql($table, 'id');
  526. $this->assertEquals($result, '"id" SERIAL');
  527. }
  528. /**
  529. * Provide data for testing constraintSql
  530. *
  531. * @return array
  532. */
  533. public static function constraintSqlProvider() {
  534. return [
  535. [
  536. 'primary',
  537. ['type' => 'primary', 'columns' => ['title']],
  538. 'PRIMARY KEY ("title")'
  539. ],
  540. [
  541. 'unique_idx',
  542. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  543. 'CONSTRAINT "unique_idx" UNIQUE ("title", "author_id")'
  544. ],
  545. [
  546. 'author_id_idx',
  547. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  548. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  549. 'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
  550. ],
  551. [
  552. 'author_id_idx',
  553. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  554. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  555. 'REFERENCES "authors" ("id") ON UPDATE CASCADE ON DELETE RESTRICT'
  556. ],
  557. [
  558. 'author_id_idx',
  559. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  560. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  561. 'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
  562. ],
  563. [
  564. 'author_id_idx',
  565. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  566. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  567. 'REFERENCES "authors" ("id") ON UPDATE SET NULL ON DELETE RESTRICT'
  568. ],
  569. [
  570. 'author_id_idx',
  571. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  572. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  573. 'REFERENCES "authors" ("id") ON UPDATE NO ACTION ON DELETE RESTRICT'
  574. ],
  575. ];
  576. }
  577. /**
  578. * Test the constraintSql method.
  579. *
  580. * @dataProvider constraintSqlProvider
  581. */
  582. public function testConstraintSql($name, $data, $expected) {
  583. $driver = $this->_getMockedDriver();
  584. $schema = new PostgresSchema($driver);
  585. $table = (new Table('schema_articles'))->addColumn('title', [
  586. 'type' => 'string',
  587. 'length' => 255
  588. ])->addColumn('author_id', [
  589. 'type' => 'integer',
  590. ])->addConstraint($name, $data);
  591. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  592. }
  593. /**
  594. * Integration test for converting a Schema\Table into MySQL table creates.
  595. *
  596. * @return void
  597. */
  598. public function testCreateSql() {
  599. $driver = $this->_getMockedDriver();
  600. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  601. $connection->expects($this->any())->method('driver')
  602. ->will($this->returnValue($driver));
  603. $table = (new Table('schema_articles'))->addColumn('id', [
  604. 'type' => 'integer',
  605. 'null' => false
  606. ])
  607. ->addColumn('title', [
  608. 'type' => 'string',
  609. 'null' => false,
  610. 'comment' => 'This is the title',
  611. ])
  612. ->addColumn('body', ['type' => 'text'])
  613. ->addColumn('created', 'datetime')
  614. ->addConstraint('primary', [
  615. 'type' => 'primary',
  616. 'columns' => ['id'],
  617. ])
  618. ->addIndex('title_idx', [
  619. 'type' => 'index',
  620. 'columns' => ['title'],
  621. ]);
  622. $expected = <<<SQL
  623. CREATE TABLE "schema_articles" (
  624. "id" SERIAL,
  625. "title" VARCHAR NOT NULL,
  626. "body" TEXT,
  627. "created" TIMESTAMP,
  628. PRIMARY KEY ("id")
  629. )
  630. SQL;
  631. $result = $table->createSql($connection);
  632. $this->assertCount(3, $result);
  633. $this->assertEquals($expected, $result[0]);
  634. $this->assertEquals(
  635. 'CREATE INDEX "title_idx" ON "schema_articles" ("title")',
  636. $result[1]
  637. );
  638. $this->assertEquals(
  639. 'COMMENT ON COLUMN "schema_articles"."title" IS "This is the title"',
  640. $result[2]
  641. );
  642. }
  643. /**
  644. * Tests creating temporary tables
  645. *
  646. * @return void
  647. */
  648. public function testCreateTemporary() {
  649. $driver = $this->_getMockedDriver();
  650. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  651. $connection->expects($this->any())->method('driver')
  652. ->will($this->returnValue($driver));
  653. $table = (new Table('schema_articles'))->addColumn('id', [
  654. 'type' => 'integer',
  655. 'null' => false
  656. ]);
  657. $table->temporary(true);
  658. $sql = $table->createSql($connection);
  659. $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]);
  660. }
  661. /**
  662. * Test primary key generation & auto-increment.
  663. *
  664. * @return void
  665. */
  666. public function testCreateSqlCompositeIntegerKey() {
  667. $driver = $this->_getMockedDriver();
  668. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  669. $connection->expects($this->any())->method('driver')
  670. ->will($this->returnValue($driver));
  671. $table = (new Table('articles_tags'))
  672. ->addColumn('article_id', [
  673. 'type' => 'integer',
  674. 'null' => false
  675. ])
  676. ->addColumn('tag_id', [
  677. 'type' => 'integer',
  678. 'null' => false,
  679. ])
  680. ->addConstraint('primary', [
  681. 'type' => 'primary',
  682. 'columns' => ['article_id', 'tag_id']
  683. ]);
  684. $expected = <<<SQL
  685. CREATE TABLE "articles_tags" (
  686. "article_id" INTEGER NOT NULL,
  687. "tag_id" INTEGER NOT NULL,
  688. PRIMARY KEY ("article_id", "tag_id")
  689. )
  690. SQL;
  691. $result = $table->createSql($connection);
  692. $this->assertCount(1, $result);
  693. $this->assertEquals($expected, $result[0]);
  694. $table = (new Table('composite_key'))
  695. ->addColumn('id', [
  696. 'type' => 'integer',
  697. 'null' => false,
  698. 'autoIncrement' => true
  699. ])
  700. ->addColumn('account_id', [
  701. 'type' => 'integer',
  702. 'null' => false,
  703. ])
  704. ->addConstraint('primary', [
  705. 'type' => 'primary',
  706. 'columns' => ['id', 'account_id']
  707. ]);
  708. $expected = <<<SQL
  709. CREATE TABLE "composite_key" (
  710. "id" SERIAL,
  711. "account_id" INTEGER NOT NULL,
  712. PRIMARY KEY ("id", "account_id")
  713. )
  714. SQL;
  715. $result = $table->createSql($connection);
  716. $this->assertCount(1, $result);
  717. $this->assertEquals($expected, $result[0]);
  718. }
  719. /**
  720. * test dropSql
  721. *
  722. * @return void
  723. */
  724. public function testDropSql() {
  725. $driver = $this->_getMockedDriver();
  726. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  727. $connection->expects($this->any())->method('driver')
  728. ->will($this->returnValue($driver));
  729. $table = new Table('schema_articles');
  730. $result = $table->dropSql($connection);
  731. $this->assertCount(1, $result);
  732. $this->assertEquals('DROP TABLE "schema_articles"', $result[0]);
  733. }
  734. /**
  735. * Test truncateSql()
  736. *
  737. * @return void
  738. */
  739. public function testTruncateSql() {
  740. $driver = $this->_getMockedDriver();
  741. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  742. $connection->expects($this->any())->method('driver')
  743. ->will($this->returnValue($driver));
  744. $table = new Table('schema_articles');
  745. $table->addColumn('id', 'integer')
  746. ->addConstraint('primary', [
  747. 'type' => 'primary',
  748. 'columns' => ['id']
  749. ]);
  750. $result = $table->truncateSql($connection);
  751. $this->assertCount(1, $result);
  752. $this->assertEquals('TRUNCATE "schema_articles" RESTART IDENTITY', $result[0]);
  753. }
  754. /**
  755. * Get a schema instance with a mocked driver/pdo instances
  756. *
  757. * @return Driver
  758. */
  759. protected function _getMockedDriver() {
  760. $driver = new \Cake\Database\Driver\Postgres();
  761. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  762. $mock->expects($this->any())
  763. ->method('quote')
  764. ->will($this->returnCallback(function ($value) {
  765. return '"' . $value . '"';
  766. }));
  767. $mock->expects($this->any())
  768. ->method('quoteIdentifier')
  769. ->will($this->returnCallback(function ($value) {
  770. return '"' . $value . '"';
  771. }));
  772. $driver->connection($mock);
  773. return $driver;
  774. }
  775. }