Skip to content

Commit 3362af3

Browse files
committed
Fix column collation not applied for uuid type
The MysqlSchemaDialect in cakephp/database only adds COLLATE for text, char, and string types. Since uuid columns generate CHAR(36) but use the 'uuid' type identifier, collation was being ignored. This fix converts uuid to char(36) in mapColumnData() so the dialect properly handles the collation. Fixes the issue reported in #1020.
1 parent 5394e4f commit 3362af3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/Db/Adapter/MysqlAdapter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ public function createTable(Table $table, array $columns = [], array $indexes =
274274
*/
275275
protected function mapColumnData(array $data): array
276276
{
277+
// Convert uuid to char(36) to ensure collation support.
278+
// The dialect only adds COLLATE for text, char, and string types.
279+
if ($data['type'] === self::PHINX_TYPE_UUID) {
280+
$data['type'] = 'char';
281+
$data['length'] = 36;
282+
}
283+
277284
if ($data['type'] == self::PHINX_TYPE_TEXT && $data['length'] !== null) {
278285
$data['length'] = match ($data['length']) {
279286
self::TEXT_LONG => TableSchema::LENGTH_LONG,

tests/TestCase/Db/Adapter/MysqlAdapterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,29 @@ public function testCreateTable()
166166
$this->assertFalse($columns[0]->isSigned());
167167
}
168168

169+
public function testCreateTableWithColumnCollation()
170+
{
171+
$table = new Table\Table('custom_collation', ['id' => false, 'primary_key' => ['id'], 'collation' => 'utf8mb4_unicode_ci']);
172+
173+
$columnId = new Column();
174+
$columnId
175+
->setName('id')
176+
->setType('uuid')
177+
->setCollation('ascii_general_ci')
178+
->setEncoding('ascii');
179+
$columnName = new Column();
180+
$columnName
181+
->setName('name')
182+
->setType('text');
183+
$columns = [$columnId, $columnName];
184+
185+
$this->adapter->createTable($table, $columns);
186+
187+
$rows = $this->adapter->fetchAll('SHOW FULL COLUMNS FROM custom_collation');
188+
$this->assertEquals('ascii_general_ci', $rows[0]['Collation']); // specified collation is set
189+
$this->assertEquals('utf8mb4_unicode_ci', $rows[1]['Collation']); // if not specified uses table's collation
190+
}
191+
169192
public function testCreateTableWithComment()
170193
{
171194
$tableComment = 'Table comment';

0 commit comments

Comments
 (0)