Skip to content

Commit e6af8fd

Browse files
committed
Fix up blob columns.
1 parent 62271dc commit e6af8fd

2 files changed

Lines changed: 19 additions & 41 deletions

File tree

src/Db/Adapter/MysqlAdapter.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -469,29 +469,9 @@ protected function mapColumnType(array $columnData): array
469469
} elseif ($type === TableSchema::TYPE_TIMESTAMP_FRACTIONAL) {
470470
$type = 'timestamp';
471471
$length = $columnData['precision'] ?? $length;
472-
} elseif ($type === TableSchema::TYPE_BINARY) {
473-
// CakePHP returns BLOB columns as 'binary' with specific lengths
474-
// Check the raw MySQL type to distinguish BLOB from BINARY columns
475-
$rawType = $columnData['rawType'] ?? '';
476-
if (str_contains($rawType, 'blob')) {
477-
// Map BLOB columns back to the appropriate BLOB types
478-
if (str_contains($rawType, 'tinyblob')) {
479-
$type = static::PHINX_TYPE_TINYBLOB;
480-
$length = static::BLOB_TINY;
481-
} elseif (str_contains($rawType, 'mediumblob')) {
482-
$type = static::PHINX_TYPE_MEDIUMBLOB;
483-
$length = static::BLOB_MEDIUM;
484-
} elseif (str_contains($rawType, 'longblob')) {
485-
$type = static::PHINX_TYPE_LONGBLOB;
486-
$length = static::BLOB_LONG;
487-
} else {
488-
// Regular BLOB
489-
$type = static::PHINX_TYPE_BLOB;
490-
$length = static::BLOB_REGULAR;
491-
}
492-
}
493-
// else: keep as binary or varbinary (actual BINARY/VARBINARY column)
494472
}
473+
// Note: TYPE_BINARY columns (including BLOB variants) remain as 'binary' type
474+
// with length parameter, aligned with CakePHP core's approach.
495475

496476
return [$type, $length];
497477
}
@@ -504,16 +484,8 @@ public function getColumns(string $tableName): array
504484
$dialect = $this->getSchemaDialect();
505485
$columnRecords = $dialect->describeColumns($tableName);
506486

507-
// Fetch raw column types to distinguish BLOB from BINARY columns
508-
$rawTypes = [];
509-
$rows = $this->fetchAll(sprintf('SHOW COLUMNS FROM %s', $this->quoteTableName($tableName)));
510-
foreach ($rows as $row) {
511-
$rawTypes[$row['Field']] = strtolower($row['Type']);
512-
}
513-
514487
$columns = [];
515488
foreach ($columnRecords as $record) {
516-
$record['rawType'] = $rawTypes[$record['name']] ?? null;
517489
[$type, $length] = $this->mapColumnType($record);
518490

519491
$column = (new Column())

tests/TestCase/Db/Adapter/MysqlAdapterTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,19 +1170,20 @@ public function testblobColumns(string $type, string $expectedType, ?int $limit,
11701170
public static function blobRoundTripData()
11711171
{
11721172
return [
1173-
// type, limit, expected type after round-trip, expected limit after round-trip
1174-
['blob', null, 'blob', MysqlAdapter::BLOB_REGULAR],
1175-
['blob', MysqlAdapter::BLOB_REGULAR, 'blob', MysqlAdapter::BLOB_REGULAR],
1176-
['tinyblob', null, 'tinyblob', MysqlAdapter::BLOB_TINY],
1177-
['mediumblob', null, 'mediumblob', MysqlAdapter::BLOB_MEDIUM],
1178-
['longblob', null, 'longblob', MysqlAdapter::BLOB_LONG],
1173+
// type, limit, expected type after round-trip (as 'binary'), expected limit after round-trip
1174+
// This aligns with CakePHP core which uses 'binary' type with length parameter
1175+
['blob', null, 'binary', null],
1176+
['blob', MysqlAdapter::BLOB_REGULAR, 'binary', null],
1177+
['tinyblob', null, 'binary', TableSchema::LENGTH_TINY],
1178+
['mediumblob', null, 'binary', TableSchema::LENGTH_MEDIUM],
1179+
['longblob', null, 'binary', TableSchema::LENGTH_LONG],
11791180
];
11801181
}
11811182

11821183
#[DataProvider('blobRoundTripData')]
1183-
public function testBlobRoundTrip(string $type, ?int $limit, string $expectedType, int $expectedLimit)
1184+
public function testBlobRoundTrip(string $type, ?int $limit, string $expectedType, ?int $expectedLimit)
11841185
{
1185-
// Create a table with a BLOB column
1186+
// Create a table with a BLOB column using Phinx-style type names
11861187
$table = new Table('blob_round_trip_test', [], $this->adapter);
11871188
$table->addColumn('blob_col', $type, ['limit' => $limit])
11881189
->save();
@@ -1200,12 +1201,17 @@ public function testBlobRoundTrip(string $type, ?int $limit, string $expectedTyp
12001201
}
12011202

12021203
$this->assertNotNull($blobColumn, 'BLOB column not found');
1204+
1205+
// After round-trip, blob columns are returned as 'binary' type with length (CakePHP style)
12031206
$this->assertSame($expectedType, $blobColumn->getType(), 'Type mismatch after round-trip');
12041207
$this->assertSame($expectedLimit, $blobColumn->getLimit(), 'Limit mismatch after round-trip');
12051208

1206-
// Verify that the SQL type is correct
1207-
$sqlType = $this->adapter->getSqlType($blobColumn->getType(), $blobColumn->getLimit());
1208-
$this->assertSame($type, $sqlType['name']);
1209+
// Verify that re-creating with the round-trip values produces an appropriate blob type
1210+
if ($expectedLimit !== null) {
1211+
$sqlType = $this->adapter->getSqlType($expectedType, $expectedLimit);
1212+
// For binary type with large limits, getSqlType delegates to blob logic
1213+
$this->assertContains($sqlType['name'], ['binary', 'tinyblob', 'blob', 'mediumblob', 'longblob']);
1214+
}
12091215

12101216
// Clean up
12111217
$this->adapter->dropTable('blob_round_trip_test');

0 commit comments

Comments
 (0)