Skip to content

Commit 9ca3cad

Browse files
committed
Merge remote-tracking branch 'origin/4.x' into 5.x
# Conflicts: # tests/TestCase/Db/Adapter/MysqlAdapterTest.php
2 parents f1ec4ef + d0f5e3c commit 9ca3cad

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/Db/Adapter/MysqlAdapter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,11 @@ protected function mapColumnData(array $data): array
387387
$data['length'] = null;
388388
}
389389
$standardLengths = [TableSchema::LENGTH_TINY, TableSchema::LENGTH_MEDIUM, TableSchema::LENGTH_LONG];
390-
if ($data['length'] !== null && !in_array($data['length'], $standardLengths, true)) {
390+
if (
391+
$data['length'] !== null &&
392+
$data['length'] > TableSchema::LENGTH_TINY &&
393+
!in_array($data['length'], $standardLengths, true)
394+
) {
391395
foreach ($standardLengths as $bucket) {
392396
if ($bucket < $data['length']) {
393397
continue;

tests/TestCase/Db/Adapter/MysqlAdapterTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Migrations\Db\Table\CheckConstraint;
1818
use Migrations\Db\Table\Column;
1919
use Migrations\Db\Table\ForeignKey;
20+
use Migrations\Db\Table\Index;
2021
use Migrations\Db\Table\Partition;
2122
use PDO;
2223
use PDOException;
@@ -350,6 +351,27 @@ public function testCreateTableWithPrimaryKeyAsBinaryUuid()
350351
$this->assertTrue($this->adapter->hasColumn('ztable', 'user_id'));
351352
}
352353

354+
public function testCreateTableBinaryLengthWithIndex()
355+
{
356+
$table = new Table('ntable', [], $this->adapter);
357+
$table
358+
->addColumn('file', 'binary', [
359+
'default' => null,
360+
'length' => 20,
361+
'null' => true,
362+
])
363+
->addIndex(
364+
(new Index())
365+
->setColumns(['file'])
366+
->setName('file_idx')
367+
->setType('unique'),
368+
)
369+
->create();
370+
$this->assertTrue($this->adapter->hasColumn('ntable', 'id'));
371+
$this->assertTrue($this->adapter->hasColumn('ntable', 'file'));
372+
$this->assertTrue($this->adapter->hasIndex('ntable', 'file'));
373+
}
374+
353375
/**
354376
* @return void
355377
*/
@@ -1224,8 +1246,11 @@ public static function binaryToBlobAutomaticConversionData()
12241246
return [
12251247
// When creating binary with limit > 255, MySQL auto-converts to BLOB
12261248
// input limit, expected SQL type name, expected column limit after round-trip
1249+
// For values smaller than 255, we preserve the length.
12271250
[null, 'blob', MysqlAdapter::BLOB_REGULAR], // binary(null) becomes BLOB
1228-
[64, 'tinyblob', MysqlAdapter::BLOB_TINY], // binary(64) becomes TINYBLOB
1251+
[64, 'binary', 64], // binary(64) becomes binary(64)
1252+
[254, 'binary', 254], // binary(254) becomes binary(254)
1253+
[255, 'tinyblob', MysqlAdapter::BLOB_TINY], // binary(255) becomes TINYBLOB
12291254
[MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
12301255
[MysqlAdapter::BLOB_REGULAR, 'blob', MysqlAdapter::BLOB_REGULAR],
12311256
[MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
@@ -1251,8 +1276,11 @@ public static function varbinaryToBlobAutomaticConversionData()
12511276
return [
12521277
// When creating varbinary with limit > 255, MySQL auto-converts to BLOB
12531278
// input limit, expected SQL type name, expected column limit after round-trip
1279+
// For values smaller than 255, we preserve the length.
12541280
[null, 'blob', MysqlAdapter::BLOB_REGULAR], // varbinary(null) becomes BLOB
1255-
[64, 'tinyblob', MysqlAdapter::BLOB_TINY], // varbinary(64) becomes TINYBLOB
1281+
[64, 'binary', 64], // varbinary(64) becomes binary(64)
1282+
[254, 'binary', 254], // varbinary(254) becomes binary(254)
1283+
[255, 'tinyblob', MysqlAdapter::BLOB_TINY], // varbinary(255) becomes TINYBLOB
12561284
[MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
12571285
[MysqlAdapter::BLOB_REGULAR, 'blob', MysqlAdapter::BLOB_REGULAR],
12581286
[MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],

0 commit comments

Comments
 (0)