From ca52cb3c491de644d53b01c0ab1e4ac02788117b Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Thu, 11 Jan 2024 19:30:56 +0330 Subject: [PATCH] fix adding stored columns on sqlite (#49638) --- .../Database/Schema/Grammars/SQLiteGrammar.php | 4 +--- .../DatabaseSQLiteSchemaGrammarTest.php | 3 ++- .../Integration/Database/SchemaBuilderTest.php | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index fcbaf0798685..2c2656eaaa9f 100644 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -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(); } diff --git a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php index b43c30a3d2bb..7eee8a9d295b 100755 --- a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php +++ b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php @@ -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); } diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index 52ac4f7c17fc..e1fb45a26267 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -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');