Skip to content

Commit

Permalink
Rename to computed() & $expression, throw exceptions when not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
jlsjonas committed Jan 30, 2019
1 parent 66ce68d commit 5443dca
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,15 +823,15 @@ public function unsignedDecimal($column, $total = 8, $places = 2)
}

/**
* Create a new generated virtual column on the table.
* Create a new generated computed column on the table.
*
* @param string $column
* @param string $formula
* @param string $expression
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function virtual($column, $formula)
public function computed($column, $expression)
{
return $this->addColumn('virtual', $column, compact('formula'));
return $this->addColumn('computed', $column, compact('expression'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @method ColumnDefinition unsigned() Set the INTEGER column as UNSIGNED (MySQL)
* @method ColumnDefinition useCurrent() Set the TIMESTAMP column to use CURRENT_TIMESTAMP as default value
* @method ColumnDefinition virtualAs(string $expression) Create a virtual generated column (MySQL)
* @method ColumnDefinition persisted() Mark the virtual generated column as persistent (SQL Server)
* @method ColumnDefinition persisted() Mark the computed generated column as persistent (SQL Server)
*/
class ColumnDefinition extends Fluent
{
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Schema\Grammars;

use RuntimeException;
use Illuminate\Support\Fluent;
use Doctrine\DBAL\Schema\TableDiff;
use Illuminate\Database\Connection;
Expand Down Expand Up @@ -190,6 +191,18 @@ public function prefixArray($prefix, array $values)
}, $values);
}

/**
* Create the column definition for a generated computed column type.
*
* @param \Illuminate\Support\Fluent $column
*
* @throws \RuntimeException
*/
protected function typeComputed(Fluent $column)
{
throw new RuntimeException('The database driver in use does not support the computed type.');
}

/**
* Wrap a table in keyword identifiers.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database\Schema\Grammars;

use RuntimeException;
use Illuminate\Support\Fluent;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint;
Expand Down Expand Up @@ -833,6 +834,18 @@ public function typeMultiPolygon(Fluent $column)
return 'multipolygon';
}

/**
* Create the column definition for a generated computed column type.
*
* @param \Illuminate\Support\Fluent $column
*
* @throws \RuntimeException
*/
protected function typeComputed(Fluent $column)
{
throw new RuntimeException('The database driver in use requires a type, see virtualAs/storedAs modifiers.');
}

/**
* Get the SQL for a generated virtual column modifier.
*
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,14 @@ public function typeMultiPolygon(Fluent $column)
}

/**
* Create the column definition for a generated virtual column type.
* Create the column definition for a generated computed column type.
*
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
protected function typeVirtual(Fluent $column)
protected function typeComputed(Fluent $column)
{
return "as ({$column->formula})";
return "as ({$column->expression})";
}

/**
Expand All @@ -767,7 +767,7 @@ protected function modifyCollate(Blueprint $blueprint, Fluent $column)
*/
protected function modifyNullable(Blueprint $blueprint, Fluent $column)
{
if ($column->type !== 'virtual') {
if ($column->type !== 'computed') {
return $column->nullable ? ' null' : ' not null';
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ public function testAddingGeneratedColumn()
{
$blueprint = new Blueprint('products');
$blueprint->integer('price');
$blueprint->virtual('discounted_virtual', 'price - 5');
$blueprint->virtual('discounted_stored', 'price - 5')->persisted();
$blueprint->computed('discounted_virtual', 'price - 5');
$blueprint->computed('discounted_stored', 'price - 5')->persisted();
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertEquals('alter table "products" add "price" int not null, "discounted_virtual" as (price - 5), "discounted_stored" as (price - 5) persisted', $statements[0]);
Expand Down

0 comments on commit 5443dca

Please sign in to comment.