Skip to content

[11.x] Fix adding stored columns on SQLite #49638

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

Merged
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
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ public function compileAdd(Blueprint $blueprint, Fluent $command)
{
$columns = $this->prefixArray('add column', $this->getColumns($blueprint));

return collect($columns)->reject(function ($column) {
return preg_match('/as \(.*\) stored/', $column) > 0;
})->map(function ($column) use ($blueprint) {
return collect($columns)->map(function ($column) use ($blueprint) {
return 'alter table '.$this->wrapTable($blueprint).' '.$column;
})->all();
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,10 +882,11 @@ public function testAddingGeneratedColumn()
$blueprint->integer('discounted_stored')->storedAs('"price" - 5')->nullable(false);
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(2, $statements);
$this->assertCount(3, $statements);
$expected = [
'alter table "products" add column "price" integer not null',
'alter table "products" add column "discounted_virtual" integer not null as ("price" - 5)',
'alter table "products" add column "discounted_stored" integer not null as ("price" - 5) stored',
];
$this->assertSame($expected, $statements);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ public function testSystemVersionedTables()
DB::statement('create table `test` (`foo` int) WITH system versioning;');
}

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

Schema::create('test', function (Blueprint $table) {
$table->integer('price');
});

Schema::table('test', function (Blueprint $table) {
$table->integer('virtual_column')->virtualAs('"price" - 5');
$table->integer('stored_column')->storedAs('"price" - 5');
});

$this->assertTrue(Schema::hasColumns('test', ['virtual_column', 'stored_column']));
}

public function testAddingMacros()
{
Schema::macro('foo', fn () => 'foo');
Expand Down