Lines
96.89%
187 / 193
Methods
85.71%
30 / 35
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| wrap | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| type | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 16 | ||
| autoIncrement | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| compileNullsNotDistinctClause | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| compilePartialIndexClause | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| compileDeferrableClause | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| compileDropIndex | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| compileDropColumn | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| compileSetColumnDefault | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 2 | ||
| compileDropColumnDefault | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | ||
| compileModifyColumn | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 5 | ||
| compileAddForeignKey | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| compileDropForeignKey | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| compileAddCheck | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| compileDropCheck | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| assertValidIdentifier | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileCreate | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileTableBody | 100.00% | 22 / 22 | 100.00% | 1 / 1 | 5 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileCheckConstraint | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 2.06 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileForeignKeyConstraint | 91.66% | 11 / 12 | 0.00% | 0 / 1 | 6.02 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileAddColumns | 93.75% | 15 / 16 | 0.00% | 0 / 1 | 5.01 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] withTemporaryDefault | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDropColumns | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDrop | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileRenameTable | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileRenameColumn | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileCopyTable | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileIndexes | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 4 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileAddColumn | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileColumnDefinition | 100.00% | 16 / 16 | 100.00% | 1 / 1 | 11 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileEnumCheckClause | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 4 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] compileDefault | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 4 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] autoIncrementBeforePrimaryKey | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] requireLength | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| [BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar] suffix | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| 18 | final class PostgresSchemaGrammar extends SchemaGrammar | |
| 19 | { | |
| 20 | /** | |
| 21 | * Wrap an identifier in Postgres double quotes. | |
| 22 | * | |
| 23 | * @param string $value | |
| 24 | * @return string | |
| 25 | */ | |
| 26 | protected function wrap(string $value): string | |
| 27 | { | |
| 28 | return '"' . str_replace('"', '""', $value) . '"'; | |
| 29 | } | |
| 30 | ||
| 31 | /** | |
| 32 | * Map a logical column type to Postgres' native type. | |
| 33 | * | |
| 34 | * Timestamp types render declared precision (`timestamp(3)`) — a display | |
| 35 | * rounding hint; Postgres always stores microseconds natively. | |
| 36 | * | |
| 37 | * @param ColumnType $type | |
| 38 | * @param int|null $length | |
| 39 | * @param int|null $precision | |
| 40 | * @param int|null $scale | |
| 41 | * @return string | |
| 42 | */ | |
| 43 | public function type(ColumnType $type, ?int $length = null, ?int $precision = null, ?int $scale = null): string | |
| 44 | { | |
| 45 | return match ($type) { | |
| 46 | ColumnType::String => 'varchar' . $this->suffix($this->requireLength($length)), | |
| 47 | ColumnType::Char => 'char' . $this->suffix($this->requireLength($length)), | |
| 48 | ColumnType::Text => 'text', | |
| 49 | ColumnType::BigInt => 'bigint', | |
| 50 | ColumnType::Int => 'integer', | |
| 51 | ColumnType::Decimal => 'numeric' . $this->suffix($precision, $scale), | |
| 52 | ColumnType::Float => 'double precision', | |
| 53 | ColumnType::Boolean => 'boolean', | |
| 54 | ColumnType::Date => 'date', | |
| 55 | ColumnType::DateTime => 'timestamp' . $this->suffix($precision), | |
| 56 | ColumnType::Timestamp => 'timestamp' . $this->suffix($precision), | |
| 57 | ColumnType::Json => 'jsonb', | |
| 58 | ColumnType::Enum => 'varchar' . $this->suffix($this->requireLength($length)), | |
| 59 | ColumnType::Binary => 'bytea', | |
| 60 | ColumnType::Uuid => 'uuid', | |
| 61 | }; | |
| 62 | } | |
| 63 | ||
| 64 | /** | |
| 65 | * The Postgres auto-increment clause. | |
| 66 | * | |
| 67 | * @return string | |
| 68 | */ | |
| 69 | protected function autoIncrement(): string | |
| 70 | { | |
| 71 | return 'GENERATED BY DEFAULT AS IDENTITY'; | |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * Postgres 15+ renders the NULLS clause explicitly in both directions. | |
| 76 | * | |
| 77 | * @param bool $nullsNotDistinct | |
| 78 | * @return string | |
| 79 | */ | |
| 80 | protected function compileNullsNotDistinctClause(bool $nullsNotDistinct): string | |
| 81 | { | |
| 82 | return $nullsNotDistinct ? 'NULLS NOT DISTINCT' : 'NULLS DISTINCT'; | |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Postgres renders partial (filtered) indexes — `CREATE INDEX ... WHERE | |
| 87 | * predicate`. | |
| 88 | * | |
| 89 | * @param string $predicate | |
| 90 | * @return string | |
| 91 | */ | |
| 92 | protected function compilePartialIndexClause(string $predicate): string | |
| 93 | { | |
| 94 | return 'WHERE ' . $predicate; | |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Postgres renders `DEFERRABLE [INITIALLY DEFERRED]` on foreign keys. | |
| 99 | * | |
| 100 | * @param bool $initiallyDeferred | |
| 101 | * @return string | |
| 102 | */ | |
| 103 | protected function compileDeferrableClause(bool $initiallyDeferred): string | |
| 104 | { | |
| 105 | return $initiallyDeferred ? 'DEFERRABLE INITIALLY DEFERRED' : 'DEFERRABLE'; | |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * Postgres drops an index by name alone: `DROP INDEX name`. | |
| 110 | * | |
| 111 | * @param string $name | |
| 112 | * @param string $table | |
| 113 | * @return string | |
| 114 | */ | |
| 115 | public function compileDropIndex(string $name, string $table): string | |
| 116 | { | |
| 117 | unset($table); | |
| 118 | $this->assertValidIdentifier($name); | |
| 119 | ||
| 120 | return 'DROP INDEX ' . $this->wrap($name); | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Compile one column's `DROP COLUMN` clause. | |
| 125 | * | |
| 126 | * @param string $column | |
| 127 | * @return string | |
| 128 | */ | |
| 129 | protected function compileDropColumn(string $column): string | |
| 130 | { | |
| 131 | return $this->wrap($column); | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * Compile the statement that restores a column's declared default. | |
| 136 | * | |
| 137 | * @param string $table | |
| 138 | * @param string $column | |
| 139 | * @param mixed $default A scalar or an Expression. | |
| 140 | * @return string | |
| 141 | */ | |
| 142 | #[\Override] | |
| 143 | protected function compileSetColumnDefault(string $table, string $column, mixed $default): string | |
| 144 | { | |
| 145 | return 'ALTER TABLE ' . $this->wrap($table) . ' ALTER COLUMN ' . $this->wrap($column) | |
| 146 | . ' SET DEFAULT ' . $this->compileDefault($default); | |
| 147 | } | |
| 148 | ||
| 149 | /** | |
| 150 | * Compile the statement that drops a column's temporary default. | |
| 151 | * | |
| 152 | * @param string $table | |
| 153 | * @param string $column | |
| 154 | * @return string | |
| 155 | */ | |
| 156 | #[\Override] | |
| 157 | protected function compileDropColumnDefault(string $table, string $column): string | |
| 158 | { | |
| 159 | return 'ALTER TABLE ' . $this->wrap($table) . ' ALTER COLUMN ' . $this->wrap($column) . ' DROP DEFAULT'; | |
| 160 | } | |
| 161 | ||
| 162 | /** | |
| 163 | * Compile the `ALTER TABLE ... ALTER COLUMN` statements — Postgres' | |
| 164 | * in-place content-drift form. | |
| 165 | * | |
| 166 | * @param Blueprint $blueprint | |
| 167 | * @return list<string> | |
| 168 | */ | |
| 169 | public function compileModifyColumn(Blueprint $blueprint): array | |
| 170 | { | |
| 171 | $table = $blueprint->getTable(); | |
| 172 | $columns = $blueprint->getColumns(); | |
| 173 | if ($columns === []) { | |
| 174 | throw new \InvalidArgumentException('Cannot modify columns with no columns defined.'); | |
| 175 | } | |
| 176 | ||
| 177 | $statements = []; | |
| 178 | ||
| 179 | foreach ($columns as $column) { | |
| 180 | $name = $this->wrap($column['name']); | |
| 181 | $base = 'ALTER TABLE ' . $this->wrap($table) . ' ALTER COLUMN ' . $name; | |
| 182 | ||
| 183 | // TYPE first: the type text is the identity of the change, and | |
| 184 | // a type change must land before constraints that depend on it. | |
| 185 | // The USING clause casts the existing values explicitly — | |
| 186 | // Postgres only auto-casts implicitly-castable pairs, so a | |
| 187 | // text→numeric / text→timestamp reshape needs the explicit cast. | |
| 188 | // The full shape (length, precision, scale) renders so a | |
| 189 | // Decimal(10,2) stays numeric(10,2) — otherwise the second | |
| 190 | // diff sees a perpetual drift. | |
| 191 | $type = $this->type($column['type'], $column['length'], $column['precision'], $column['scale']); | |
| 192 | $statements[] = $base . ' TYPE ' . $type . ' USING ' . $name . '::' . $type; | |
| 193 | ||
| 194 | // Nullability second: SET NOT NULL validates existing rows, so | |
| 195 | // it must come after the type conversion. | |
| 196 | $statements[] = $column['nullable'] ? $base . ' DROP NOT NULL' : $base . ' SET NOT NULL'; | |
| 197 | ||
| 198 | // Default last: a null desired default means DROP DEFAULT (the | |
| 199 | // column carries no default); anything else renders the literal. | |
| 200 | $statements[] = $column['default'] === null | |
| 201 | ? $base . ' DROP DEFAULT' | |
| 202 | : $base . ' SET DEFAULT ' . $this->compileDefault($column['default']); | |
| 203 | } | |
| 204 | ||
| 205 | return $statements; | |
| 206 | } | |
| 207 | ||
| 208 | /** | |
| 209 | * Compile an `ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY` | |
| 210 | * statement — Postgres' in-place FK-add form. | |
| 211 | * | |
| 212 | * @param string $table | |
| 213 | * @param array{columns: list<string>, references: list<string>, onDelete: \BlueprintAU\Radiant\Database\Schema\Enums\ForeignKeyAction|null, onUpdate: \BlueprintAU\Radiant\Database\Schema\Enums\ForeignKeyAction|null, deferrable: bool, initiallyDeferred: bool} $foreignKey | |
| 214 | * @param string $name | |
| 215 | * @return string | |
| 216 | */ | |
| 217 | public function compileAddForeignKey(string $table, array $foreignKey, string $name): string | |
| 218 | { | |
| 219 | $this->assertValidIdentifier($name); | |
| 220 | ||
| 221 | return 'ALTER TABLE ' . $this->wrap($table) . ' ADD CONSTRAINT ' . $this->wrap($name) . ' ' | |
| 222 | . $this->compileForeignKeyConstraint($foreignKey); | |
| 223 | } | |
| 224 | ||
| 225 | /** | |
| 226 | * Compile an `ALTER TABLE ... DROP CONSTRAINT` statement — Postgres' | |
| 227 | * in-place constraint-drop form (covers FKs and CHECKs). | |
| 228 | * | |
| 229 | * @param string $table | |
| 230 | * @param string $name | |
| 231 | * @return string | |
| 232 | */ | |
| 233 | public function compileDropForeignKey(string $table, string $name): string | |
| 234 | { | |
| 235 | $this->assertValidIdentifier($name); | |
| 236 | ||
| 237 | return 'ALTER TABLE ' . $this->wrap($table) . ' DROP CONSTRAINT ' . $this->wrap($name); | |
| 238 | } | |
| 239 | ||
| 240 | /** | |
| 241 | * Compile an `ALTER TABLE ... ADD CONSTRAINT ... CHECK` statement — | |
| 242 | * Postgres' in-place CHECK-add form. | |
| 243 | * | |
| 244 | * @param string $table | |
| 245 | * @param string $name | |
| 246 | * @param string $expression | |
| 247 | * @return string | |
| 248 | */ | |
| 249 | public function compileAddCheck(string $table, string $name, string $expression): string | |
| 250 | { | |
| 251 | $this->assertValidIdentifier($name); | |
| 252 | ||
| 253 | return 'ALTER TABLE ' . $this->wrap($table) . ' ADD CONSTRAINT ' . $this->wrap($name) | |
| 254 | . ' CHECK (' . $expression . ')'; | |
| 255 | } | |
| 256 | ||
| 257 | /** | |
| 258 | * Compile an `ALTER TABLE ... DROP CONSTRAINT` statement for a CHECK. | |
| 259 | * | |
| 260 | * @param string $table | |
| 261 | * @param string $name | |
| 262 | * @return string | |
| 263 | */ | |
| 264 | public function compileDropCheck(string $table, string $name): string | |
| 265 | { | |
| 266 | $this->assertValidIdentifier($name); | |
| 267 | ||
| 268 | return 'ALTER TABLE ' . $this->wrap($table) . ' DROP CONSTRAINT ' . $this->wrap($name); | |
| 269 | } | |
| 270 | ||
| 271 | /** | |
| 272 | * Postgres caps identifiers at 63 bytes — fail fast at compile time, | |
| 273 | * never silently truncate. | |
| 274 | * | |
| 275 | * @param string $name | |
| 276 | * @return void | |
| 277 | * @throws \InvalidArgumentException | |
| 278 | */ | |
| 279 | public function assertValidIdentifier(string $name): void | |
| 280 | { | |
| 281 | if (strlen($name) > 63) { | |
| 282 | throw new \InvalidArgumentException(sprintf( | |
| 283 | 'Identifier [%s] exceeds Postgres\'s 63-byte limit (%d bytes); ' | |
| 284 | . 'declare a shorter #[Unique(name: ...)] / #[Index(name: ...)].', | |
| 285 | $name, | |
| 286 | strlen($name), | |
| 287 | )); | |
| 288 | } | |
| 289 | } | |
| 290 | } |
Inherited from BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar
| 49 | final public function compileCreate(Blueprint $blueprint): string | |
| 50 | { | |
| 51 | return 'CREATE TABLE ' . $this->wrap($blueprint->getTable()) . ' (' . $this->compileTableBody($blueprint) . ')'; | |
| 52 | } |
| 60 | final public function compileTableBody(Blueprint $blueprint): string | |
| 61 | { | |
| 62 | $columns = $blueprint->getColumns(); | |
| 63 | if ($columns === []) { | |
| 64 | throw new \InvalidArgumentException('Cannot create a table with no columns.'); | |
| 65 | } | |
| 66 | ||
| 67 | // A composite primary key is declared table-level, not per-column. | |
| 68 | $primaryKeys = array_values(array_filter( | |
| 69 | $columns, | |
| 70 | fn (array $column) => $column['primaryKey'] === true, | |
| 71 | )); | |
| 72 | $composite = count($primaryKeys) > 1; | |
| 73 | ||
| 74 | $definitions = array_map( | |
| 75 | fn (array $column) => $this->compileColumnDefinition($column, $composite), | |
| 76 | $columns, | |
| 77 | ); | |
| 78 | ||
| 79 | if ($composite) { | |
| 80 | $definitions[] = 'PRIMARY KEY (' . implode(', ', array_map( | |
| 81 | fn (array $column) => $this->wrap($column['name']), | |
| 82 | $primaryKeys, | |
| 83 | )) . ')'; | |
| 84 | } | |
| 85 | ||
| 86 | foreach ($blueprint->getForeignKeys() as $foreignKey) { | |
| 87 | $definitions[] = $this->compileForeignKeyConstraint($foreignKey); | |
| 88 | } | |
| 89 | ||
| 90 | foreach ($blueprint->getChecks() as $check) { | |
| 91 | $definitions[] = $this->compileCheckConstraint($check); | |
| 92 | } | |
| 93 | ||
| 94 | return implode(', ', $definitions); | |
| 95 | } |
| 103 | protected function compileCheckConstraint(array $check): string | |
| 104 | { | |
| 105 | if ($check['name'] !== null) { | |
| 106 | $this->assertValidIdentifier($check['name']); | |
| 107 | return 'CONSTRAINT ' . $this->wrap($check['name']) . ' CHECK (' . $check['expression'] . ')'; | |
| 108 | } | |
| 109 | ||
| 110 | return 'CHECK (' . $check['expression'] . ')'; | |
| 111 | } |
| 119 | protected function compileForeignKeyConstraint(array $foreignKey): string | |
| 120 | { | |
| 121 | $table = array_shift($foreignKey['references']); | |
| 122 | if ($table === null) { | |
| 123 | throw new \InvalidArgumentException('A foreign key constraint requires a referenced table.'); | |
| 124 | } | |
| 125 | ||
| 126 | // Statement assembly: the constraint body plus optional clauses — | |
| 127 | // the concatenate() join (absent clause = '' segment, dropped), | |
| 128 | // NOT a list join. Column lists inside the parens stay list-joins. | |
| 129 | return $this->concatenate([ | |
| 130 | 'FOREIGN KEY (' . implode(', ', array_map(fn (string $column) => $this->wrap($column), $foreignKey['columns'])) . ')', | |
| 131 | 'REFERENCES ' . $this->wrap($table) . ' (' . implode(', ', array_map(fn (string $column) => $this->wrap($column), $foreignKey['references'])) . ')', | |
| 132 | $foreignKey['onDelete'] === null ? '' : 'ON DELETE ' . $foreignKey['onDelete']->value, | |
| 133 | $foreignKey['onUpdate'] === null ? '' : 'ON UPDATE ' . $foreignKey['onUpdate']->value, | |
| 134 | $foreignKey['initiallyDeferred'] || $foreignKey['deferrable'] | |
| 135 | ? $this->compileDeferrableClause($foreignKey['initiallyDeferred']) | |
| 136 | : '', | |
| 137 | ]); | |
| 138 | } |
| 164 | public function compileAddColumns(Blueprint $blueprint): array | |
| 165 | { | |
| 166 | $columns = $blueprint->getColumns(); | |
| 167 | if ($columns === []) { | |
| 168 | throw new \InvalidArgumentException('Cannot add columns with no columns defined.'); | |
| 169 | } | |
| 170 | ||
| 171 | $backfills = $blueprint->getBackfills(); | |
| 172 | $table = $blueprint->getTable(); | |
| 173 | ||
| 174 | // A NOT NULL column with a backfill is added with the backfill as | |
| 175 | // a TEMPORARY default (existing rows get it), then the default is | |
| 176 | // restored to the declared one (new rows get that) or dropped. | |
| 177 | $statements = ['ALTER TABLE ' . $this->wrap($table) . ' ADD COLUMN ' . implode(', ADD COLUMN ', array_map( | |
| 178 | fn (array $column) => $this->compileAddColumn($this->withTemporaryDefault($column, $backfills)), | |
| 179 | $columns, | |
| 180 | ))]; | |
| 181 | ||
| 182 | foreach ($columns as $column) { | |
| 183 | if (!array_key_exists($column['name'], $backfills)) { | |
| 184 | continue; | |
| 185 | } | |
| 186 | ||
| 187 | $statements[] = $column['default'] !== null | |
| 188 | ? $this->compileSetColumnDefault($table, $column['name'], $column['default']) | |
| 189 | : $this->compileDropColumnDefault($table, $column['name']); | |
| 190 | } | |
| 191 | ||
| 192 | return $statements; | |
| 193 | } |
| 203 | protected function withTemporaryDefault(array $column, array $backfills): array | |
| 204 | { | |
| 205 | if (!array_key_exists($column['name'], $backfills)) { | |
| 206 | return $column; | |
| 207 | } | |
| 208 | ||
| 209 | return [...$column, 'default' => $backfills[$column['name']]]; | |
| 210 | } |
| 252 | public function compileDropColumns(Blueprint $blueprint): array | |
| 253 | { | |
| 254 | $columns = $blueprint->getDropColumns(); | |
| 255 | if ($columns === []) { | |
| 256 | throw new \InvalidArgumentException('Cannot drop columns with no columns defined.'); | |
| 257 | } | |
| 258 | ||
| 259 | return ['ALTER TABLE ' . $this->wrap($blueprint->getTable()) . ' DROP COLUMN ' . implode(', DROP COLUMN ', array_map( | |
| 260 | fn (string $column) => $this->compileDropColumn($column), | |
| 261 | $columns, | |
| 262 | ))]; | |
| 263 | } |
| 271 | final public function compileDrop(string $table): string | |
| 272 | { | |
| 273 | return 'DROP TABLE ' . $this->wrap($table); | |
| 274 | } |
| 283 | final public function compileRenameTable(string $from, string $to): string | |
| 284 | { | |
| 285 | return 'ALTER TABLE ' . $this->wrap($from) . ' RENAME TO ' . $this->wrap($to); | |
| 286 | } |
| 296 | final public function compileRenameColumn(string $table, string $from, string $to): string | |
| 297 | { | |
| 298 | return 'ALTER TABLE ' . $this->wrap($table) . ' RENAME COLUMN ' | |
| 299 | . $this->wrap($from) . ' TO ' . $this->wrap($to); | |
| 300 | } |
| 311 | final public function compileCopyTable(string $from, string $to, array $columns): string | |
| 312 | { | |
| 313 | if ($columns === []) { | |
| 314 | throw new \InvalidArgumentException('A table copy requires at least one column.'); | |
| 315 | } | |
| 316 | ||
| 317 | $wrapped = implode(', ', array_map(fn (string $column) => $this->wrap($column), $columns)); | |
| 318 | ||
| 319 | return 'INSERT INTO ' . $this->wrap($to) . ' (' . $wrapped . ') SELECT ' . $wrapped . ' FROM ' . $this->wrap($from); | |
| 320 | } |
| 418 | final public function compileIndexes(Blueprint $blueprint): array | |
| 419 | { | |
| 420 | $table = $blueprint->getTable(); | |
| 421 | ||
| 422 | return array_map( | |
| 423 | function (array $index) use ($table): string { | |
| 424 | // Names on the blueprint are FINAL (built at declaration | |
| 425 | // time — user-set names verbatim, derived names with the | |
| 426 | // table prefix and kind suffix). The grammar renders them | |
| 427 | // as-is; its only job is quoting + dialect validation. | |
| 428 | $this->assertValidIdentifier($index['name']); | |
| 429 | ||
| 430 | $sql = ($index['unique'] ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') | |
| 431 | . $this->wrap($index['name']) | |
| 432 | . ' ON ' . $this->wrap($table) | |
| 433 | . ' (' . implode(', ', array_map(fn (string $column) => $this->wrap($column), $index['columns'])) . ')'; | |
| 434 | ||
| 435 | // The option clauses are optional segments of the one | |
| 436 | // statement — statement assembly, not a list join. The | |
| 437 | // NULLS clause rides EVERY unique index (the base carries | |
| 438 | // the default semantics implicitly as ''; Postgres pins | |
| 439 | // them explicitly); a plain index renders neither. | |
| 440 | return $this->concatenate([ | |
| 441 | $sql, | |
| 442 | $index['unique'] | |
| 443 | ? $this->compileNullsNotDistinctClause($index['nullsNotDistinct']) | |
| 444 | : '', | |
| 445 | $index['where'] === null ? '' : $this->compilePartialIndexClause($index['where']), | |
| 446 | ]); | |
| 447 | }, | |
| 448 | $blueprint->getIndexes(), | |
| 449 | ); | |
| 450 | } |
| 500 | protected function compileAddColumn(array $column): string | |
| 501 | { | |
| 502 | return $this->compileColumnDefinition($column); | |
| 503 | } |
| 524 | protected function compileColumnDefinition(array $column, bool $composite = false): string | |
| 525 | { | |
| 526 | $name = $this->wrap($column['name']); | |
| 527 | $type = $this->type($column['type'], $column['length'], $column['precision'], $column['scale']); | |
| 528 | ||
| 529 | // A composite PK has no generated id in the ORM's contract (the | |
| 530 | // caller assigns every key part — insertGetId is a single-column | |
| 531 | // concept), so the auto-increment clause is suppressed on its | |
| 532 | // member columns. On SQLite it would be outright invalid: | |
| 533 | // AUTOINCREMENT is only legal on a single-column INTEGER PRIMARY | |
| 534 | // KEY. Auto-increment placement is otherwise dialect-dependent: | |
| 535 | // MySQL/Postgres render it before PRIMARY KEY (`AUTO_INCREMENT | |
| 536 | // PRIMARY KEY`, `GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY`), | |
| 537 | // while SQLite requires it after (`INTEGER PRIMARY KEY | |
| 538 | // AUTOINCREMENT`). | |
| 539 | $autoIncrement = $column['autoIncrement'] === true && !$composite; | |
| 540 | $beforeKey = $autoIncrement && $this->autoIncrementBeforePrimaryKey(); | |
| 541 | $afterKey = $autoIncrement && !$this->autoIncrementBeforePrimaryKey(); | |
| 542 | ||
| 543 | // Statement assembly: the optional clauses are concatenate() | |
| 544 | // segments — NOT NULL / DEFAULT / UNIQUE / the auto-increment and | |
| 545 | // PRIMARY KEY clauses each may be absent. No list items here. | |
| 546 | return $this->concatenate([ | |
| 547 | $name, | |
| 548 | $type, | |
| 549 | $column['nullable'] !== true ? 'NOT NULL' : '', | |
| 550 | $column['default'] !== null ? 'DEFAULT ' . $this->compileDefault($column['default']) : '', | |
| 551 | $column['unique'] === true ? 'UNIQUE' : '', | |
| 552 | $beforeKey ? $this->autoIncrement() : '', | |
| 553 | // A single-column PK is declared inline; a composite PK is | |
| 554 | // declared table-level (see compileCreate), so a column that is | |
| 555 | // part of a composite PK must not also get an inline PRIMARY KEY. | |
| 556 | $column['primaryKey'] === true && !$composite ? 'PRIMARY KEY' : '', | |
| 557 | $afterKey ? $this->autoIncrement() : '', | |
| 558 | $this->compileEnumCheckClause($column), | |
| 559 | ]); | |
| 560 | } |
| 569 | protected function compileEnumCheckClause(array $column): string | |
| 570 | { | |
| 571 | $values = $column['values'] ?? null; | |
| 572 | ||
| 573 | if ($column['type'] !== ColumnType::Enum || $values === null || $values === []) { | |
| 574 | return ''; | |
| 575 | } | |
| 576 | ||
| 577 | $literals = implode(', ', array_map( | |
| 578 | fn (string $value) => $this->quoteLiteral($value), | |
| 579 | $values, | |
| 580 | )); | |
| 581 | ||
| 582 | return 'CHECK (' . $this->wrap($column['name']) . ' IN (' . $literals . '))'; | |
| 583 | } |
| 592 | protected function compileDefault(mixed $default): string | |
| 593 | { | |
| 594 | if ($default instanceof \BlueprintAU\Radiant\Database\Query\Expression) { | |
| 595 | return $default->value; | |
| 596 | } | |
| 597 | if (is_scalar($default) || $default === null) { | |
| 598 | return $this->quoteLiteral($default); | |
| 599 | } | |
| 600 | throw new \InvalidArgumentException( | |
| 601 | 'A column default must be a scalar or an Expression; got ' . get_debug_type($default) . '.' | |
| 602 | ); | |
| 603 | } |
| 617 | protected function autoIncrementBeforePrimaryKey(): bool | |
| 618 | { | |
| 619 | return true; | |
| 620 | } |
| 629 | protected function requireLength(?int $length): int | |
| 630 | { | |
| 631 | if ($length === null) { | |
| 632 | throw new \InvalidArgumentException('A string column requires a length.'); | |
| 633 | } | |
| 634 | return $length; | |
| 635 | } |
| 644 | protected function suffix(\Stringable|int|null ...$parts): string | |
| 645 | { | |
| 646 | // Drop the nulls, then stringify what's left. The filter is | |
| 647 | // explicit (`!== null`), not the default falsy filter: a numeric | |
| 648 | // part of 0 stringifies to '0', which is falsy and would be | |
| 649 | // silently dropped. | |
| 650 | $rendered = array_map( | |
| 651 | fn (\Stringable|int $part): string => (string) $part, | |
| 652 | array_filter($parts, fn (\Stringable|int|null $part): bool => $part !== null), | |
| 653 | ); | |
| 654 | ||
| 655 | if ($rendered === []) { | |
| 656 | return ''; | |
| 657 | } | |
| 658 | ||
| 659 | return '(' . implode(',', $rendered) . ')'; | |
| 660 | } |