Skip to content

Commit

Permalink
fix renaming columns with null default on legacy DBs (#51177)
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari authored Apr 22, 2024
1 parent 1355ba2 commit 077eba3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ protected function compileLegacyRenameColumn(Blueprint $blueprint, Fluent $comma
default => $column['type_name'],
},
'nullable' => $column['nullable'],
'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp')
'default' => $column['default'] && (str_starts_with(strtolower($column['default']), 'current_timestamp') || $column['default'] === 'NULL')
? new Expression($column['default'])
: $column['default'],
'autoIncrement' => $column['auto_increment'],
Expand Down
3 changes: 3 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public function testNativeRenameColumnOnLegacyMariaDB()
$table->renameColumn('name', 'title');
$table->renameColumn('id', 'key');
$table->renameColumn('generated', 'new_generated');
$table->renameColumn('foo', 'bar');
});

$connection = m::mock(Connection::class);
Expand All @@ -224,12 +225,14 @@ public function testNativeRenameColumnOnLegacyMariaDB()
['name' => 'name', 'type' => 'varchar(255)', 'type_name' => 'varchar', 'nullable' => true, 'collation' => 'utf8mb4_unicode_ci', 'default' => 'foo', 'comment' => null, 'auto_increment' => false],
['name' => 'id', 'type' => 'bigint unsigned', 'type_name' => 'bigint', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => 'lorem ipsum', 'auto_increment' => true],
['name' => 'generated', 'type' => 'int', 'type_name' => 'int', 'nullable' => false, 'collation' => null, 'default' => null, 'comment' => null, 'auto_increment' => false, 'generation' => ['type' => 'stored', 'expression' => 'expression']],
['name' => 'foo', 'type' => 'int', 'type_name' => 'int', 'nullable' => true, 'collation' => null, 'default' => 'NULL', 'comment' => null, 'auto_increment' => false, 'generation' => null],
]);

$this->assertEquals([
"alter table `users` change `name` `title` varchar(255) collate 'utf8mb4_unicode_ci' null default 'foo'",
"alter table `users` change `id` `key` bigint unsigned not null auto_increment comment 'lorem ipsum'",
'alter table `users` change `generated` `new_generated` int as (expression) stored not null',
'alter table `users` change `foo` `bar` int null default NULL',
], $blueprint->toSql($connection, new MariaDbGrammar));
}

Expand Down

0 comments on commit 077eba3

Please sign in to comment.