Skip to content

Commit

Permalink
fix adding stored columns on sqlite (laravel#49638)
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari authored Jan 11, 2024
1 parent 0026a0a commit ca52cb3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
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

0 comments on commit ca52cb3

Please sign in to comment.