Skip to content

Commit ca52cb3

Browse files
fix adding stored columns on sqlite (#49638)
1 parent 0026a0a commit ca52cb3

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,7 @@ public function compileAdd(Blueprint $blueprint, Fluent $command)
269269
{
270270
$columns = $this->prefixArray('add column', $this->getColumns($blueprint));
271271

272-
return collect($columns)->reject(function ($column) {
273-
return preg_match('/as \(.*\) stored/', $column) > 0;
274-
})->map(function ($column) use ($blueprint) {
272+
return collect($columns)->map(function ($column) use ($blueprint) {
275273
return 'alter table '.$this->wrapTable($blueprint).' '.$column;
276274
})->all();
277275
}

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,11 @@ public function testAddingGeneratedColumn()
882882
$blueprint->integer('discounted_stored')->storedAs('"price" - 5')->nullable(false);
883883
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
884884

885-
$this->assertCount(2, $statements);
885+
$this->assertCount(3, $statements);
886886
$expected = [
887887
'alter table "products" add column "price" integer not null',
888888
'alter table "products" add column "discounted_virtual" integer not null as ("price" - 5)',
889+
'alter table "products" add column "discounted_stored" integer not null as ("price" - 5) stored',
889890
];
890891
$this->assertSame($expected, $statements);
891892
}

tests/Integration/Database/SchemaBuilderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,24 @@ public function testSystemVersionedTables()
387387
DB::statement('create table `test` (`foo` int) WITH system versioning;');
388388
}
389389

390+
public function testAddingStoredColumnOnSqlite()
391+
{
392+
if ($this->driver !== 'sqlite') {
393+
$this->markTestSkipped('Test requires a SQLite connection.');
394+
}
395+
396+
Schema::create('test', function (Blueprint $table) {
397+
$table->integer('price');
398+
});
399+
400+
Schema::table('test', function (Blueprint $table) {
401+
$table->integer('virtual_column')->virtualAs('"price" - 5');
402+
$table->integer('stored_column')->storedAs('"price" - 5');
403+
});
404+
405+
$this->assertTrue(Schema::hasColumns('test', ['virtual_column', 'stored_column']));
406+
}
407+
390408
public function testAddingMacros()
391409
{
392410
Schema::macro('foo', fn () => 'foo');

0 commit comments

Comments
 (0)