Skip to content

Commit

Permalink
Update MySQL schema to match how things will need to work.
Browse files Browse the repository at this point in the history
createTableSql will now need to get additional parameters to account for
how postgres and sqlite need to create indexes + constraints
differently.
  • Loading branch information
markstory committed May 20, 2013
1 parent 0a83f4f commit 74ad900
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 30 deletions.
44 changes: 29 additions & 15 deletions lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -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)];
}

/**
Expand Down Expand Up @@ -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(
Expand All @@ -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) . ')';
}

}
60 changes: 45 additions & 15 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -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',
[
Expand All @@ -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.
*
Expand All @@ -469,7 +498,7 @@ public function testColumnSqlPrimaryKey() {
'type' => 'integer',
'null' => false
])
->addIndex('primary', [
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
]);
Expand All @@ -481,7 +510,7 @@ public function testColumnSqlPrimaryKey() {
'type' => 'biginteger',
'null' => false
])
->addIndex('primary', [
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
]);
Expand Down Expand Up @@ -511,7 +540,7 @@ public function testCreateTableSql() {
])
->addColumn('body', ['type' => 'text'])
->addColumn('created', 'datetime')
->addIndex('primary', [
->addConstraint('primary', [
'type' => 'primary',
'columns' => ['id']
]);
Expand All @@ -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]);
}

/**
Expand Down

0 comments on commit 74ad900

Please sign in to comment.