packages/sql-semantics-postgres/tests/Unit/Rules/Table/Command/AlterationsTest.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;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Table\Command\Alterations::class)]
12#[Medium]
13final class AlterationsTest extends TestCase
14{
15 public function testDeriveDerivesEachAction(): void
16 {
17 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
18 $context = [];
19 array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
20 $statement = $semantics->inspect('ALTER TABLE t ALTER zz DROP DEFAULT, ADD CHECK (yy)', $context);
21 self::assertSame([
22 0 => 'column "zz" does not exist',
23 1 => 'Column yy does not exist.',
24 ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
25 }
26
27 public function testWidenedAddsTheNewColumn(): void
28 {
29 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
30 $context = [];
31 array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
32 $statement = $semantics->inspect('ALTER TABLE t ADD n int NOT NULL CHECK (n > a)', $context);
33 self::assertSame(0, count($statement->facts->diagnostics));
34 }
35
36 public function testColumnReportsAColumnTheTableLacks(): void
37 {
38 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
39 $context = [];
40 array_push($context, ...$semantics->inspect('CREATE TABLE t (a int NOT NULL, b int, c text)')->declarations());
41 $statement = $semantics->inspect('ALTER TABLE t ALTER zz SET STORAGE plain', $context);
42 self::assertSame([
43 0 => 'column "zz" does not exist',
44 ], array_map(static fn ($problem): string => $problem->message(), $statement->facts->diagnostics));
45 }
46
47 public function testAdmitsRefusesAPartitionActionAmongOthers(): void
48 {
49 self::assertSame(false, (new \SqlSemantics\Platform\PostgreSql\Rules\Table\Command\Alterations())->admits([new \SqlSemantics\Platform\PostgreSql\Statement\Table\Partition\AttachIndex(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('i'))), new \SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\TableAction(\SqlSemantics\Platform\PostgreSql\Statement\Table\Alter\TableActionKind::SetLogged)]));
50 }
51
52 public function testDeriveReportsTheKindOfTheRelation(): void
53 {
54 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
55 $context = [$semantics->inspect('CREATE TABLE t (a int)'), $semantics->inspect('CREATE SEQUENCE s')];
56 self::assertSame(['"t" is not a materialized view'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER MATERIALIZED VIEW t SET (fillfactor = 70)', $context)->facts->diagnostics));
57 self::assertSame(['ALTER action ADD COLUMN cannot be performed on relation "s"'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER TABLE s ADD COLUMN c int', $context)->facts->diagnostics));
58 self::assertSame([], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER TABLE s SET LOGGED', $context)->facts->diagnostics));
59 }
60
61 public function testIdentityRequiresANotNullColumn(): void
62 {
63 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics();
64 $context = [$semantics->inspect('CREATE TABLE t (a int, b int, c int NOT NULL)')];
65 self::assertSame(['column "a" of relation "t" must be declared NOT NULL before identity can be added'], array_map(static fn ($problem): string => $problem->message(), $semantics->inspect('ALTER TABLE t ALTER a ADD GENERATED ALWAYS AS IDENTITY', $context)->facts->diagnostics));
66 self::assertSame([], $semantics->inspect('ALTER TABLE t ALTER a ADD GENERATED ALWAYS AS IDENTITY, ALTER a SET NOT NULL, ALTER b ADD GENERATED BY DEFAULT AS IDENTITY, ADD PRIMARY KEY (b), ALTER c ADD GENERATED ALWAYS AS IDENTITY', $context)->facts->diagnostics);
67 }
68}
69