From 52f2381020d70256afdd35143161b4090a45fe54 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 4 May 2016 22:51:34 +0000 Subject: [PATCH] add generated columns support to mysql schema grammar --- .../Database/Schema/Grammars/MySqlGrammar.php | 30 ++++++++++++++++++- .../DatabaseMySqlSchemaGrammarTest.php | 12 ++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index b7141780e7b6..b6f31bbbe0f8 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -13,7 +13,7 @@ class MySqlGrammar extends Grammar * * @var array */ - protected $modifiers = ['Unsigned', 'Charset', 'Collate', 'Nullable', 'Default', 'Increment', 'Comment', 'After', 'First']; + protected $modifiers = ['VirtualAs', 'StoredAs', 'Unsigned', 'Charset', 'Collate', 'Nullable', 'Default', 'Increment', 'Comment', 'After', 'First']; /** * The possible column serials. @@ -624,6 +624,34 @@ protected function typeMacAddress(Fluent $column) return 'varchar(17)'; } + /** + * Get the SQL for a generated virtual column modifier. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * @return string|null + */ + protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) + { + if (! is_null($column->virtualAs)) { + return " as ({$column->virtualAs})"; + } + } + + /** + * Get the SQL for a generated stored column modifier. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $column + * @return string|null + */ + protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) + { + if (! is_null($column->storedAs)) { + return " as ({$column->storedAs}) stored"; + } + } + /** * Get the SQL for an unsigned column modifier. * diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 69eea6597aa9..54b4fa2045d1 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -328,6 +328,18 @@ public function testAddingColumnAfterAnotherColumn() $this->assertEquals('alter table `users` add `name` varchar(255) not null after `foo`', $statements[0]); } + public function testAddingGeneratedColumn() + { + $blueprint = new Blueprint('products'); + $blueprint->integer('price'); + $blueprint->integer('discounted_virtual')->virtualAs('price - 5'); + $blueprint->integer('discounted_stored')->storedAs('price - 5'); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertEquals(1, count($statements)); + $this->assertEquals('alter table `products` add `price` int not null, add `discounted_virtual` int as (price - 5) not null, add `discounted_stored` int as (price - 5) stored not null', $statements[0]); + } + public function testAddingString() { $blueprint = new Blueprint('users');