Skip to content

Commit 28de0f0

Browse files
committed
Implement key lengths for MySQL.
Mysql is wonky and lets you set key lengths on indexes. Allow for that and acommodate it.
1 parent 871123e commit 28de0f0

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

lib/Cake/Database/Schema/MysqlSchema.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,17 @@ public function indexSql(Table $table, $name) {
296296
if ($data['type'] === Table::INDEX_FULLTEXT) {
297297
$out = 'FULLTEXT KEY ';
298298
}
299-
// TODO finish MySQL indexes with length
300299
$out .= $this->_driver->quoteIdentifier($name);
301300

302301
$columns = array_map(
303302
[$this->_driver, 'quoteIdentifier'],
304303
$data['columns']
305304
);
305+
foreach ($data['columns'] as $i => $column) {
306+
if (isset($data['length'][$column])) {
307+
$columns[$i] .= sprintf('(%d)', $data['length'][$column]);
308+
}
309+
}
306310
return $out . ' (' . implode(', ', $columns) . ')';
307311
}
308312

lib/Cake/Database/Schema/Table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Table {
7878
protected $_indexKeys = [
7979
'type' => null,
8080
'columns' => [],
81+
'length' => [],
8182
];
8283

8384
/**

lib/Cake/Test/TestCase/Database/Schema/MysqlSchemaTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ public static function createTableProvider() {
461461
UNIQUE KEY `unique_idx` (`author_id`, `article_id`)
462462
);
463463
SQL;
464+
464465
$simpleKey = (new Table('things'))
465466
->addColumn('thing_id', [
466467
'type' => 'integer',
@@ -489,12 +490,35 @@ public static function createTableProvider() {
489490
`stuff` TEXT,
490491
FULLTEXT KEY `stuff_idx` (`stuff`)
491492
);
493+
SQL;
494+
495+
$keyLength = (new Table('articles_authors'))
496+
->addColumn('author_id', [
497+
'type' => 'integer',
498+
'null' => false
499+
])
500+
->addColumn('article_id', [
501+
'type' => 'integer',
502+
'null' => false,
503+
])
504+
->addIndex('unique_idx', [
505+
'type' => 'unique',
506+
'columns' => ['author_id', 'article_id'],
507+
'length' => ['author_id' => 5, 'article_id' => 4],
508+
]);
509+
$keyLengthExpected = <<<SQL
510+
CREATE TABLE `articles_authors` (
511+
`author_id` INTEGER NOT NULL,
512+
`article_id` INTEGER NOT NULL,
513+
UNIQUE KEY `unique_idx` (`author_id`(5), `article_id`(4))
514+
);
492515
SQL;
493516
return [
494517
[$basic, $basicExpected],
495518
[$uniqueKey, $uniqueExpected],
496519
[$fullText, $fullTextExpected],
497520
[$simpleKey, $simpleKeyExpected],
521+
[$keyLength, $keyLengthExpected],
498522
];
499523
}
500524

0 commit comments

Comments
 (0)