From 74ad9002ff94c12d61bac5e9323baf8141c6e4d8 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 19 May 2013 22:13:14 -0400 Subject: [PATCH] Update MySQL schema to match how things will need to work. createTableSql will now need to get additional parameters to account for how postgres and sqlite need to create indexes + constraints differently. --- lib/Cake/Database/Schema/MysqlSchema.php | 44 +++++++++----- .../Database/Schema/MysqlSchemaTest.php | 60 ++++++++++++++----- 2 files changed, 74 insertions(+), 30 deletions(-) diff --git a/lib/Cake/Database/Schema/MysqlSchema.php b/lib/Cake/Database/Schema/MysqlSchema.php index 23d1f282166..3897539c5cb 100644 --- a/lib/Cake/Database/Schema/MysqlSchema.php +++ b/lib/Cake/Database/Schema/MysqlSchema.php @@ -172,12 +172,14 @@ public function extraSchemaColumns() { * Generate the SQL to create a table. * * @param string $table The name of the table. - * @param array $lines The lines (columns + indexes) to go inside the table. - * @return string A complete CREATE TABLE statement + * @param array $columns The columns to go inside the table. + * @param array $constraints The constraints for the table. + * @param array $indexes The indexes for the table. + * @return array A complete CREATE TABLE statement(s) */ - public function createTableSql($table, $lines) { - $content = implode(",\n", $lines); - return sprintf("CREATE TABLE `%s` (\n%s\n);", $table, $content); + public function createTableSql($table, $columns, $constraints, $indexes) { + $content = implode(",\n", array_merge($columns, $constraints, $indexes)); + return [sprintf("CREATE TABLE `%s` (\n%s\n);", $table, $content)]; } /** @@ -265,23 +267,17 @@ public function columnSql(Table $table, $name) { * @return string SQL fragment. */ public function constraintSql(Table $table, $name) { - $data = $table->index($name); - if ($data['type'] === Table::INDEX_PRIMARY) { + $data = $table->constraint($name); + if ($data['type'] === Table::CONSTRAINT_PRIMARY) { $columns = array_map( [$this->_driver, 'quoteIdentifier'], $data['columns'] ); return sprintf('PRIMARY KEY (%s)', implode(', ', $columns)); } - if ($data['type'] === Table::INDEX_UNIQUE) { + if ($data['type'] === Table::CONSTRAINT_UNIQUE) { $out = 'UNIQUE KEY '; } - if ($data['type'] === Table::INDEX_INDEX) { - $out = 'KEY '; - } - if ($data['type'] === Table::INDEX_FULLTEXT) { - $out = 'FULLTEXT KEY '; - } $out .= $this->_driver->quoteIdentifier($name); $columns = array_map( @@ -304,7 +300,25 @@ public function constraintSql(Table $table, $name) { * @return string SQL fragment. */ public function indexSql(Table $table, $name) { - return ''; + $data = $table->index($name); + if ($data['type'] === Table::INDEX_INDEX) { + $out = 'KEY '; + } + if ($data['type'] === Table::INDEX_FULLTEXT) { + $out = 'FULLTEXT KEY '; + } + $out .= $this->_driver->quoteIdentifier($name); + + $columns = array_map( + [$this->_driver, 'quoteIdentifier'], + $data['columns'] + ); + foreach ($data['columns'] as $i => $column) { + if (isset($data['length'][$column])) { + $columns[$i] .= sprintf('(%d)', $data['length'][$column]); + } + } + return $out . ' (' . implode(', ', $columns) . ')'; } } diff --git a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php index 8b5116fc5c9..dfb0ec4c514 100644 --- a/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php +++ b/lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php @@ -414,16 +414,6 @@ public static function constraintSqlProvider() { ['type' => 'unique', 'columns' => ['title', 'author_id']], 'UNIQUE KEY `unique_idx` (`title`, `author_id`)' ], - [ - 'key_key', - ['type' => 'index', 'columns' => ['author_id']], - 'KEY `key_key` (`author_id`)' - ], - [ - 'full_text', - ['type' => 'fulltext', 'columns' => ['title']], - 'FULLTEXT KEY `full_text` (`title`)' - ], [ 'length_idx', [ @@ -450,11 +440,50 @@ public function testConstraintSql($name, $data, $expected) { 'length' => 255 ])->addColumn('author_id', [ 'type' => 'integer', - ])->addIndex($name, $data); + ])->addConstraint($name, $data); $this->assertEquals($expected, $schema->constraintSql($table, $name)); } +/** + * Test provider for indexSql() + * + * @return array + */ + public static function indexSqlProvider() { + return [ + [ + 'key_key', + ['type' => 'index', 'columns' => ['author_id']], + 'KEY `key_key` (`author_id`)' + ], + [ + 'full_text', + ['type' => 'fulltext', 'columns' => ['title']], + 'FULLTEXT KEY `full_text` (`title`)' + ], + ]; + } + +/** + * Test the indexSql method. + * + * @dataProvider indexSqlProvider + */ + public function testIndexSql($name, $data, $expected) { + $driver = $this->_getMockedDriver(); + $schema = new MysqlSchema($driver); + + $table = (new Table('articles'))->addColumn('title', [ + 'type' => 'string', + 'length' => 255 + ])->addColumn('author_id', [ + 'type' => 'integer', + ])->addIndex($name, $data); + + $this->assertEquals($expected, $schema->indexSql($table, $name)); + } + /** * Test generating a column that is a primary key. * @@ -469,7 +498,7 @@ public function testColumnSqlPrimaryKey() { 'type' => 'integer', 'null' => false ]) - ->addIndex('primary', [ + ->addConstraint('primary', [ 'type' => 'primary', 'columns' => ['id'] ]); @@ -481,7 +510,7 @@ public function testColumnSqlPrimaryKey() { 'type' => 'biginteger', 'null' => false ]) - ->addIndex('primary', [ + ->addConstraint('primary', [ 'type' => 'primary', 'columns' => ['id'] ]); @@ -511,7 +540,7 @@ public function testCreateTableSql() { ]) ->addColumn('body', ['type' => 'text']) ->addColumn('created', 'datetime') - ->addIndex('primary', [ + ->addConstraint('primary', [ 'type' => 'primary', 'columns' => ['id'] ]); @@ -526,7 +555,8 @@ public function testCreateTableSql() { ); SQL; $result = $table->createTableSql($connection); - $this->assertEquals($expected, $result); + $this->assertCount(1, $result); + $this->assertEquals($expected, $result[0]); } /**