Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ protected function mapColumnData(array $data): array
$data['length'] = null;
}
$standardLengths = [TableSchema::LENGTH_TINY, TableSchema::LENGTH_MEDIUM, TableSchema::LENGTH_LONG];
if ($data['length'] !== null && !in_array($data['length'], $standardLengths, true)) {
if (
$data['length'] !== null &&
$data['length'] > TableSchema::LENGTH_TINY &&
!in_array($data['length'], $standardLengths, true)
) {
foreach ($standardLengths as $bucket) {
if ($bucket < $data['length']) {
continue;
Expand Down
32 changes: 30 additions & 2 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Migrations\Db\Table;
use Migrations\Db\Table\Column;
use Migrations\Db\Table\ForeignKey;
use Migrations\Db\Table\Index;
use PDO;
use PDOException;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -337,6 +338,27 @@ public function testCreateTableWithPrimaryKeyAsBinaryUuid()
$this->assertTrue($this->adapter->hasColumn('ztable', 'user_id'));
}

public function testCreateTableBinaryLengthWithIndex()
{
$table = new Table('ntable', [], $this->adapter);
$table
->addColumn('file', 'binary', [
'default' => null,
'length' => 20,
'null' => true,
])
->addIndex(
(new Index())
->setColumns(['file'])
->setName('file_idx')
->setType('unique'),
)
->create();
$this->assertTrue($this->adapter->hasColumn('ntable', 'id'));
$this->assertTrue($this->adapter->hasColumn('ntable', 'file'));
$this->assertTrue($this->adapter->hasIndex('ntable', 'file'));
}

/**
* @return void
*/
Expand Down Expand Up @@ -1071,8 +1093,11 @@ public static function binaryToBlobAutomaticConversionData()
return [
// When creating binary with limit > 255, MySQL auto-converts to BLOB
// input limit, expected SQL type name, expected column limit after round-trip
// For values smaller than 255, we preserve the length.
[null, 'blob', MysqlAdapter::BLOB_REGULAR], // binary(null) becomes BLOB
[64, 'tinyblob', MysqlAdapter::BLOB_TINY], // binary(64) becomes TINYBLOB
[64, 'binary', 64], // binary(64) becomes binary(64)
[254, 'binary', 254], // binary(254) becomes binary(254)
[255, 'tinyblob', MysqlAdapter::BLOB_TINY], // binary(255) becomes TINYBLOB
[MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
[MysqlAdapter::BLOB_REGULAR, 'blob', MysqlAdapter::BLOB_REGULAR],
[MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
Expand All @@ -1099,8 +1124,11 @@ public static function varbinaryToBlobAutomaticConversionData()
return [
// When creating varbinary with limit > 255, MySQL auto-converts to BLOB
// input limit, expected SQL type name, expected column limit after round-trip
// For values smaller than 255, we preserve the length.
[null, 'blob', MysqlAdapter::BLOB_REGULAR], // varbinary(null) becomes BLOB
[64, 'tinyblob', MysqlAdapter::BLOB_TINY], // varbinary(64) becomes TINYBLOB
[64, 'binary', 64], // varbinary(64) becomes binary(64)
[254, 'binary', 254], // varbinary(254) becomes binary(254)
[255, 'tinyblob', MysqlAdapter::BLOB_TINY], // varbinary(255) becomes TINYBLOB
[MysqlAdapter::BLOB_REGULAR - 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
[MysqlAdapter::BLOB_REGULAR, 'blob', MysqlAdapter::BLOB_REGULAR],
[MysqlAdapter::BLOB_REGULAR + 20, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
Expand Down