packages/sql-semantics/tests/Unit/Core/Ast/SchemaChangesTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Core\Ast;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\Attributes\TestWith;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Core\Binder;
12use SqlSemantics\Core\Dialect;
13use SqlSemantics\Core\SchemaBuilder;
14use SqlSemantics\Core\SemanticException;
15use SqlSemantics\Facade\Schema;
16use SqlSemantics\Platform\MySql\Dialect as MySqlDialect;
17use SqlSemantics\Platform\PostgreSql\Dialect as PostgreSqlDialect;
18use SqlSemantics\Platform\Sqlite\Dialect as SqliteDialect;
19use SqlSemantics\Statement\Writer;
20
21#[CoversClass(Binder::class)]
22#[CoversClass(SchemaBuilder::class)]
23#[CoversClass(\SqlSemantics\Core\Ast\DialectParser::class)]
24#[CoversClass(\SqlSemantics\Core\Binding\ExpressionBinder::class)]
25#[CoversClass(\SqlSemantics\Core\Binding\ExpressionRules::class)]
26#[CoversClass(\SqlSemantics\Core\Binding\FromBinder::class)]
27#[CoversClass(\SqlSemantics\Core\Binding\LiteralBinder::class)]
28#[CoversClass(\SqlSemantics\Core\Binding\NullFacts::class)]
29#[CoversClass(\SqlSemantics\Core\Binding\ProjectionBinder::class)]
30#[CoversClass(\SqlSemantics\Core\Binding\SelectBinder::class)]
31#[CoversClass(\SqlSemantics\Core\Binding\SyntaxGuard::class)]
32#[CoversClass(\SqlSemantics\Core\Binding\SelectModifiersBinder::class)]
33#[CoversClass(\SqlSemantics\Core\Binding\TypeResolution::class)]
34#[CoversClass(\SqlSemantics\Core\Ast\ColumnReader::class)]
35#[CoversClass(\SqlSemantics\Core\Ast\ConstraintReader::class)]
36#[CoversClass(\SqlSemantics\Core\Ast\Identifiers::class)]
37#[CoversClass(\SqlSemantics\Core\Ast\SchemaReader::class)]
38#[CoversClass(\SqlSemantics\Core\Ast\StatementList::class)]
39#[CoversClass(\SqlSemantics\Core\Ast\TokenGroups::class)]
40#[CoversClass(\SqlSemantics\Core\Ast\Tree::class)]
41#[CoversClass(\SqlSemantics\Core\Ast\TypeReader::class)]
42#[CoversClass(\SqlSemantics\Core\Binding\BoundRelation::class)]
43#[CoversClass(\SqlSemantics\Core\Binding\IdentitySequence::class)]
44#[CoversClass(\SqlSemantics\Core\Binding\Scope::class)]
45#[CoversClass(\SqlSemantics\Core\Binding\TableResolver::class)]
46#[CoversClass(\SqlSemantics\Core\Model\ColumnBinding::class)]
47#[CoversClass(\SqlSemantics\Core\Model\Expression::class)]
48#[CoversClass(\SqlSemantics\Core\Model\Join::class)]
49#[CoversClass(\SqlSemantics\Core\Model\Ordering::class)]
50#[CoversClass(\SqlSemantics\Core\Model\OutputColumn::class)]
51#[CoversClass(\SqlSemantics\Core\Model\BoundSelect::class)]
52#[CoversClass(\SqlSemantics\Core\Model\TableUse::class)]
53#[CoversClass(\SqlSemantics\Core\Schema::class)]
54#[CoversClass(\SqlSemantics\Core\Schema\ColumnDefinition::class)]
55#[CoversClass(\SqlSemantics\Core\Schema\TableConstraint::class)]
56#[CoversClass(\SqlSemantics\Core\Schema\TableDefinition::class)]
57#[CoversClass(SemanticException::class)]
58#[CoversClass(\SqlSemantics\Core\Type\TypeDescriptor::class)]
59#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Core\Policy\SyntaxRules::class)]
60#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\QueryRules::class)]
61#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\Platform::class)]
62#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\TypeRules::class)]
63#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\NameRules::class)]
64#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\PostgreSql\SchemaRules::class)]
65#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\QueryRules::class)]
66#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\Platform::class)]
67#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\TypeRules::class)]
68#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\NameRules::class)]
69#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\Sqlite\SchemaRules::class)]
70#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\QueryRules::class)]
71#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\Platform::class)]
72#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\TypeRules::class)]
73#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\NameRules::class)]
74#[\PHPUnit\Framework\Attributes\UsesClass(\SqlSemantics\Platform\MySql\SchemaRules::class)]
75#[Medium]
76#[CoversClass(\SqlSemantics\Core\Analysis\SchemaAnalyzer::class)]
77#[CoversClass(\SqlSemantics\Core\Analysis\ValueReader::class)]
78#[CoversClass(\SqlSemantics\Core\Ast\ColumnProperties::class)]
79#[CoversClass(\SqlSemantics\Core\Ast\SchemaChanges::class)]
80#[CoversClass(\SqlSemantics\Core\Schema\ColumnGeneration::class)]
81#[CoversClass(\SqlSemantics\Core\Schema\Invariant::class)]
82#[CoversClass(Schema::class)]
83#[CoversClass(\SqlSemantics\Statement\ImmutableGraph::class)]
84#[CoversClass(Writer::class)]
85#[CoversClass(\SqlSemantics\Statement\Assertion::class)]
86final class SchemaChangesTest extends TestCase
87{
88 #[TestWith([MySqlDialect::MySql])]
89 #[TestWith([PostgreSqlDialect::PostgreSql])]
90 #[TestWith([SqliteDialect::Sqlite])]
91 public function testDropAppliesInDeclarationOrder(Dialect $dialect): void
92 {
93 $schema = (new Schema($dialect))->analyze('CREATE TABLE t (old_column INT); DROP TABLE t; CREATE TABLE t (new_column TEXT);');
94 self::assertSame(['new_column'], array_column($schema->tables[0]->columns, 'name'));
95 }
96
97 public function testDropRejectsMissingUnconditionalTargets(): void
98 {
99 $this->expectException(SemanticException::class);
100 $this->expectExceptionMessage('Cannot drop');
101 (new Schema(PostgreSqlDialect::PostgreSql))->analyze('DROP TABLE absent');
102 }
103
104 public function testDropDoesNotRemoveAQualifiedNamesake(): void
105 {
106 $state = (new Schema(PostgreSqlDialect::PostgreSql))->analyze('CREATE TABLE public.t (id INT); CREATE TABLE app.t (id INT); DROP TABLE public.t;');
107 self::assertSame('app', $state->tables[0]->schema);
108 }
109
110 public function testDropHandlesMultipleTargetsAndConditionalCreation(): void
111 {
112 $state = (new Schema(PostgreSqlDialect::PostgreSql))->analyze('CREATE TABLE a (id INT); CREATE TABLE b (id INT); CREATE TABLE IF NOT EXISTS a (ignored TEXT); DROP TABLE a, b;');
113 self::assertSame([], $state->tables);
114 }
115
116 public function testDropRejectsUnresolvedCatalogQualification(): void
117 {
118 $this->expectException(SemanticException::class);
119 $this->expectExceptionMessage('three-part table name');
120 (new Schema(PostgreSqlDialect::PostgreSql))->analyze('CREATE TABLE public.t (id INT); DROP TABLE db.public.t;');
121 }
122
123}
124