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
4 changes: 3 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,10 @@ public function createTable(TableMetadata $table, array $columns = [], array $in
protected function mapColumnData(array $data): array
{
if ($data['type'] == self::TYPE_TEXT && $data['length'] !== null) {
// Accept both migrations TEXT_LONG and CakePHP LENGTH_LONG for backward compatibility
// with migrations generated before the fix (LENGTH_TINY/MEDIUM are already equal to TEXT_TINY/MEDIUM)
$data['length'] = match ($data['length']) {
self::TEXT_LONG => TableSchema::LENGTH_LONG,
self::TEXT_LONG, TableSchema::LENGTH_LONG => TableSchema::LENGTH_LONG,
self::TEXT_MEDIUM => TableSchema::LENGTH_MEDIUM,
self::TEXT_REGULAR => null,
self::TEXT_TINY => TableSchema::LENGTH_TINY,
Expand Down
9 changes: 9 additions & 0 deletions src/View/Helper/MigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Database\Schema\CollectionInterface;
use Cake\Database\Schema\TableSchema;
use Cake\Database\Schema\TableSchemaInterface;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\View\Helper;
use Cake\View\View;
use Migrations\Db\Adapter\MysqlAdapter;
use Migrations\Db\Table\ForeignKey;

/**
Expand Down Expand Up @@ -445,6 +447,13 @@ public function getColumnOption(array $options): array
}
}

// Convert CakePHP's LENGTH_LONG to migrations TEXT_LONG for text columns
// CakePHP uses LENGTH_LONG = 4294967295, but migrations expects TEXT_LONG = 2147483647
// (LENGTH_TINY and LENGTH_MEDIUM have the same values as TEXT_TINY and TEXT_MEDIUM)
if (isset($columnOptions['limit']) && $columnOptions['limit'] === TableSchema::LENGTH_LONG) {
$columnOptions['limit'] = MysqlAdapter::TEXT_LONG;
}

return $columnOptions;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Database\Schema\TableSchema;
use Cake\Datasource\ConnectionManager;
use InvalidArgumentException;
use Migrations\Db\Adapter\MysqlAdapter;
Expand Down Expand Up @@ -1377,6 +1378,9 @@ public static function textRoundTripData()
['text', MysqlAdapter::TEXT_TINY, 'text', MysqlAdapter::TEXT_TINY],
['text', MysqlAdapter::TEXT_MEDIUM, 'text', MysqlAdapter::TEXT_MEDIUM],
['text', MysqlAdapter::TEXT_LONG, 'text', MysqlAdapter::TEXT_LONG],
// Test backward compatibility: CakePHP's LENGTH_LONG (4294967295) should also work
// This ensures migrations generated before the fix still create LONGTEXT correctly
['text', TableSchema::LENGTH_LONG, 'text', MysqlAdapter::TEXT_LONG],
];
}

Expand Down
38 changes: 38 additions & 0 deletions tests/TestCase/View/Helper/MigrationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Sqlserver;
use Cake\Database\Schema\Collection;
use Cake\Database\Schema\TableSchema;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
use Cake\View\View;
use Migrations\Db\Adapter\MysqlAdapter;
use Migrations\View\Helper\MigrationHelper;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

Expand Down Expand Up @@ -490,4 +492,40 @@ public function testGetColumnOptionExcludesFixedWhenNotSet(): void

$this->assertArrayNotHasKey('fixed', $result);
}

/**
* Test that getColumnOption converts CakePHP's LENGTH_LONG to migrations TEXT_LONG
*
* CakePHP uses LENGTH_LONG = 4294967295 for LONGTEXT, but migrations expects
* TEXT_LONG = 2147483647. This ensures generated migrations use the correct value.
*/
public function testGetColumnOptionConvertsLengthLongToTextLong(): void
{
$options = [
'limit' => TableSchema::LENGTH_LONG, // 4294967295
'null' => true,
'default' => null,
];

$result = $this->helper->getColumnOption($options);

$this->assertArrayHasKey('limit', $result);
$this->assertSame(MysqlAdapter::TEXT_LONG, $result['limit']); // 2147483647
}

/**
* Test that getColumnOption preserves other limit values unchanged
*/
public function testGetColumnOptionPreservesOtherLimits(): void
{
$options = [
'limit' => 255, // TEXT_TINY / LENGTH_TINY - same value
'null' => true,
'default' => null,
];

$result = $this->helper->getColumnOption($options);

$this->assertSame(255, $result['limit']);
}
}
Loading