Skip to content

Commit

Permalink
Fix for MySQL 8 DEFAULT_GENERATED extra (#2253)
Browse files Browse the repository at this point in the history
* Fix for MySQL8 DEFAULT_GENERATED extra

* cleanup

* Improving comment

* linting fixes

* Linting fix for newline for the return

* Adding test testRenameColumnWithDefaultGeneratedExtra

* Simulating test fail

* Testing done

---------

Co-authored-by: Sergei Selivanov <sergei@testlio.com>
  • Loading branch information
cergfix and Sergei Selivanov committed Dec 28, 2023
1 parent afee0ad commit 5b42393
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,17 @@ 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'])), function ($value) {
if ($value == 'DEFAULT_GENERATED') {
return false;
}

return true;
});
$extra = implode(' ', $extras);

if (($row['Default'] !== null)) {
$extra .= $this->getDefaultValueDefinition($row['Default']);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,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 5b42393

Please sign in to comment.