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
14 changes: 14 additions & 0 deletions src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,20 @@ protected function mapColumnType(array $columnData): array
}
}
// else: keep as binary or varbinary (actual BINARY/VARBINARY column)
} elseif ($type === TableSchema::TYPE_TEXT) {
// CakePHP returns TEXT columns as 'text' with specific lengths
// Check the raw MySQL type to distinguish TEXT variants
$rawType = $columnData['rawType'] ?? '';
if (str_contains($rawType, 'tinytext')) {
$length = static::TEXT_TINY;
} elseif (str_contains($rawType, 'mediumtext')) {
$length = static::TEXT_MEDIUM;
} elseif (str_contains($rawType, 'longtext')) {
$length = static::TEXT_LONG;
} else {
// Regular TEXT - use null to indicate default TEXT type
$length = null;
}
}

return [$type, $length];
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,37 @@ public function testBlobRoundTrip(string $type, ?int $limit, string $expectedTyp
$this->adapter->dropTable('blob_round_trip_test');
}

public static function textRoundTripData()
{
return [
// type, limit, expected type after round-trip, expected limit after round-trip
['text', null, 'text', null],
['text', MysqlAdapter::TEXT_TINY, 'text', MysqlAdapter::TEXT_TINY],
['text', MysqlAdapter::TEXT_MEDIUM, 'text', MysqlAdapter::TEXT_MEDIUM],
['text', MysqlAdapter::TEXT_LONG, 'text', MysqlAdapter::TEXT_LONG],
Comment on lines +1376 to +1379
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need all four? Wouldn't one pair suffice?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be reduced? Yes, technically. The code paths are symmetric.

Downsides of reducing:

  1. Each is a genuinely different MySQL column type with different storage characteristics
  2. If someone modifies the constants or the mapping, a missing test case could let a regression slip through
  3. It's testing 4 branches in that match statement - reducing means less branch coverage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of the duplication in the parameter lists. The number of scenarios seems required. In any case it doesn't need to be a blocker.

];
}

#[DataProvider('textRoundTripData')]
public function testTextRoundTrip(string $type, ?int $limit, string $expectedType, ?int $expectedLimit)
{
// Create a table with a TEXT column
$table = new Table('text_round_trip_test', [], $this->adapter);
$table->addColumn('text_col', $type, ['limit' => $limit])
->save();

// Read the column back from the database
$columns = $this->adapter->getColumns('text_round_trip_test');

$textColumn = $columns[1];
$this->assertNotNull($textColumn, 'TEXT column not found');
$this->assertSame($expectedType, $textColumn->getType(), 'Type mismatch after round-trip');
$this->assertSame($expectedLimit, $textColumn->getLimit(), 'Limit mismatch after round-trip');

// Clean up
$this->adapter->dropTable('text_round_trip_test');
}

public function testTimestampInvalidLimit()
{
$this->adapter->connect();
Expand Down