Skip to content

Commit

Permalink
[6.x] Add generated columns (virtual/stored) support for PostgreSQL (#…
Browse files Browse the repository at this point in the history
…30971)

* Add generated columns (virtual/stored) support for PostgreSQL

* Fix StyleCI issue
  • Loading branch information
frodeknutsen authored and taylorotwell committed Dec 28, 2019
1 parent c0fdb56 commit ea47c51
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Expand Up @@ -19,7 +19,7 @@ class PostgresGrammar extends Grammar
*
* @var array
*/
protected $modifiers = ['Collate', 'Increment', 'Nullable', 'Default'];
protected $modifiers = ['Collate', 'Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs'];

/**
* The columns available as serials.
Expand Down Expand Up @@ -952,4 +952,32 @@ protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
return ' primary key';
}
}

/**
* 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 ($column->virtualAs !== null) {
return " generated always 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 ($column->storedAs !== null) {
return " generated always as ({$column->storedAs}) stored";
}
}
}
20 changes: 20 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Expand Up @@ -742,6 +742,26 @@ public function testAddingGeneratedAs()
$this->assertSame('alter table "users" add column "foo" integer generated by default as identity not null', $statements[0]);
}

public function testAddingVirtualAs()
{
$blueprint = new Blueprint('users');
$blueprint->integer('foo')->nullable();
$blueprint->boolean('bar')->virtualAs('foo is not null');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" integer null, add column "bar" boolean not null generated always as (foo is not null)', $statements[0]);
}

public function testAddingStoredAs()
{
$blueprint = new Blueprint('users');
$blueprint->integer('foo')->nullable();
$blueprint->boolean('bar')->storedAs('foo is not null');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
$this->assertCount(1, $statements);
$this->assertSame('alter table "users" add column "foo" integer null, add column "bar" boolean not null generated always as (foo is not null) stored', $statements[0]);
}

public function testAddingIpAddress()
{
$blueprint = new Blueprint('users');
Expand Down

0 comments on commit ea47c51

Please sign in to comment.