Skip to content

Commit

Permalink
Merge pull request #755 from nojimage/fixes-issue754
Browse files Browse the repository at this point in the history
Fix for MySQL 8 DEFAULT_GENERATED extra
  • Loading branch information
markstory authored Oct 8, 2024
2 parents d39616f + 27ddeaf commit 40b5ef9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,16 @@ protected function getRenameColumnInstructions(string $tableName, string $column
if (strcasecmp($row['Field'], $columnName) === 0) {
$null = $row['Null'] === 'NO' ? 'NOT NULL' : 'NULL';
$comment = isset($row['Comment']) ? ' COMMENT ' . '\'' . addslashes($row['Comment']) . '\'' : '';
$extra = ' ' . strtoupper($row['Extra']);

// create the extra string by also filtering out the DEFAULT_GENERATED option (MySQL 8 fix)
$extras = array_filter(
explode(' ', strtoupper($row['Extra'])),
static function ($value) {
return $value !== 'DEFAULT_GENERATED';
},
);
$extra = ' ' . implode(' ', $extras);

if (($row['Default'] !== null)) {
$extra .= $this->getDefaultValueDefinition($row['Default']);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,19 @@ public function testRenameColumnPreserveComment()
$this->assertEquals('comment1', $columns[1]['Comment']);
}

public function testRenameColumnWithDefaultGeneratedExtra()
{
$table = new Table('t', [], $this->adapter);
$table->save();
$this->assertFalse($table->hasColumn('last_changed'));
$table->addColumn('last_changed', 'datetime', ['default' => 'CURRENT_TIMESTAMP', 'null' => false])
->save();
$this->assertTrue($table->hasColumn('last_changed'));
$table->renameColumn('last_changed', 'last_changed2')->save();
$this->assertFalse($this->adapter->hasColumn('t', 'last_changed'));
$this->assertTrue($this->adapter->hasColumn('t', 'last_changed2'));
}

public function testRenamingANonExistentColumn()
{
$table = new Table('t', [], $this->adapter);
Expand Down

0 comments on commit 40b5ef9

Please sign in to comment.