packages/sql-semantics-postgres/tests/Unit/Rules/Table/Command/AlterKindsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Table\Command;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\RelationKind;
11use SqlSemantics\Platform\PostgreSql\Rules\Table\Command\AlterKinds;
12use SqlSemantics\Platform\PostgreSql\Semantics;
13use SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\AlterTable;
14use SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Column\ColumnActionKind;
15use SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\NamedActionKind;
16use SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\Option\OwnerTo;
17use SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\TableAction;
18use SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\TableActionKind;
19use SqlSemantics\Platform\PostgreSql\Statement\Table\Problem\AlterAction;
20
21#[CoversClass(AlterKinds::class)]
22#[Medium]
23final class AlterKindsTest extends TestCase
24{
25 public function testCheckReportsEachActionTheKindDoesNotAccept(): void
26 {
27 $semantics = new Semantics();
28 $context = [$semantics->inspect('CREATE VIEW v AS SELECT 1 AS a')];
29 self::assertSame([
30 'ALTER action ADD COLUMN cannot be performed on relation "v"',
31 'ALTER action ALTER COLUMN ... SET NOT NULL cannot be performed on relation "v"',
32 ], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER TABLE v ADD COLUMN b int, ALTER a SET DEFAULT 1, ALTER a SET NOT NULL, OWNER TO r', $context)->facts->diagnostics));
33 }
34
35 public function testCheckReportsTheTargetKindFirst(): void
36 {
37 $semantics = new Semantics();
38 $context = [$semantics->inspect('CREATE TABLE t (a int)')];
39 self::assertSame(['"t" is not a view'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER VIEW t ALTER a SET NOT NULL', $context)->facts->diagnostics));
40 }
41
42 public function testParentReportsAParentThatCannotBeInherited(): void
43 {
44 $semantics = new Semantics();
45 $context = [$semantics->inspect('CREATE TABLE t (a int)'), $semantics->inspect('CREATE SEQUENCE s')];
46 self::assertSame(['ALTER action INHERIT cannot be performed on relation "s"'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER TABLE t INHERIT s', $context)->facts->diagnostics));
47 }
48
49 public function testForeignReportsKeysAddedToAForeignTable(): void
50 {
51 $semantics = new Semantics();
52 $context = [$semantics->inspect('CREATE FOREIGN TABLE f (a int) SERVER x')];
53 self::assertSame([
54 'unique constraints are not supported on foreign tables',
55 'primary key constraints are not supported on foreign tables',
56 ], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER TABLE f ADD COLUMN b int UNIQUE, ADD PRIMARY KEY (a), ADD CHECK (a > 0)', $context)->facts->diagnostics));
57 }
58
59 public function testAcceptedAnswersNullForOwnerTo(): void
60 {
61 $statement = (new Semantics())->inspect('ALTER TABLE t OWNER TO r')->statement;
62 self::assertInstanceOf(AlterTable::class, $statement);
63 self::assertInstanceOf(OwnerTo::class, $statement->commands[0]);
64 self::assertNull((new AlterKinds())->accepted($statement->commands[0]));
65 }
66
67 public function testOtherAnswersTheActionOfAColumnChange(): void
68 {
69 $semantics = new Semantics();
70 $statement = $semantics->inspect('ALTER TABLE t ALTER a SET STORAGE plain, ALTER a SET COMPRESSION pglz')->statement;
71 self::assertInstanceOf(AlterTable::class, $statement);
72 self::assertSame([
73 [AlterAction::SetStorage, [RelationKind::BaseTable, RelationKind::MaterializedView, RelationKind::ForeignTable]],
74 [AlterAction::SetCompression, [RelationKind::BaseTable, RelationKind::MaterializedView]],
75 ], [(new AlterKinds())->other($statement->commands[0]), (new AlterKinds())->other($statement->commands[1] ?? $statement->commands[0])]);
76 }
77
78 public function testRelationWideAnswersTheActionOfARelationOption(): void
79 {
80 $semantics = new Semantics();
81 $statement = $semantics->inspect('ALTER TABLE t RESET (fillfactor)')->statement;
82 self::assertInstanceOf(AlterTable::class, $statement);
83 self::assertSame([AlterAction::ResetRelationOptions, [RelationKind::BaseTable, RelationKind::View, RelationKind::MaterializedView]], (new AlterKinds())->relationWide($statement->commands[0]));
84 }
85
86 public function testColumnAnswersTheActionOfAColumnAction(): void
87 {
88 self::assertSame([AlterAction::SetDefault, [RelationKind::BaseTable, RelationKind::View, RelationKind::ForeignTable]], (new AlterKinds())->column(ColumnActionKind::DropDefault));
89 }
90
91 public function testTableAnswersTheActionOfATableAction(): void
92 {
93 self::assertSame([AlterAction::SetLogged, [RelationKind::BaseTable, RelationKind::Sequence]], (new AlterKinds())->table(TableActionKind::SetLogged));
94 self::assertSame([AlterAction::NotOf, [RelationKind::BaseTable]], (new AlterKinds())->accepted(new TableAction(TableActionKind::NotOf)));
95 }
96
97 public function testNamedAnswersTheActionOfANamedAction(): void
98 {
99 self::assertSame([AlterAction::ClusterOn, [RelationKind::BaseTable, RelationKind::MaterializedView]], (new AlterKinds())->named(NamedActionKind::ClusterOn));
100 }
101}
102