packages/sql-semantics-postgres/tests/Unit/Rules/Table/Definition/GeneratedColumnsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Definition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\PostgreSql\Rules\Table\Definition\GeneratedColumns;
13use SqlSemantics\Platform\PostgreSql\Semantics;
14use SqlSemantics\Platform\PostgreSql\Statement\Table\Problem\DefinitionProblem;
15use SqlSemantics\Platform\PostgreSql\Statement\Table\Problem\DefinitionRule;
16
17#[CoversClass(GeneratedColumns::class)]
18#[Medium]
19final class GeneratedColumnsTest extends TestCase
20{
21    public function testReferencedFindsTheFirstGeneratedColumnInWrittenOrder(): void
22    {
23        $semantics = new Semantics('pg-17.2');
24        $create = $semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED, c int GENERATED ALWAYS AS (a) STORED, d int GENERATED ALWAYS AS (a + c + b) STORED)');
25        self::assertEquals([new DefinitionProblem(DefinitionRule::GeneratedInGeneration, new Name('c'))], $create->facts->diagnostics);
26    }
27
28    public function testExpressionAcceptsRegularColumns(): void
29    {
30        $semantics = new Semantics('pg-16.6');
31        $create = $semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED, c int DEFAULT 1, d int GENERATED ALWAYS AS (t.c + a) STORED)');
32        self::assertSame([], $create->facts->diagnostics);
33    }
34
35    public function testExpressionReportsAnAddedColumnUsingAGeneratedColumn(): void
36    {
37        $semantics = new Semantics('pg-17.2');
38        $table = $semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED)');
39        $alter = $semantics->inspect('ALTER TABLE t ADD COLUMN d int GENERATED ALWAYS AS (b + 1) STORED', $table->declarations());
40        self::assertSame(['cannot use generated column "b" in column generation expression'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $alter->facts->diagnostics));
41    }
42
43    public function testPartitionKeyReportsAGeneratedColumnInAnExpression(): void
44    {
45        $semantics = new Semantics('pg-17.2');
46        $create = $semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED) PARTITION BY RANGE (a, (b + 1))');
47        self::assertSame(['cannot use generated column in partition key'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
48    }
49
50    public function testAmongTellsAGeneratedReferencingColumn(): void
51    {
52        $semantics = new Semantics('pg-17.2');
53        $context = [...$semantics->inspect('CREATE TABLE r (a int PRIMARY KEY)')->declarations(), ...$semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED)')->declarations()];
54        $alter = $semantics->inspect('ALTER TABLE t ADD FOREIGN KEY (a) REFERENCES r ON DELETE SET NULL, ADD FOREIGN KEY (b) REFERENCES r ON DELETE SET DEFAULT', $context);
55        self::assertSame(['invalid ON DELETE action for foreign key constraint containing generated column'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $alter->facts->diagnostics));
56    }
57
58    public function testKeyActionsReportsOnUpdateFirst(): void
59    {
60        $semantics = new Semantics('pg-17.2');
61        $table = $semantics->inspect('CREATE TABLE r (a int PRIMARY KEY)');
62        $create = $semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED, FOREIGN KEY (b) REFERENCES r ON DELETE SET NULL ON UPDATE CASCADE)', $table->declarations());
63        self::assertSame(['invalid ON UPDATE action for foreign key constraint containing generated column'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
64    }
65
66    public function testColumnKeysReportsAReferencesConstraintOfAGeneratedColumn(): void
67    {
68        $semantics = new Semantics('pg-16.6');
69        $table = $semantics->inspect('CREATE TABLE r (a int PRIMARY KEY)');
70        $create = $semantics->inspect('CREATE TABLE t (a int, b int GENERATED ALWAYS AS (a) STORED REFERENCES r ON UPDATE SET DEFAULT, c int REFERENCES r ON UPDATE SET NULL)', $table->declarations());
71        self::assertSame(['invalid ON UPDATE action for foreign key constraint containing generated column'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
72    }
73
74    public function testInheritedReportsAGenerationConflictOfTwoParents(): void
75    {
76        $semantics = new Semantics('pg-17.2');
77        $context = [...$semantics->inspect('CREATE TABLE p (a int, b int GENERATED ALWAYS AS (a) STORED)')->declarations(), ...$semantics->inspect('CREATE TABLE q (b int)')->declarations()];
78        $create = $semantics->inspect('CREATE TABLE c () INHERITS (p, q)', $context);
79        self::assertSame(['inherited column "b" has a generation conflict'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
80    }
81
82    public function testInheritedReportsTheOptionsOfAPartitionColumn(): void
83    {
84        $semantics = new Semantics('pg-17.2');
85        $table = $semantics->inspect('CREATE TABLE p (a int, b int GENERATED ALWAYS AS (a) STORED) PARTITION BY LIST (a)');
86        $create = $semantics->inspect('CREATE TABLE p1 PARTITION OF p (b DEFAULT 1) FOR VALUES IN (1)', $table->declarations());
87        self::assertSame(['column "b" inherits from generated column but specifies default'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
88    }
89
90    public function testMergedReportsTheGenerationOfAColumnDefinition(): void
91    {
92        $semantics = new Semantics('pg-17.2');
93        $table = $semantics->inspect('CREATE TABLE p (a int, b int GENERATED ALWAYS AS (a) STORED)');
94        $create = $semantics->inspect('CREATE TABLE c (b int GENERATED ALWAYS AS IDENTITY, a int GENERATED ALWAYS AS (2) STORED) INHERITS (p)', $table->declarations());
95        self::assertSame([
96            'column "b" inherits from generated column but specifies identity',
97            'child column "a" specifies generation expression',
98        ], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $create->facts->diagnostics));
99    }
100}
101