Skip to content

Commit cd30ed8

Browse files
bastien-phitpetry
andauthored
Allow defining columns of the view (#66)
Co-authored-by: tpetry <[email protected]>
1 parent b89af5d commit cd30ed8

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ Schema::createView('users_with_2fa', 'SELECT * FROM users WHERE two_factor_secre
285285
Schema::createViewOrReplace('users_without_2fa', DB::table('users')->whereNull('two_factor_secret'));
286286
```
287287

288+
You can specify alternative names for the view's columns by passing an array as the last parameter:
289+
```php
290+
use Illuminate\Support\Facades\DB;
291+
use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
292+
293+
Schema::createView('users_with_2fa', DB::table('users')->select('id')->whereNull('two_factor_secret'), ['user_id']);
294+
```
295+
288296
If you need to create recursive views the `createRecursiveView` and `createRecursiveViewOrReplace` methods can be used like in the former examples but you need to provide the available columns as last parameter:
289297

290298
```php
@@ -330,6 +338,7 @@ use Tpetry\PostgresqlEnhanced\Support\Facades\Schema;
330338

331339
Schema::createMaterializedView('users_with_2fa', 'SELECT * FROM users WHERE two_factor_secret IS NOT NULL');
332340
Schema::createMaterializedView('users_with_2fa', DB::table('users')->whereNull('two_factor_secret'));
341+
Schema::createMaterializedView('users_with_2fa', DB::table('users')->select('id')->whereNull('two_factor_secret'), columns: ['user_id']);
333342

334343
Schema::createMaterializedView('very_slow_query_materialized', 'SELECT ...', withData: false);
335344

src/Schema/BuilderView.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ trait BuilderView
1212
/**
1313
* Create a materialized view on the schema.
1414
*/
15-
public function createMaterializedView(string $name, QueryBuilder|string $query, bool $withData = true): void
15+
public function createMaterializedView(string $name, QueryBuilder|string $query, bool $withData = true, array $columns = []): void
1616
{
1717
$name = $this->getConnection()->getSchemaGrammar()->wrapTable($name);
18+
if (filled($columns)) {
19+
$columns = $this->getConnection()->getSchemaGrammar()->columnize($columns);
20+
$name = "{$name} ({$columns})";
21+
}
1822
$query = Query::toSql($query);
1923
$withData = $withData ? 'with data' : 'with no data';
2024
$this->getConnection()->statement("create materialized view {$name} as {$query} {$withData}");
@@ -45,19 +49,27 @@ public function createRecursiveViewOrReplace(string $name, QueryBuilder|string $
4549
/**
4650
* Create a view on the schema.
4751
*/
48-
public function createView(string $name, QueryBuilder|string $query): void
52+
public function createView(string $name, QueryBuilder|string $query, array $columns = []): void
4953
{
5054
$name = $this->getConnection()->getSchemaGrammar()->wrapTable($name);
55+
if (filled($columns)) {
56+
$columns = $this->getConnection()->getSchemaGrammar()->columnize($columns);
57+
$name = "{$name} ({$columns})";
58+
}
5159
$query = Query::toSql($query);
5260
$this->getConnection()->statement("create view {$name} as {$query}");
5361
}
5462

5563
/**
5664
* Create or replace a view on the schema.
5765
*/
58-
public function createViewOrReplace(string $name, QueryBuilder|string $query): void
66+
public function createViewOrReplace(string $name, QueryBuilder|string $query, array $columns = []): void
5967
{
6068
$name = $this->getConnection()->getSchemaGrammar()->wrapTable($name);
69+
if (filled($columns)) {
70+
$columns = $this->getConnection()->getSchemaGrammar()->columnize($columns);
71+
$name = "{$name} ({$columns})";
72+
}
6173
$query = Query::toSql($query);
6274
$this->getConnection()->statement("create or replace view {$name} as {$query}");
6375
}

src/Support/Facades/Schema.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
* @method static void createFunctionOrReplace(string $name, array $parameters, array|string $return, string $language, string $body, array $options = [])
1919
* @method static void createRecursiveView(string $name, Builder|string $query, array $columns)
2020
* @method static void createRecursiveViewOrReplace(string $name, Builder|string $query, array $columns)
21-
* @method static void createMaterializedView(string $name, Builder|string $query, bool $withData = true)
22-
* @method static void createView(string $name, Builder|string $query)
23-
* @method static void createViewOrReplace(string $name, Builder|string $query)
21+
* @method static void createMaterializedView(string $name, Builder|string $query, bool $withData = true, array $columns = [])
22+
* @method static void createView(string $name, Builder|string $query, array $columns = [])
23+
* @method static void createViewOrReplace(string $name, Builder|string $query, array $columns = [])
2424
* @method static void dropExtension(string ...$name)
2525
* @method static void dropExtensionIfExists(string ...$name)
2626
* @method static void dropFunction(string $name, ?array $arguments = null)

tests/Migration/ViewTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
class ViewTest extends TestCase
1212
{
13+
public function testCreateMaterializedViewWithColumns(): void
14+
{
15+
$queries = $this->withQueryLog(function (): void {
16+
Schema::createMaterializedView('test_553410', DB::query()->selectRaw('random()'), columns: ['column_221818']);
17+
});
18+
$this->assertEquals(['create materialized view "test_553410" ("column_221818") as select random() with data'], array_column($queries, 'query'));
19+
}
20+
1321
public function testCreateMaterializedViewWithData(): void
1422
{
1523
$queries = $this->withQueryLog(function (): void {
@@ -58,6 +66,22 @@ public function testCreateViewOrReplace(): void
5866
$this->assertEquals(['create or replace view "test_623631" as select random() as column_449988'], array_column($queries, 'query'));
5967
}
6068

69+
public function testCreateViewOrReplaceWithColumns(): void
70+
{
71+
$queries = $this->withQueryLog(function (): void {
72+
Schema::createViewOrReplace('test_623632', DB::query()->selectRaw('random()'), ['column_449989']);
73+
});
74+
$this->assertEquals(['create or replace view "test_623632" ("column_449989") as select random()'], array_column($queries, 'query'));
75+
}
76+
77+
public function testCreateViewWithColumns(): void
78+
{
79+
$queries = $this->withQueryLog(function (): void {
80+
Schema::createView('test_787483', DB::query()->selectRaw('random()'), ['column_275665']);
81+
});
82+
$this->assertEquals(['create view "test_787483" ("column_275665") as select random()'], array_column($queries, 'query'));
83+
}
84+
6185
public function testDropView(): void
6286
{
6387
DB::statement('CREATE VIEW test_125382 AS SELECT random() as column_298864');

0 commit comments

Comments
 (0)