Skip to content

Commit d0f5e3c

Browse files
authored
Merge pull request #1000 from cakephp/binary-column-mysql
Fix small binary column creation
2 parents 7dbc620 + 8f07c53 commit d0f5e3c

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
@@ -305,7 +305,11 @@ protected function mapColumnData(array $data): array
305305
$data['length'] = null;
306306
}
307307
$standardLengths = [TableSchema::LENGTH_TINY, TableSchema::LENGTH_MEDIUM, TableSchema::LENGTH_LONG];
308-
if ($data['length'] !== null && !in_array($data['length'], $standardLengths, true)) {
308+
if (
309+
$data['length'] !== null &&
310+
$data['length'] > TableSchema::LENGTH_TINY &&
311+
!in_array($data['length'], $standardLengths, true)
312+
) {
309313
foreach ($standardLengths as $bucket) {
310314
if ($bucket < $data['length']) {
311315
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;
1818
use Migrations\Db\Table\Column;
1919
use Migrations\Db\Table\ForeignKey;
20+
use Migrations\Db\Table\Index;
2021
use PDO;
2122
use PDOException;
2223
use PHPUnit\Framework\Attributes\DataProvider;
@@ -337,6 +338,27 @@ public function testCreateTableWithPrimaryKeyAsBinaryUuid()
337338
$this->assertTrue($this->adapter->hasColumn('ztable', 'user_id'));
338339
}
339340

341+
public function testCreateTableBinaryLengthWithIndex()
342+
{
343+
$table = new Table('ntable', [], $this->adapter);
344+
$table
345+
->addColumn('file', 'binary', [
346+
'default' => null,
347+
'length' => 20,
348+
'null' => true,
349+
])
350+
->addIndex(
351+
(new Index())
352+
->setColumns(['file'])
353+
->setName('file_idx')
354+
->setType('unique'),
355+
)
356+
->create();
357+
$this->assertTrue($this->adapter->hasColumn('ntable', 'id'));
358+
$this->assertTrue($this->adapter->hasColumn('ntable', 'file'));
359+
$this->assertTrue($this->adapter->hasIndex('ntable', 'file'));
360+
}
361+
340362
/**
341363
* @return void
342364
*/
@@ -1071,8 +1093,11 @@ public static function binaryToBlobAutomaticConversionData()
10711093
return [
10721094
// When creating binary with limit > 255, MySQL auto-converts to BLOB
10731095
// input limit, expected SQL type name, expected column limit after round-trip
1096+
// For values smaller than 255, we preserve the length.
10741097
[null, 'blob', MysqlAdapter::BLOB_REGULAR], // binary(null) becomes BLOB
1075-
[64, 'tinyblob', MysqlAdapter::BLOB_TINY], // binary(64) becomes TINYBLOB
1098+
[64, 'binary', 64], // binary(64) becomes binary(64)
1099+
[254, 'binary', 254], // binary(254) becomes binary(254)
1100+
[255, 'tinyblob', MysqlAdapter::BLOB_TINY], // binary(255) becomes TINYBLOB
10761101
[MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
10771102
[MysqlAdapter::BLOB_REGULAR, 'blob', MysqlAdapter::BLOB_REGULAR],
10781103
[MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
@@ -1099,8 +1124,11 @@ public static function varbinaryToBlobAutomaticConversionData()
10991124
return [
11001125
// When creating varbinary with limit > 255, MySQL auto-converts to BLOB
11011126
// input limit, expected SQL type name, expected column limit after round-trip
1127+
// For values smaller than 255, we preserve the length.
11021128
[null, 'blob', MysqlAdapter::BLOB_REGULAR], // varbinary(null) becomes BLOB
1103-
[64, 'tinyblob', MysqlAdapter::BLOB_TINY], // varbinary(64) becomes TINYBLOB
1129+
[64, 'binary', 64], // varbinary(64) becomes binary(64)
1130+
[254, 'binary', 254], // varbinary(254) becomes binary(254)
1131+
[255, 'tinyblob', MysqlAdapter::BLOB_TINY], // varbinary(255) becomes TINYBLOB
11041132
[MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
11051133
[MysqlAdapter::BLOB_REGULAR, 'blob', MysqlAdapter::BLOB_REGULAR],
11061134
[MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],

0 commit comments

Comments
 (0)