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

[11.x] Fix altering a table that has a column with default 0 on SQLite #51803

Merged
merged 1 commit into from
Jun 14, 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
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
'change' => true,
'type' => $column['type_name'],
'nullable' => $column['nullable'],
'default' => $column['default'] ? new Expression($column['default']) : null,
'default' => is_null($column['default']) ? null : new Expression($column['default']),
'autoIncrement' => $column['auto_increment'],
'collation' => $column['collation'],
'comment' => $column['comment'],
Expand Down
22 changes: 22 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -195,6 +196,27 @@ public function testRenameColumnWithDefault()
$this->assertEquals(collect(Schema::getColumns('test'))->firstWhere('name', 'new_bar')['default'], $defaultBar);
}

public function testModifyColumnWithZeroDefaultOnSqlite()
{
if ($this->driver !== 'sqlite') {
$this->markTestSkipped('Test requires a SQLite connection.');
}

Schema::create('test', static function (Blueprint $table) {
$table->integer('column_default_zero')->default(new Expression('0'));
$table->integer('column_to_change');
});

Schema::table('test', function (Blueprint $table) {
$table->smallInteger('column_to_change')->default(new Expression('0'))->change();
});

$columns = collect(Schema::getColumns('test'));

$this->assertSame('0', $columns->firstWhere('name', 'column_default_zero')['default']);
$this->assertSame('0', $columns->firstWhere('name', 'column_to_change')['default']);
}

public function testCompoundPrimaryWithAutoIncrement()
{
if ($this->driver === 'sqlite') {
Expand Down