Skip to content

Commit 813ab71

Browse files
committed
Remove null collate option from generated migrations
When baking migration diffs, the collate option was included in column options even when null. This caused "collate is not a valid column option" errors when running migrations on PostgreSQL. Fixes #974
1 parent ea48012 commit 813ab71

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/View/Helper/MigrationHelper.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,9 @@ public function getColumnOption(array $options): array
401401
if (empty($columnOptions['autoIncrement'])) {
402402
unset($columnOptions['autoIncrement']);
403403
}
404+
if (empty($columnOptions['collate'])) {
405+
unset($columnOptions['collate']);
406+
}
404407

405408
// currently only MySQL supports the signed option
406409
$driver = $connection->getDriver();

tests/TestCase/View/Helper/MigrationHelperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Migrations\Test\TestCase\View\Helper;
1515

1616
use Cake\Database\Driver\Mysql;
17+
use Cake\Database\Driver\Postgres;
1718
use Cake\Database\Driver\Sqlserver;
1819
use Cake\Database\Schema\Collection;
1920
use Cake\Datasource\ConnectionManager;
@@ -397,4 +398,26 @@ public function testStringifyList()
397398
],
398399
]));
399400
}
401+
402+
/**
403+
* Test that getColumnOption removes null collate for all databases
404+
*
405+
* @see https://github.com/cakephp/migrations/issues/974
406+
*/
407+
public function testGetColumnOptionRemovesNullCollate(): void
408+
{
409+
$options = [
410+
'length' => 255,
411+
'null' => true,
412+
'default' => null,
413+
'collate' => null,
414+
];
415+
416+
$result = $this->helper->getColumnOption($options);
417+
418+
// collate => null should NOT be in the output for any database
419+
// because it causes "collate is not a valid column option" error
420+
$this->assertArrayNotHasKey('collate', $result, 'collate => null should be removed');
421+
$this->assertArrayNotHasKey('collation', $result, 'collation should not be set when collate is null');
422+
}
400423
}

0 commit comments

Comments
 (0)