MysqlSchemaTest.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Schema;
  16. use Cake\Database\Driver\Mysql;
  17. use Cake\Database\Schema\Collection as SchemaCollection;
  18. use Cake\Database\Schema\MysqlSchema;
  19. use Cake\Database\Schema\TableSchema;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. use PDO;
  23. /**
  24. * Test case for Mysql Schema Dialect.
  25. */
  26. class MysqlSchemaTest extends TestCase
  27. {
  28. /**
  29. * Helper method for skipping tests that need a real connection.
  30. *
  31. * @return void
  32. */
  33. protected function _needsConnection()
  34. {
  35. $config = ConnectionManager::config('test');
  36. $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Not using Mysql for test config');
  37. }
  38. /**
  39. * Data provider for convert column testing
  40. *
  41. * @return array
  42. */
  43. public static function convertColumnProvider()
  44. {
  45. return [
  46. [
  47. 'DATETIME',
  48. ['type' => 'datetime', 'length' => null]
  49. ],
  50. [
  51. 'DATE',
  52. ['type' => 'date', 'length' => null]
  53. ],
  54. [
  55. 'TIME',
  56. ['type' => 'time', 'length' => null]
  57. ],
  58. [
  59. 'TIMESTAMP',
  60. ['type' => 'timestamp', 'length' => null]
  61. ],
  62. [
  63. 'TINYINT(1)',
  64. ['type' => 'boolean', 'length' => null]
  65. ],
  66. [
  67. 'TINYINT(2)',
  68. ['type' => 'tinyinteger', 'length' => 2, 'unsigned' => false]
  69. ],
  70. [
  71. 'TINYINT(3)',
  72. ['type' => 'tinyinteger', 'length' => 3, 'unsigned' => false]
  73. ],
  74. [
  75. 'TINYINT(3) UNSIGNED',
  76. ['type' => 'tinyinteger', 'length' => 3, 'unsigned' => true]
  77. ],
  78. [
  79. 'SMALLINT(4)',
  80. ['type' => 'smallinteger', 'length' => 4, 'unsigned' => false]
  81. ],
  82. [
  83. 'SMALLINT(4) UNSIGNED',
  84. ['type' => 'smallinteger', 'length' => 4, 'unsigned' => true]
  85. ],
  86. [
  87. 'INTEGER(11)',
  88. ['type' => 'integer', 'length' => 11, 'unsigned' => false]
  89. ],
  90. [
  91. 'MEDIUMINT(11)',
  92. ['type' => 'integer', 'length' => 11, 'unsigned' => false]
  93. ],
  94. [
  95. 'INTEGER(11) UNSIGNED',
  96. ['type' => 'integer', 'length' => 11, 'unsigned' => true]
  97. ],
  98. [
  99. 'BIGINT',
  100. ['type' => 'biginteger', 'length' => null, 'unsigned' => false]
  101. ],
  102. [
  103. 'BIGINT UNSIGNED',
  104. ['type' => 'biginteger', 'length' => null, 'unsigned' => true]
  105. ],
  106. [
  107. 'VARCHAR(255)',
  108. ['type' => 'string', 'length' => 255]
  109. ],
  110. [
  111. 'CHAR(25)',
  112. ['type' => 'string', 'length' => 25, 'fixed' => true]
  113. ],
  114. [
  115. 'CHAR(36)',
  116. ['type' => 'uuid', 'length' => null]
  117. ],
  118. [
  119. 'TEXT',
  120. ['type' => 'text', 'length' => null]
  121. ],
  122. [
  123. 'TINYTEXT',
  124. ['type' => 'text', 'length' => TableSchema::LENGTH_TINY]
  125. ],
  126. [
  127. 'MEDIUMTEXT',
  128. ['type' => 'text', 'length' => TableSchema::LENGTH_MEDIUM]
  129. ],
  130. [
  131. 'LONGTEXT',
  132. ['type' => 'text', 'length' => TableSchema::LENGTH_LONG]
  133. ],
  134. [
  135. 'TINYBLOB',
  136. ['type' => 'binary', 'length' => TableSchema::LENGTH_TINY]
  137. ],
  138. [
  139. 'BLOB',
  140. ['type' => 'binary', 'length' => null]
  141. ],
  142. [
  143. 'MEDIUMBLOB',
  144. ['type' => 'binary', 'length' => TableSchema::LENGTH_MEDIUM]
  145. ],
  146. [
  147. 'LONGBLOB',
  148. ['type' => 'binary', 'length' => TableSchema::LENGTH_LONG]
  149. ],
  150. [
  151. 'FLOAT',
  152. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
  153. ],
  154. [
  155. 'DOUBLE',
  156. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
  157. ],
  158. [
  159. 'DOUBLE UNSIGNED',
  160. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => true]
  161. ],
  162. [
  163. 'DECIMAL(11,2) UNSIGNED',
  164. ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => true]
  165. ],
  166. [
  167. 'DECIMAL(11,2)',
  168. ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => false]
  169. ],
  170. [
  171. 'FLOAT(11,2)',
  172. ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => false]
  173. ],
  174. [
  175. 'FLOAT(11,2) UNSIGNED',
  176. ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => true]
  177. ],
  178. [
  179. 'DOUBLE(10,4)',
  180. ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => false]
  181. ],
  182. [
  183. 'DOUBLE(10,4) UNSIGNED',
  184. ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => true]
  185. ],
  186. [
  187. 'JSON',
  188. ['type' => 'json', 'length' => null]
  189. ],
  190. ];
  191. }
  192. /**
  193. * Test parsing MySQL column types from field description.
  194. *
  195. * @dataProvider convertColumnProvider
  196. * @return void
  197. */
  198. public function testConvertColumn($type, $expected)
  199. {
  200. $field = [
  201. 'Field' => 'field',
  202. 'Type' => $type,
  203. 'Null' => 'YES',
  204. 'Default' => 'Default value',
  205. 'Collation' => 'utf8_general_ci',
  206. 'Comment' => 'Comment section',
  207. ];
  208. $expected += [
  209. 'null' => true,
  210. 'default' => 'Default value',
  211. 'collate' => 'utf8_general_ci',
  212. 'comment' => 'Comment section',
  213. ];
  214. $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')->getMock();
  215. $dialect = new MysqlSchema($driver);
  216. $table = $this->getMockBuilder(TableSchema::class)
  217. ->setConstructorArgs(['table'])
  218. ->getMock();
  219. $table->expects($this->at(0))->method('addColumn')->with('field', $expected);
  220. $dialect->convertColumnDescription($table, $field);
  221. }
  222. /**
  223. * Helper method for testing methods.
  224. *
  225. * @param \Cake\Datasource\ConnectionInterface $connection
  226. * @return void
  227. */
  228. protected function _createTables($connection)
  229. {
  230. $this->_needsConnection();
  231. $connection->execute('DROP TABLE IF EXISTS schema_articles');
  232. $connection->execute('DROP TABLE IF EXISTS schema_authors');
  233. $connection->execute('DROP TABLE IF EXISTS schema_json');
  234. $table = <<<SQL
  235. CREATE TABLE schema_authors (
  236. id INT(11) PRIMARY KEY AUTO_INCREMENT,
  237. name VARCHAR(50),
  238. bio TEXT,
  239. created DATETIME
  240. )ENGINE=InnoDB
  241. SQL;
  242. $connection->execute($table);
  243. $table = <<<SQL
  244. CREATE TABLE schema_articles (
  245. id BIGINT PRIMARY KEY AUTO_INCREMENT,
  246. title VARCHAR(20) COMMENT 'A title',
  247. body TEXT,
  248. author_id INT(11) NOT NULL,
  249. published BOOLEAN DEFAULT 0,
  250. allow_comments TINYINT(1) DEFAULT 0,
  251. created DATETIME,
  252. KEY `author_idx` (`author_id`),
  253. UNIQUE KEY `length_idx` (`title`(4)),
  254. FOREIGN KEY `author_idx` (`author_id`) REFERENCES `schema_authors`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
  255. ) ENGINE=InnoDB COLLATE=utf8_general_ci
  256. SQL;
  257. $connection->execute($table);
  258. if ($connection->driver()->supportsNativeJson()) {
  259. $table = <<<SQL
  260. CREATE TABLE schema_json (
  261. id INT(11) PRIMARY KEY AUTO_INCREMENT,
  262. data JSON NOT NULL
  263. )
  264. SQL;
  265. $connection->execute($table);
  266. }
  267. }
  268. /**
  269. * Integration test for SchemaCollection & MysqlDialect.
  270. *
  271. * @return void
  272. */
  273. public function testListTables()
  274. {
  275. $connection = ConnectionManager::get('test');
  276. $this->_createTables($connection);
  277. $schema = new SchemaCollection($connection);
  278. $result = $schema->listTables();
  279. $this->assertInternalType('array', $result);
  280. $this->assertContains('schema_articles', $result);
  281. $this->assertContains('schema_authors', $result);
  282. }
  283. /**
  284. * Test describing a table with Mysql
  285. *
  286. * @return void
  287. */
  288. public function testDescribeTable()
  289. {
  290. $connection = ConnectionManager::get('test');
  291. $this->_createTables($connection);
  292. $schema = new SchemaCollection($connection);
  293. $result = $schema->describe('schema_articles');
  294. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  295. $expected = [
  296. 'id' => [
  297. 'type' => 'biginteger',
  298. 'null' => false,
  299. 'unsigned' => false,
  300. 'default' => null,
  301. 'length' => 20,
  302. 'precision' => null,
  303. 'comment' => null,
  304. 'autoIncrement' => true,
  305. ],
  306. 'title' => [
  307. 'type' => 'string',
  308. 'null' => true,
  309. 'default' => null,
  310. 'length' => 20,
  311. 'precision' => null,
  312. 'comment' => 'A title',
  313. 'fixed' => null,
  314. 'collate' => 'utf8_general_ci',
  315. ],
  316. 'body' => [
  317. 'type' => 'text',
  318. 'null' => true,
  319. 'default' => null,
  320. 'length' => null,
  321. 'precision' => null,
  322. 'comment' => null,
  323. 'collate' => 'utf8_general_ci',
  324. ],
  325. 'author_id' => [
  326. 'type' => 'integer',
  327. 'null' => false,
  328. 'unsigned' => false,
  329. 'default' => null,
  330. 'length' => 11,
  331. 'precision' => null,
  332. 'comment' => null,
  333. 'autoIncrement' => null,
  334. ],
  335. 'published' => [
  336. 'type' => 'boolean',
  337. 'null' => true,
  338. 'default' => 0,
  339. 'length' => null,
  340. 'precision' => null,
  341. 'comment' => null,
  342. ],
  343. 'allow_comments' => [
  344. 'type' => 'boolean',
  345. 'null' => true,
  346. 'default' => 0,
  347. 'length' => null,
  348. 'precision' => null,
  349. 'comment' => null,
  350. ],
  351. 'created' => [
  352. 'type' => 'datetime',
  353. 'null' => true,
  354. 'default' => null,
  355. 'length' => null,
  356. 'precision' => null,
  357. 'comment' => null,
  358. ],
  359. ];
  360. $this->assertEquals(['id'], $result->primaryKey());
  361. foreach ($expected as $field => $definition) {
  362. $this->assertEquals(
  363. $definition,
  364. $result->column($field),
  365. 'Field definition does not match for ' . $field
  366. );
  367. }
  368. }
  369. /**
  370. * Test describing a table with indexes in Mysql
  371. *
  372. * @return void
  373. */
  374. public function testDescribeTableIndexes()
  375. {
  376. $connection = ConnectionManager::get('test');
  377. $this->_createTables($connection);
  378. $schema = new SchemaCollection($connection);
  379. $result = $schema->describe('schema_articles');
  380. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  381. $this->assertCount(3, $result->constraints());
  382. $expected = [
  383. 'primary' => [
  384. 'type' => 'primary',
  385. 'columns' => ['id'],
  386. 'length' => []
  387. ],
  388. 'length_idx' => [
  389. 'type' => 'unique',
  390. 'columns' => ['title'],
  391. 'length' => [
  392. 'title' => 4,
  393. ]
  394. ],
  395. 'schema_articles_ibfk_1' => [
  396. 'type' => 'foreign',
  397. 'columns' => ['author_id'],
  398. 'references' => ['schema_authors', 'id'],
  399. 'length' => [],
  400. 'update' => 'cascade',
  401. 'delete' => 'restrict',
  402. ]
  403. ];
  404. $this->assertEquals($expected['primary'], $result->constraint('primary'));
  405. $this->assertEquals($expected['length_idx'], $result->constraint('length_idx'));
  406. $this->assertEquals($expected['schema_articles_ibfk_1'], $result->constraint('schema_articles_ibfk_1'));
  407. $this->assertCount(1, $result->indexes());
  408. $expected = [
  409. 'type' => 'index',
  410. 'columns' => ['author_id'],
  411. 'length' => []
  412. ];
  413. $this->assertEquals($expected, $result->index('author_idx'));
  414. }
  415. /**
  416. * Test describing a table creates options
  417. *
  418. * @return void
  419. */
  420. public function testDescribeTableOptions()
  421. {
  422. $connection = ConnectionManager::get('test');
  423. $this->_createTables($connection);
  424. $schema = new SchemaCollection($connection);
  425. $result = $schema->describe('schema_articles');
  426. $this->assertArrayHasKey('engine', $result->options());
  427. $this->assertArrayHasKey('collation', $result->options());
  428. }
  429. public function testDescribeNonPrimaryAutoIncrement()
  430. {
  431. $this->_needsConnection();
  432. $connection = ConnectionManager::get('test');
  433. $sql = <<<SQL
  434. CREATE TABLE `odd_primary_key` (
  435. `id` BIGINT UNSIGNED NOT NULL,
  436. `other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,
  437. PRIMARY KEY (`id`),
  438. UNIQUE KEY `other_field` (`other_field`)
  439. )
  440. SQL;
  441. $connection->execute($sql);
  442. $schema = new SchemaCollection($connection);
  443. $table = $schema->describe('odd_primary_key');
  444. $connection->execute('DROP TABLE odd_primary_key');
  445. $column = $table->column('id');
  446. $this->assertNull($column['autoIncrement'], 'should not autoincrement');
  447. $this->assertTrue($column['unsigned'], 'should be unsigned');
  448. $column = $table->column('other_field');
  449. $this->assertTrue($column['autoIncrement'], 'should not autoincrement');
  450. $this->assertFalse($column['unsigned'], 'should not be unsigned');
  451. $output = $table->createSql($connection);
  452. $this->assertContains('`id` BIGINT UNSIGNED NOT NULL,', $output[0]);
  453. $this->assertContains('`other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,', $output[0]);
  454. }
  455. /**
  456. * Column provider for creating column sql
  457. *
  458. * @return array
  459. */
  460. public static function columnSqlProvider()
  461. {
  462. return [
  463. // strings
  464. [
  465. 'title',
  466. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => null],
  467. '`title` VARCHAR(25)',
  468. ],
  469. [
  470. 'title',
  471. ['type' => 'string', 'length' => 25, 'null' => false],
  472. '`title` VARCHAR(25) NOT NULL'
  473. ],
  474. [
  475. 'title',
  476. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  477. '`title` VARCHAR(25) DEFAULT \'ignored\'',
  478. ],
  479. [
  480. 'title',
  481. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => ''],
  482. '`title` VARCHAR(25) DEFAULT \'\'',
  483. ],
  484. [
  485. 'role',
  486. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  487. '`role` VARCHAR(10) NOT NULL DEFAULT \'admin\''
  488. ],
  489. [
  490. 'id',
  491. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  492. '`id` CHAR(32) NOT NULL'
  493. ],
  494. [
  495. 'title',
  496. ['type' => 'string'],
  497. '`title` VARCHAR(255)'
  498. ],
  499. [
  500. 'id',
  501. ['type' => 'uuid'],
  502. '`id` CHAR(36)'
  503. ],
  504. [
  505. 'title',
  506. ['type' => 'string', 'length' => 255, 'null' => false, 'collate' => 'utf8_unicode_ci'],
  507. '`title` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL'
  508. ],
  509. // Text
  510. [
  511. 'body',
  512. ['type' => 'text', 'null' => false],
  513. '`body` TEXT NOT NULL'
  514. ],
  515. [
  516. 'body',
  517. ['type' => 'text', 'length' => TableSchema::LENGTH_TINY, 'null' => false],
  518. '`body` TINYTEXT NOT NULL'
  519. ],
  520. [
  521. 'body',
  522. ['type' => 'text', 'length' => TableSchema::LENGTH_MEDIUM, 'null' => false],
  523. '`body` MEDIUMTEXT NOT NULL'
  524. ],
  525. [
  526. 'body',
  527. ['type' => 'text', 'length' => TableSchema::LENGTH_LONG, 'null' => false],
  528. '`body` LONGTEXT NOT NULL'
  529. ],
  530. [
  531. 'body',
  532. ['type' => 'text', 'null' => false, 'collate' => 'utf8_unicode_ci'],
  533. '`body` TEXT COLLATE utf8_unicode_ci NOT NULL'
  534. ],
  535. // Blob / binary
  536. [
  537. 'body',
  538. ['type' => 'binary', 'null' => false],
  539. '`body` BLOB NOT NULL'
  540. ],
  541. [
  542. 'body',
  543. ['type' => 'binary', 'length' => TableSchema::LENGTH_TINY, 'null' => false],
  544. '`body` TINYBLOB NOT NULL'
  545. ],
  546. [
  547. 'body',
  548. ['type' => 'binary', 'length' => TableSchema::LENGTH_MEDIUM, 'null' => false],
  549. '`body` MEDIUMBLOB NOT NULL'
  550. ],
  551. [
  552. 'body',
  553. ['type' => 'binary', 'length' => TableSchema::LENGTH_LONG, 'null' => false],
  554. '`body` LONGBLOB NOT NULL'
  555. ],
  556. // Integers
  557. [
  558. 'post_id',
  559. ['type' => 'tinyinteger', 'length' => 2],
  560. '`post_id` TINYINT(2)'
  561. ],
  562. [
  563. 'post_id',
  564. ['type' => 'tinyinteger', 'length' => 2, 'unsigned' => true],
  565. '`post_id` TINYINT(2) UNSIGNED'
  566. ],
  567. [
  568. 'post_id',
  569. ['type' => 'smallinteger', 'length' => 4],
  570. '`post_id` SMALLINT(4)'
  571. ],
  572. [
  573. 'post_id',
  574. ['type' => 'smallinteger', 'length' => 4, 'unsigned' => true],
  575. '`post_id` SMALLINT(4) UNSIGNED'
  576. ],
  577. [
  578. 'post_id',
  579. ['type' => 'integer', 'length' => 11],
  580. '`post_id` INTEGER(11)'
  581. ],
  582. [
  583. 'post_id',
  584. ['type' => 'integer', 'length' => 11, 'unsigned' => true],
  585. '`post_id` INTEGER(11) UNSIGNED'
  586. ],
  587. [
  588. 'post_id',
  589. ['type' => 'biginteger', 'length' => 20],
  590. '`post_id` BIGINT'
  591. ],
  592. [
  593. 'post_id',
  594. ['type' => 'biginteger', 'length' => 20, 'unsigned' => true],
  595. '`post_id` BIGINT UNSIGNED'
  596. ],
  597. [
  598. 'post_id',
  599. ['type' => 'integer', 'length' => 20, 'autoIncrement' => true],
  600. '`post_id` INTEGER(20) AUTO_INCREMENT'
  601. ],
  602. [
  603. 'post_id',
  604. ['type' => 'integer', 'length' => 20, 'null' => false, 'autoIncrement' => false],
  605. '`post_id` INTEGER(20) NOT NULL'
  606. ],
  607. [
  608. 'post_id',
  609. ['type' => 'biginteger', 'length' => 20, 'autoIncrement' => true],
  610. '`post_id` BIGINT AUTO_INCREMENT'
  611. ],
  612. // Decimal
  613. [
  614. 'value',
  615. ['type' => 'decimal'],
  616. '`value` DECIMAL'
  617. ],
  618. [
  619. 'value',
  620. ['type' => 'decimal', 'length' => 11, 'unsigned' => true],
  621. '`value` DECIMAL(11,0) UNSIGNED'
  622. ],
  623. [
  624. 'value',
  625. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  626. '`value` DECIMAL(12,5)'
  627. ],
  628. // Float
  629. [
  630. 'value',
  631. ['type' => 'float', 'unsigned'],
  632. '`value` FLOAT'
  633. ],
  634. [
  635. 'value',
  636. ['type' => 'float', 'unsigned' => true],
  637. '`value` FLOAT UNSIGNED'
  638. ],
  639. [
  640. 'value',
  641. ['type' => 'float', 'length' => 11, 'precision' => 3],
  642. '`value` FLOAT(11,3)'
  643. ],
  644. // Boolean
  645. [
  646. 'checked',
  647. ['type' => 'boolean', 'default' => false],
  648. '`checked` BOOLEAN DEFAULT FALSE'
  649. ],
  650. [
  651. 'checked',
  652. ['type' => 'boolean', 'default' => false, 'null' => false],
  653. '`checked` BOOLEAN NOT NULL DEFAULT FALSE'
  654. ],
  655. [
  656. 'checked',
  657. ['type' => 'boolean', 'default' => true, 'null' => false],
  658. '`checked` BOOLEAN NOT NULL DEFAULT TRUE'
  659. ],
  660. [
  661. 'checked',
  662. ['type' => 'boolean', 'default' => false, 'null' => true],
  663. '`checked` BOOLEAN DEFAULT FALSE'
  664. ],
  665. // datetimes
  666. [
  667. 'created',
  668. ['type' => 'datetime', 'comment' => 'Created timestamp'],
  669. '`created` DATETIME COMMENT \'Created timestamp\''
  670. ],
  671. [
  672. 'created',
  673. ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'],
  674. '`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'
  675. ],
  676. [
  677. 'open_date',
  678. ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
  679. '`open_date` DATETIME NOT NULL DEFAULT \'2016-12-07 23:04:00\''
  680. ],
  681. // Date & Time
  682. [
  683. 'start_date',
  684. ['type' => 'date'],
  685. '`start_date` DATE'
  686. ],
  687. [
  688. 'start_time',
  689. ['type' => 'time'],
  690. '`start_time` TIME'
  691. ],
  692. // timestamps
  693. [
  694. 'created',
  695. ['type' => 'timestamp', 'null' => true],
  696. '`created` TIMESTAMP NULL'
  697. ],
  698. [
  699. 'created',
  700. ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'],
  701. '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
  702. ],
  703. [
  704. 'open_date',
  705. ['type' => 'timestamp', 'null' => false, 'default' => '2016-12-07 23:04:00'],
  706. '`open_date` TIMESTAMP NOT NULL DEFAULT \'2016-12-07 23:04:00\''
  707. ],
  708. ];
  709. }
  710. /**
  711. * Test generating column definitions
  712. *
  713. * @dataProvider columnSqlProvider
  714. * @return void
  715. */
  716. public function testColumnSql($name, $data, $expected)
  717. {
  718. $driver = $this->_getMockedDriver();
  719. $schema = new MysqlSchema($driver);
  720. $table = (new TableSchema('articles'))->addColumn($name, $data);
  721. $this->assertEquals($expected, $schema->columnSql($table, $name));
  722. }
  723. /**
  724. * Provide data for testing constraintSql
  725. *
  726. * @return array
  727. */
  728. public static function constraintSqlProvider()
  729. {
  730. return [
  731. [
  732. 'primary',
  733. ['type' => 'primary', 'columns' => ['title']],
  734. 'PRIMARY KEY (`title`)'
  735. ],
  736. [
  737. 'unique_idx',
  738. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  739. 'UNIQUE KEY `unique_idx` (`title`, `author_id`)'
  740. ],
  741. [
  742. 'length_idx',
  743. [
  744. 'type' => 'unique',
  745. 'columns' => ['author_id', 'title'],
  746. 'length' => ['author_id' => 5, 'title' => 4]
  747. ],
  748. 'UNIQUE KEY `length_idx` (`author_id`(5), `title`(4))'
  749. ],
  750. [
  751. 'author_id_idx',
  752. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  753. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  754. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  755. ],
  756. [
  757. 'author_id_idx',
  758. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  759. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  760. 'REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT'
  761. ],
  762. [
  763. 'author_id_idx',
  764. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  765. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  766. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  767. ],
  768. [
  769. 'author_id_idx',
  770. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  771. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  772. 'REFERENCES `authors` (`id`) ON UPDATE SET NULL ON DELETE RESTRICT'
  773. ],
  774. [
  775. 'author_id_idx',
  776. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  777. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  778. 'REFERENCES `authors` (`id`) ON UPDATE NO ACTION ON DELETE RESTRICT'
  779. ],
  780. ];
  781. }
  782. /**
  783. * Test the constraintSql method.
  784. *
  785. * @dataProvider constraintSqlProvider
  786. */
  787. public function testConstraintSql($name, $data, $expected)
  788. {
  789. $driver = $this->_getMockedDriver();
  790. $schema = new MysqlSchema($driver);
  791. $table = (new TableSchema('articles'))->addColumn('title', [
  792. 'type' => 'string',
  793. 'length' => 255
  794. ])->addColumn('author_id', [
  795. 'type' => 'integer',
  796. ])->addConstraint($name, $data);
  797. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  798. }
  799. /**
  800. * Test provider for indexSql()
  801. *
  802. * @return array
  803. */
  804. public static function indexSqlProvider()
  805. {
  806. return [
  807. [
  808. 'key_key',
  809. ['type' => 'index', 'columns' => ['author_id']],
  810. 'KEY `key_key` (`author_id`)'
  811. ],
  812. [
  813. 'full_text',
  814. ['type' => 'fulltext', 'columns' => ['title']],
  815. 'FULLTEXT KEY `full_text` (`title`)'
  816. ],
  817. ];
  818. }
  819. /**
  820. * Test the indexSql method.
  821. *
  822. * @dataProvider indexSqlProvider
  823. */
  824. public function testIndexSql($name, $data, $expected)
  825. {
  826. $driver = $this->_getMockedDriver();
  827. $schema = new MysqlSchema($driver);
  828. $table = (new TableSchema('articles'))->addColumn('title', [
  829. 'type' => 'string',
  830. 'length' => 255
  831. ])->addColumn('author_id', [
  832. 'type' => 'integer',
  833. ])->addIndex($name, $data);
  834. $this->assertEquals($expected, $schema->indexSql($table, $name));
  835. }
  836. /**
  837. * Test the addConstraintSql method.
  838. *
  839. * @return void
  840. */
  841. public function testAddConstraintSql()
  842. {
  843. $driver = $this->_getMockedDriver();
  844. $connection = $this->getMockBuilder('Cake\Database\Connection')
  845. ->disableOriginalConstructor()
  846. ->getMock();
  847. $connection->expects($this->any())->method('getDriver')
  848. ->will($this->returnValue($driver));
  849. $table = (new TableSchema('posts'))
  850. ->addColumn('author_id', [
  851. 'type' => 'integer',
  852. 'null' => false
  853. ])
  854. ->addColumn('category_id', [
  855. 'type' => 'integer',
  856. 'null' => false
  857. ])
  858. ->addColumn('category_name', [
  859. 'type' => 'integer',
  860. 'null' => false
  861. ])
  862. ->addConstraint('author_fk', [
  863. 'type' => 'foreign',
  864. 'columns' => ['author_id'],
  865. 'references' => ['authors', 'id'],
  866. 'update' => 'cascade',
  867. 'delete' => 'cascade'
  868. ])
  869. ->addConstraint('category_fk', [
  870. 'type' => 'foreign',
  871. 'columns' => ['category_id', 'category_name'],
  872. 'references' => ['categories', ['id', 'name']],
  873. 'update' => 'cascade',
  874. 'delete' => 'cascade'
  875. ]);
  876. $expected = [
  877. 'ALTER TABLE `posts` ADD CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;',
  878. 'ALTER TABLE `posts` ADD CONSTRAINT `category_fk` FOREIGN KEY (`category_id`, `category_name`) REFERENCES `categories` (`id`, `name`) ON UPDATE CASCADE ON DELETE CASCADE;'
  879. ];
  880. $result = $table->addConstraintSql($connection);
  881. $this->assertCount(2, $result);
  882. $this->assertEquals($expected, $result);
  883. }
  884. /**
  885. * Test the dropConstraintSql method.
  886. *
  887. * @return void
  888. */
  889. public function testDropConstraintSql()
  890. {
  891. $driver = $this->_getMockedDriver();
  892. $connection = $this->getMockBuilder('Cake\Database\Connection')
  893. ->disableOriginalConstructor()
  894. ->getMock();
  895. $connection->expects($this->any())->method('getDriver')
  896. ->will($this->returnValue($driver));
  897. $table = (new TableSchema('posts'))
  898. ->addColumn('author_id', [
  899. 'type' => 'integer',
  900. 'null' => false
  901. ])
  902. ->addColumn('category_id', [
  903. 'type' => 'integer',
  904. 'null' => false
  905. ])
  906. ->addColumn('category_name', [
  907. 'type' => 'integer',
  908. 'null' => false
  909. ])
  910. ->addConstraint('author_fk', [
  911. 'type' => 'foreign',
  912. 'columns' => ['author_id'],
  913. 'references' => ['authors', 'id'],
  914. 'update' => 'cascade',
  915. 'delete' => 'cascade'
  916. ])
  917. ->addConstraint('category_fk', [
  918. 'type' => 'foreign',
  919. 'columns' => ['category_id', 'category_name'],
  920. 'references' => ['categories', ['id', 'name']],
  921. 'update' => 'cascade',
  922. 'delete' => 'cascade'
  923. ]);
  924. $expected = [
  925. 'ALTER TABLE `posts` DROP FOREIGN KEY `author_fk`;',
  926. 'ALTER TABLE `posts` DROP FOREIGN KEY `category_fk`;'
  927. ];
  928. $result = $table->dropConstraintSql($connection);
  929. $this->assertCount(2, $result);
  930. $this->assertEquals($expected, $result);
  931. }
  932. /**
  933. * Test generating a column that is a primary key.
  934. *
  935. * @return void
  936. */
  937. public function testColumnSqlPrimaryKey()
  938. {
  939. $driver = $this->_getMockedDriver();
  940. $schema = new MysqlSchema($driver);
  941. $table = new TableSchema('articles');
  942. $table->addColumn('id', [
  943. 'type' => 'integer',
  944. 'null' => false,
  945. ])
  946. ->addConstraint('primary', [
  947. 'type' => 'primary',
  948. 'columns' => ['id']
  949. ]);
  950. $result = $schema->columnSql($table, 'id');
  951. $this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');
  952. $table = new TableSchema('articles');
  953. $table->addColumn('id', [
  954. 'type' => 'biginteger',
  955. 'null' => false
  956. ])
  957. ->addConstraint('primary', [
  958. 'type' => 'primary',
  959. 'columns' => ['id']
  960. ]);
  961. $result = $schema->columnSql($table, 'id');
  962. $this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
  963. }
  964. /**
  965. * Integration test for converting a Schema\Table into MySQL table creates.
  966. *
  967. * @return void
  968. */
  969. public function testCreateSql()
  970. {
  971. $driver = $this->_getMockedDriver();
  972. $connection = $this->getMockBuilder('Cake\Database\Connection')
  973. ->disableOriginalConstructor()
  974. ->getMock();
  975. $connection->expects($this->any())->method('getDriver')
  976. ->will($this->returnValue($driver));
  977. $driver->connection()
  978. ->expects($this->any())
  979. ->method('getAttribute')
  980. ->will($this->returnValue('5.6.0'));
  981. $table = (new TableSchema('posts'))->addColumn('id', [
  982. 'type' => 'integer',
  983. 'null' => false
  984. ])
  985. ->addColumn('title', [
  986. 'type' => 'string',
  987. 'null' => false,
  988. 'comment' => 'The title'
  989. ])
  990. ->addColumn('body', [
  991. 'type' => 'text',
  992. 'comment' => ''
  993. ])
  994. ->addColumn('data', [
  995. 'type' => 'json'
  996. ])
  997. ->addColumn('hash', [
  998. 'type' => 'string',
  999. 'fixed' => true,
  1000. 'length' => 40,
  1001. 'collate' => 'latin1_bin',
  1002. 'null' => false,
  1003. ])
  1004. ->addColumn('created', 'datetime')
  1005. ->addConstraint('primary', [
  1006. 'type' => 'primary',
  1007. 'columns' => ['id']
  1008. ])
  1009. ->options([
  1010. 'engine' => 'InnoDB',
  1011. 'charset' => 'utf8',
  1012. 'collate' => 'utf8_general_ci',
  1013. ]);
  1014. $expected = <<<SQL
  1015. CREATE TABLE `posts` (
  1016. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1017. `title` VARCHAR(255) NOT NULL COMMENT 'The title',
  1018. `body` TEXT,
  1019. `data` LONGTEXT,
  1020. `hash` CHAR(40) COLLATE latin1_bin NOT NULL,
  1021. `created` DATETIME,
  1022. PRIMARY KEY (`id`)
  1023. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  1024. SQL;
  1025. $result = $table->createSql($connection);
  1026. $this->assertCount(1, $result);
  1027. $this->assertTextEquals($expected, $result[0]);
  1028. }
  1029. /**
  1030. * Integration test for converting a Schema\Table with native JSON
  1031. *
  1032. * @return void
  1033. */
  1034. public function testCreateSqlJson()
  1035. {
  1036. $driver = $this->_getMockedDriver();
  1037. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1038. ->disableOriginalConstructor()
  1039. ->getMock();
  1040. $connection->expects($this->any())
  1041. ->method('getDriver')
  1042. ->will($this->returnValue($driver));
  1043. $driver->connection()
  1044. ->expects($this->any())
  1045. ->method('getAttribute')
  1046. ->will($this->returnValue('5.7.0'));
  1047. $table = (new TableSchema('posts'))->addColumn('id', [
  1048. 'type' => 'integer',
  1049. 'null' => false
  1050. ])
  1051. ->addColumn('data', [
  1052. 'type' => 'json'
  1053. ])
  1054. ->addConstraint('primary', [
  1055. 'type' => 'primary',
  1056. 'columns' => ['id']
  1057. ])
  1058. ->options([
  1059. 'engine' => 'InnoDB',
  1060. 'charset' => 'utf8',
  1061. 'collate' => 'utf8_general_ci',
  1062. ]);
  1063. $expected = <<<SQL
  1064. CREATE TABLE `posts` (
  1065. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1066. `data` JSON,
  1067. PRIMARY KEY (`id`)
  1068. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  1069. SQL;
  1070. $result = $table->createSql($connection);
  1071. $this->assertCount(1, $result);
  1072. $this->assertTextEquals($expected, $result[0]);
  1073. }
  1074. /**
  1075. * Tests creating temporary tables
  1076. *
  1077. * @return void
  1078. */
  1079. public function testCreateTemporary()
  1080. {
  1081. $driver = $this->_getMockedDriver();
  1082. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1083. ->disableOriginalConstructor()
  1084. ->getMock();
  1085. $connection->expects($this->any())->method('getDriver')
  1086. ->will($this->returnValue($driver));
  1087. $table = (new TableSchema('schema_articles'))->addColumn('id', [
  1088. 'type' => 'integer',
  1089. 'null' => false
  1090. ]);
  1091. $table->temporary(true);
  1092. $sql = $table->createSql($connection);
  1093. $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]);
  1094. }
  1095. /**
  1096. * Test primary key generation & auto-increment.
  1097. *
  1098. * @return void
  1099. */
  1100. public function testCreateSqlCompositeIntegerKey()
  1101. {
  1102. $driver = $this->_getMockedDriver();
  1103. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1104. ->disableOriginalConstructor()
  1105. ->getMock();
  1106. $connection->expects($this->any())->method('getDriver')
  1107. ->will($this->returnValue($driver));
  1108. $table = (new TableSchema('articles_tags'))
  1109. ->addColumn('article_id', [
  1110. 'type' => 'integer',
  1111. 'null' => false
  1112. ])
  1113. ->addColumn('tag_id', [
  1114. 'type' => 'integer',
  1115. 'null' => false,
  1116. ])
  1117. ->addConstraint('primary', [
  1118. 'type' => 'primary',
  1119. 'columns' => ['article_id', 'tag_id']
  1120. ]);
  1121. $expected = <<<SQL
  1122. CREATE TABLE `articles_tags` (
  1123. `article_id` INTEGER NOT NULL,
  1124. `tag_id` INTEGER NOT NULL,
  1125. PRIMARY KEY (`article_id`, `tag_id`)
  1126. )
  1127. SQL;
  1128. $result = $table->createSql($connection);
  1129. $this->assertCount(1, $result);
  1130. $this->assertTextEquals($expected, $result[0]);
  1131. $table = (new TableSchema('composite_key'))
  1132. ->addColumn('id', [
  1133. 'type' => 'integer',
  1134. 'null' => false,
  1135. 'autoIncrement' => true
  1136. ])
  1137. ->addColumn('account_id', [
  1138. 'type' => 'integer',
  1139. 'null' => false,
  1140. ])
  1141. ->addConstraint('primary', [
  1142. 'type' => 'primary',
  1143. 'columns' => ['id', 'account_id']
  1144. ]);
  1145. $expected = <<<SQL
  1146. CREATE TABLE `composite_key` (
  1147. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1148. `account_id` INTEGER NOT NULL,
  1149. PRIMARY KEY (`id`, `account_id`)
  1150. )
  1151. SQL;
  1152. $result = $table->createSql($connection);
  1153. $this->assertCount(1, $result);
  1154. $this->assertTextEquals($expected, $result[0]);
  1155. }
  1156. /**
  1157. * test dropSql
  1158. *
  1159. * @return void
  1160. */
  1161. public function testDropSql()
  1162. {
  1163. $driver = $this->_getMockedDriver();
  1164. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1165. ->disableOriginalConstructor()
  1166. ->getMock();
  1167. $connection->expects($this->any())->method('getDriver')
  1168. ->will($this->returnValue($driver));
  1169. $table = new TableSchema('articles');
  1170. $result = $table->dropSql($connection);
  1171. $this->assertCount(1, $result);
  1172. $this->assertEquals('DROP TABLE `articles`', $result[0]);
  1173. }
  1174. /**
  1175. * Test truncateSql()
  1176. *
  1177. * @return void
  1178. */
  1179. public function testTruncateSql()
  1180. {
  1181. $driver = $this->_getMockedDriver();
  1182. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1183. ->disableOriginalConstructor()
  1184. ->getMock();
  1185. $connection->expects($this->any())->method('getDriver')
  1186. ->will($this->returnValue($driver));
  1187. $table = new TableSchema('articles');
  1188. $result = $table->truncateSql($connection);
  1189. $this->assertCount(1, $result);
  1190. $this->assertEquals('TRUNCATE TABLE `articles`', $result[0]);
  1191. }
  1192. /**
  1193. * Test that constructing a schema dialect connects the driver.
  1194. *
  1195. * @return void
  1196. */
  1197. public function testConstructConnectsDriver()
  1198. {
  1199. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  1200. $driver->expects($this->once())
  1201. ->method('connect');
  1202. $schema = new MysqlSchema($driver);
  1203. }
  1204. /**
  1205. * Tests json column parsing on Mysql 5.7+
  1206. *
  1207. * @return void
  1208. */
  1209. public function testDescribeJson()
  1210. {
  1211. $connection = ConnectionManager::get('test');
  1212. $this->_createTables($connection);
  1213. $this->skipIf(!$connection->driver()->supportsNativeJson(), 'Does not support native json');
  1214. $schema = new SchemaCollection($connection);
  1215. $result = $schema->describe('schema_json');
  1216. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  1217. $expected = [
  1218. 'type' => 'json',
  1219. 'null' => false,
  1220. 'default' => null,
  1221. 'length' => null,
  1222. 'precision' => null,
  1223. 'comment' => null,
  1224. ];
  1225. $this->assertEquals(
  1226. $expected,
  1227. $result->column('data'),
  1228. 'Field definition does not match for data'
  1229. );
  1230. }
  1231. /**
  1232. * Get a schema instance with a mocked driver/pdo instances
  1233. *
  1234. * @return MysqlSchema
  1235. */
  1236. protected function _getMockedDriver()
  1237. {
  1238. $driver = new Mysql();
  1239. $mock = $this->getMockBuilder(PDO::class)
  1240. ->setMethods(['quote', 'quoteIdentifier', 'getAttribute'])
  1241. ->disableOriginalConstructor()
  1242. ->getMock();
  1243. $mock->expects($this->any())
  1244. ->method('quote')
  1245. ->will($this->returnCallback(function ($value) {
  1246. return "'$value'";
  1247. }));
  1248. $driver->connection($mock);
  1249. return $driver;
  1250. }
  1251. }