Skip to content
Permalink
Browse files

Implement key lengths for MySQL.

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 28de0f04944186e1ca9124cc4937e58146b855d6
@@ -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) . ')';
}
@@ -78,6 +78,7 @@ class Table {
protected $_indexKeys = [
'type' => null,
'columns' => [],
'length' => [],
];
/**
@@ -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',
@@ -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],
];
}

0 comments on commit 28de0f0

Please sign in to comment.
You can’t perform that action at this time.