packages/sql-semantics-postgres/tests/Unit/Lowering/Table/AlterCommandRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Table;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule::class)]
12#[Medium]
13final class AlterCommandRuleTest extends TestCase
14{
15    public function testCommandLowersANamedAction(): void
16    {
17        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
18        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t ENABLE ALWAYS TRIGGER g');
19        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->command($tree->find('alter_table_cmd')[0]);
20        $n1 = $value;
21        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\NamedAction::class, $n1);
22        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\NamedActionKind::EnableAlwaysTrigger, $n1->kind);
23    }
24
25    public function testColumnLowersAStatisticsTargetByNumber(): void
26    {
27        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
28        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER INDEX i ALTER 2 SET STATISTICS 10');
29        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->column($lowering->productions->form($tree->find('alter_table_cmd')[0]));
30        $n1 = $value;
31        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Column\ColumnStatistics::class, $n1);
32        $n2 = $n1->column;
33        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant::class, $n2);
34        self::assertSame('2', $n2->digits);
35    }
36
37    public function testRelationLowersADroppedColumn(): void
38    {
39        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
40        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t DROP IF EXISTS a CASCADE');
41        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->relation($lowering->productions->form($tree->find('alter_table_cmd')[0]));
42        $n1 = $value;
43        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Column\DropColumn::class, $n1);
44        $n2 = $value;
45        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Column\DropColumn::class, $n2);
46        self::assertSame([
47          0 => true,
48          1 =>
49          \SqlSemantics\Platform\PostgreSql\Statement\Option\DropBehavior::Cascade,
50        ], [$n1->ifExists, $n2->behavior]);
51    }
52
53    public function testSettingLowersReplicaIdentity(): void
54    {
55        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
56        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t REPLICA IDENTITY FULL');
57        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->setting($lowering->productions->form($tree->find('alter_table_cmd')[0]));
58        $n1 = $value;
59        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Option\ReplicaIdentity::class, $n1);
60        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Option\ReplicaIdentityKind::Full, $n1->kind);
61    }
62
63    public function testDefaultedIsADropDefaultAction(): void
64    {
65        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
66        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t ALTER a DROP DEFAULT');
67        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->defaulted(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), $tree->find('alter_column_default')[0]);
68        $n1 = $value;
69        self::assertInstanceOf(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Column\ColumnAction::class, $n1);
70        self::assertSame(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Column\ColumnActionKind::DropDefault, $n1->kind);
71    }
72
73    public function testDefaultValueLowersTheExpression(): void
74    {
75        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
76        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t ALTER a SET DEFAULT 1');
77        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->defaultValue($tree->find('alter_column_default')[0]);
78        self::assertSame('SqlSemantics\\Platform\\PostgreSql\\Statement\\Literal\\Constant', get_debug_type($value));
79    }
80
81    public function testUsingIsNullWhenNotWritten(): void
82    {
83        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
84        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t ALTER a TYPE int');
85        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->using($tree->find('alter_using')[0]);
86        self::assertSame(null, $value);
87    }
88
89    public function testStatisticsIsNullForDefault(): void
90    {
91        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
92        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t ALTER a SET STATISTICS DEFAULT');
93        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->statistics($tree->find('set_statistics_value')[0]);
94        self::assertSame(null, $value);
95    }
96
97    public function testMethodIsNullForDefault(): void
98    {
99        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
100        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t SET ACCESS METHOD DEFAULT');
101        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->method($tree->find('set_access_method_name')[0]);
102        self::assertSame(null, $value);
103    }
104
105    public function testReplicaLowersUsingIndex(): void
106    {
107        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
108        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t REPLICA IDENTITY USING INDEX k');
109        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->replica($tree->find('replica_identity')[0]);
110        self::assertSame('k', $value->index?->value);
111    }
112
113    public function testIdentityKeepsTheOrder(): void
114    {
115        $lowering = new \SqlSemantics\Platform\PostgreSql\Lowering\Lowering((new \SqlSemantics\Platform\PostgreSql\Platform())->productions(new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172)), new \SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves(), \SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
116        $tree = (new \SqlParser\PostgreSql\PostgreSqlParser('pg-17.2'))->parse('ALTER TABLE t ALTER a SET CYCLE RESTART SET GENERATED ALWAYS');
117        $value = (new \SqlSemantics\Platform\PostgreSql\Lowering\Table\AlterCommandRule($lowering))->identity($tree->find('alter_identity_column_option_list')[0]);
118        self::assertSame([
119          0 => 'SqlSemantics\\Platform\\PostgreSql\\Statement\\Table\\Alter\\Column\\IdentitySetting',
120          1 => 'SqlSemantics\\Platform\\PostgreSql\\Statement\\Table\\Alter\\Column\\IdentityRestart',
121          2 => 'SqlSemantics\\Platform\\PostgreSql\\Statement\\Table\\Alter\\Column\\IdentityGeneration',
122        ], array_map(static fn ($change): string => $change::class, $value));
123    }
124}
125