Skip to content

Commit

Permalink
Implement key lengths for MySQL.
Browse files Browse the repository at this point in the history
Mysql is wonky and lets you set key lengths on indexes. Allow for that
and acommodate it.
  • Loading branch information
markstory committed May 7, 2013
1 parent 871123e commit 28de0f0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/Cake/Database/Schema/MysqlSchema.php
Expand Up @@ -296,13 +296,17 @@ public function indexSql(Table $table, $name) {
if ($data['type'] === Table::INDEX_FULLTEXT) {
$out = 'FULLTEXT KEY ';
}
// TODO finish MySQL indexes with length
$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) . ')';
}

Expand Down
1 change: 1 addition & 0 deletions lib/Cake/Database/Schema/Table.php
Expand Up @@ -78,6 +78,7 @@ class Table {
protected $_indexKeys = [
'type' => null,
'columns' => [],
'length' => [],
];

/**
Expand Down
24 changes: 24 additions & 0 deletions lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -461,6 +461,7 @@ public static function createTableProvider() {
UNIQUE KEY `unique_idx` (`author_id`, `article_id`)
);
SQL;

$simpleKey = (new Table('things'))
->addColumn('thing_id', [
'type' => 'integer',
Expand Down Expand Up @@ -489,12 +490,35 @@ public static function createTableProvider() {
`stuff` TEXT,
FULLTEXT KEY `stuff_idx` (`stuff`)
);
SQL;

$keyLength = (new Table('articles_authors'))
->addColumn('author_id', [
'type' => 'integer',
'null' => false
])
->addColumn('article_id', [
'type' => 'integer',
'null' => false,
])
->addIndex('unique_idx', [
'type' => 'unique',
'columns' => ['author_id', 'article_id'],
'length' => ['author_id' => 5, 'article_id' => 4],
]);
$keyLengthExpected = <<<SQL
CREATE TABLE `articles_authors` (
`author_id` INTEGER NOT NULL,
`article_id` INTEGER NOT NULL,
UNIQUE KEY `unique_idx` (`author_id`(5), `article_id`(4))
);
SQL;
return [
[$basic, $basicExpected],
[$uniqueKey, $uniqueExpected],
[$fullText, $fullTextExpected],
[$simpleKey, $simpleKeyExpected],
[$keyLength, $keyLengthExpected],
];
}

Expand Down

0 comments on commit 28de0f0

Please sign in to comment.