From 27ddeaf67ba7d9fa76a971b4217ddc2da0b3a5b8 Mon Sep 17 00:00:00 2001 From: nojima Date: Tue, 8 Oct 2024 16:26:51 +0900 Subject: [PATCH] Fix for MySQL 8 DEFAULT_GENERATED extra --- src/Db/Adapter/MysqlAdapter.php | 11 ++++++++++- tests/TestCase/Db/Adapter/MysqlAdapterTest.php | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Db/Adapter/MysqlAdapter.php b/src/Db/Adapter/MysqlAdapter.php index 4930d584..359ec179 100644 --- a/src/Db/Adapter/MysqlAdapter.php +++ b/src/Db/Adapter/MysqlAdapter.php @@ -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']); } diff --git a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php index dc77d565..99ab98dc 100644 --- a/tests/TestCase/Db/Adapter/MysqlAdapterTest.php +++ b/tests/TestCase/Db/Adapter/MysqlAdapterTest.php @@ -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);