Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add generated columns (virtual/stored) support for PostgreSQL #30971

Merged
merged 2 commits into from
Dec 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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