Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for MySQL 8 DEFAULT_GENERATED extra #755

Merged
merged 1 commit into from
Oct 8, 2024
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
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 @@
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';
},
);

Check warning on line 532 in src/Db/Adapter/MysqlAdapter.php

View check run for this annotation

Codecov / codecov/patch

src/Db/Adapter/MysqlAdapter.php#L532

Added line #L532 was not covered by tests
$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
Loading