| 12 | | final class PostgresSchemaInspector extends SchemaInspector |
| 13 | | { |
| 14 | | |
| 15 | | |
| 16 | | |
| 17 | | |
| 18 | | |
| 19 | | protected function getDefaultSchemaGrammar(): \BlueprintAU\Radiant\Database\Schema\Grammars\SchemaGrammar |
| 20 | | { |
| 21 | | return new \BlueprintAU\Radiant\Database\Schema\Grammars\PostgresSchemaGrammar(); |
| 22 | | } |
| 23 | | |
| 24 | | |
| 25 | | |
| 26 | | |
| 27 | | |
| 28 | | |
| 29 | | |
| 30 | | |
| 31 | | |
| 32 | | |
| 33 | | |
| 34 | | |
| 35 | | public function columnTypeMatches(string $liveType, \BlueprintAU\Radiant\Database\Schema\Enums\ColumnType $declaredType, int|null $declaredLength, int|null $declaredPrecision = null, int|null $declaredScale = null): bool |
| 36 | | { |
| 37 | | |
| 38 | | |
| 39 | | |
| 40 | | $base = strtolower($liveType); |
| 41 | | $suffix = ''; |
| 42 | | if (preg_match('/^(.+?)\((\d+(?:,\d+)?)\)$/', $base, $matches) === 1) { |
| 43 | | $base = $matches[1]; |
| 44 | | $suffix = '(' . $matches[2] . ')'; |
| 45 | | } |
| 46 | | |
| 47 | | $normalized = match ($base) { |
| 48 | | 'int4' => 'integer', |
| 49 | | 'int8' => 'bigint', |
| 50 | | 'float8' => 'double precision', |
| 51 | | 'bool' => 'boolean', |
| 52 | | 'timestamp' => 'timestamp', |
| 53 | | 'timestamptz' => 'timestamp', |
| 54 | | 'json' => 'jsonb', |
| 55 | | 'bytea' => 'bytea', |
| 56 | | 'uuid' => 'uuid', |
| 57 | | 'bpchar' => 'char', |
| 58 | | default => $base, |
| 59 | | }; |
| 60 | | |
| 61 | | return $normalized . $suffix === strtolower($this->schemaGrammar->type($declaredType, $declaredLength, $declaredPrecision, $declaredScale)); |
| 62 | | } |
| 63 | | |
| 64 | | |
| 65 | | |
| 66 | | |
| 67 | | |
| 68 | | |
| 69 | | |
| 70 | | |
| 71 | | |
| 72 | | |
| 73 | | #[\Override] |
| 74 | | public function castSafety(string $liveType, \BlueprintAU\Radiant\Database\Schema\Enums\ColumnType $desiredType): \BlueprintAU\Radiant\Database\Schema\Enums\CastSafety |
| 75 | | { |
| 76 | | $from = $this->liveTypeFamily($liveType); |
| 77 | | $to = $this->desiredTypeFamily($desiredType); |
| 78 | | |
| 79 | | |
| 80 | | if (in_array($from, ['json', 'binary', 'temporal', 'bool'], true) && $to === 'number') { |
| 81 | | return \BlueprintAU\Radiant\Database\Schema\Enums\CastSafety::Uncastable; |
| 82 | | } |
| 83 | | |
| 84 | | |
| 85 | | if ($from === 'string' && in_array($to, ['number', 'temporal'], true)) { |
| 86 | | return \BlueprintAU\Radiant\Database\Schema\Enums\CastSafety::Risky; |
| 87 | | } |
| 88 | | |
| 89 | | return parent::castSafety($liveType, $desiredType); |
| 90 | | } |
| 91 | | |
| 92 | | |
| 93 | | |
| 94 | | |
| 95 | | |
| 96 | | |
| 97 | | |
| 98 | | |
| 99 | | |
| 100 | | |
| 101 | | |
| 102 | | |
| 103 | | private function composeType(array $row): string |
| 104 | | { |
| 105 | | $type = strtolower((string) $row['udt_name']); |
| 106 | | |
| 107 | | if (in_array($type, ['varchar', 'bpchar'], true) && $row['character_maximum_length'] !== null) { |
| 108 | | return $type . '(' . (int) $row['character_maximum_length'] . ')'; |
| 109 | | } |
| 110 | | |
| 111 | | if ($type === 'numeric' && $row['numeric_precision'] !== null) { |
| 112 | | return $type . '(' . (int) $row['numeric_precision'] . ',' . (int) $row['numeric_scale'] . ')'; |
| 113 | | } |
| 114 | | |
| 115 | | return $type; |
| 116 | | } |
| 117 | | |
| 118 | | |
| 119 | | |
| 120 | | |
| 121 | | |
| 122 | | |
| 123 | | |
| 124 | | public function referencingTables(string $table): array |
| 125 | | { |
| 126 | | $statement = $this->pdo->prepare( |
| 127 | | "SELECT DISTINCT relname FROM pg_catalog.pg_constraint c " |
| 128 | | . "JOIN pg_catalog.pg_class r ON r.oid = c.conrelid " |
| 129 | | . "JOIN pg_catalog.pg_namespace n ON n.oid = r.relnamespace " |
| 130 | | . "WHERE c.contype = 'f' AND c.confrelid = (SELECT oid FROM pg_catalog.pg_class " |
| 131 | | . "WHERE relname = ? AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace " |
| 132 | | . "WHERE nspname = current_schema())) AND n.nspname = current_schema()", |
| 133 | | ); |
| 134 | | $statement->execute([$table]); |
| 135 | | |
| 136 | | $tables = []; |
| 137 | | foreach ($statement->fetchAll(\PDO::FETCH_COLUMN) as $name) { |
| 138 | | $tables[] = (string) $name; |
| 139 | | } |
| 140 | | |
| 141 | | return $tables; |
| 142 | | } |
| 143 | | |
| 144 | | |
| 145 | | |
| 146 | | |
| 147 | | |
| 148 | | public function tables(): array |
| 149 | | { |
| 150 | | $statement = $this->pdo->prepare( |
| 151 | | "SELECT tablename FROM pg_catalog.pg_tables " |
| 152 | | . "WHERE schemaname NOT IN ('pg_catalog', 'information_schema') " |
| 153 | | . 'ORDER BY tablename', |
| 154 | | ); |
| 155 | | $statement->execute(); |
| 156 | | |
| 157 | | |
| 158 | | |
| 159 | | |
| 160 | | $tables = []; |
| 161 | | foreach ($statement->fetchAll(\PDO::FETCH_COLUMN) as $name) { |
| 162 | | $tables[] = (string) $name; |
| 163 | | } |
| 164 | | |
| 165 | | return $tables; |
| 166 | | } |
| 167 | | |
| 168 | | |
| 169 | | |
| 170 | | |
| 171 | | |
| 172 | | |
| 173 | | |
| 174 | | |
| 175 | | public function table(string $name): LiveTable |
| 176 | | { |
| 177 | | if (!$this->hasTable($name)) { |
| 178 | | throw new \RuntimeException("Table [{$name}] does not exist in the Postgres schema."); |
| 179 | | } |
| 180 | | |
| 181 | | return new LiveTable( |
| 182 | | $name, |
| 183 | | $this->columns($name), |
| 184 | | $this->indexes($name), |
| 185 | | $this->foreignKeys($name), |
| 186 | | ); |
| 187 | | } |
| 188 | | |
| 189 | | |
| 190 | | |
| 191 | | |
| 192 | | |
| 193 | | |
| 194 | | |
| 195 | | private function columns(string $name): array |
| 196 | | { |
| 197 | | $statement = $this->pdo->prepare( |
| 198 | | 'SELECT c.column_name, c.udt_name, c.character_maximum_length, ' |
| 199 | | . 'c.numeric_precision, c.numeric_scale, c.is_nullable, c.column_default, ' |
| 200 | | . 'EXISTS (' |
| 201 | | . ' SELECT 1 FROM information_schema.table_constraints tc ' |
| 202 | | . ' JOIN information_schema.key_column_usage kcu ' |
| 203 | | . ' ON kcu.constraint_name = tc.constraint_name ' |
| 204 | | . ' AND kcu.table_schema = tc.table_schema ' |
| 205 | | . ' WHERE tc.table_schema = c.table_schema ' |
| 206 | | . ' AND tc.table_name = c.table_name ' |
| 207 | | . ' AND tc.constraint_type = \'PRIMARY KEY\' ' |
| 208 | | . ' AND kcu.column_name = c.column_name' |
| 209 | | . ') AS is_primary ' |
| 210 | | . 'FROM information_schema.columns c ' |
| 211 | | . 'WHERE c.table_schema = current_schema() AND c.table_name = ? ' |
| 212 | | . 'ORDER BY c.ordinal_position', |
| 213 | | ); |
| 214 | | $statement->execute([$name]); |
| 215 | | |
| 216 | | $columns = []; |
| 217 | | |
| 218 | | |
| 219 | | foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
| 220 | | $columns[] = [ |
| 221 | | 'name' => (string) $row['column_name'], |
| 222 | | |
| 223 | | |
| 224 | | |
| 225 | | |
| 226 | | |
| 227 | | 'type' => $this->composeType($row), |
| 228 | | 'nullable' => strtoupper((string) $row['is_nullable']) === 'YES', |
| 229 | | |
| 230 | | |
| 231 | | 'default' => $row['column_default'], |
| 232 | | 'primaryKey' => ((int) $row['is_primary']) === 1, |
| 233 | | ]; |
| 234 | | } |
| 235 | | |
| 236 | | return $columns; |
| 237 | | } |
| 238 | | |
| 239 | | |
| 240 | | |
| 241 | | |
| 242 | | |
| 243 | | |
| 244 | | |
| 245 | | private function indexes(string $name): array |
| 246 | | { |
| 247 | | $statement = $this->pdo->prepare( |
| 248 | | 'SELECT i.relname AS index_name, ix.indisunique, ix.indisprimary, ' |
| 249 | | . 'pg_get_indexdef(ix.indexrelid) AS indexdef ' |
| 250 | | . 'FROM pg_class t ' |
| 251 | | . 'JOIN pg_namespace n ON n.oid = t.relnamespace ' |
| 252 | | . 'JOIN pg_index ix ON ix.indrelid = t.oid ' |
| 253 | | . 'JOIN pg_class i ON i.oid = ix.indexrelid ' |
| 254 | | . 'WHERE n.nspname = current_schema() AND t.relname = ? ' |
| 255 | | . 'ORDER BY i.relname', |
| 256 | | ); |
| 257 | | $statement->execute([$name]); |
| 258 | | |
| 259 | | $indexes = []; |
| 260 | | |
| 261 | | |
| 262 | | foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
| 263 | | if (((int) $row['indisprimary']) === 1) { |
| 264 | | continue; |
| 265 | | } |
| 266 | | |
| 267 | | $indexdef = (string) $row['indexdef']; |
| 268 | | |
| 269 | | $indexes[] = [ |
| 270 | | 'name' => (string) $row['index_name'], |
| 271 | | |
| 272 | | |
| 273 | | |
| 274 | | 'columns' => $this->parseIndexColumns($indexdef), |
| 275 | | 'unique' => ((int) $row['indisunique']) === 1, |
| 276 | | |
| 277 | | |
| 278 | | |
| 279 | | 'where' => $this->parseIndexWhere($indexdef), |
| 280 | | 'nullsNotDistinct' => str_contains($indexdef, 'NULLS NOT DISTINCT'), |
| 281 | | ]; |
| 282 | | } |
| 283 | | |
| 284 | | return $indexes; |
| 285 | | } |
| 286 | | |
| 287 | | |
| 288 | | |
| 289 | | |
| 290 | | |
| 291 | | |
| 292 | | |
| 293 | | private function parseIndexColumns(string $indexdef): array |
| 294 | | { |
| 295 | | $paren = strrpos($indexdef, '('); |
| 296 | | |
| 297 | | if ($paren === false) { |
| 298 | | return []; |
| 299 | | } |
| 300 | | |
| 301 | | $inner = rtrim(substr($indexdef, $paren + 1), ') '); |
| 302 | | |
| 303 | | return array_map( |
| 304 | | fn (string $column) => trim(trim($column), '"'), |
| 305 | | explode(',', $inner), |
| 306 | | ); |
| 307 | | } |
| 308 | | |
| 309 | | |
| 310 | | |
| 311 | | |
| 312 | | |
| 313 | | |
| 314 | | |
| 315 | | |
| 316 | | private function parseIndexWhere(string $indexdef): ?string |
| 317 | | { |
| 318 | | $where = strripos($indexdef, ' WHERE '); |
| 319 | | |
| 320 | | if ($where === false) { |
| 321 | | return null; |
| 322 | | } |
| 323 | | |
| 324 | | return trim(substr($indexdef, $where + 7)); |
| 325 | | } |
| 326 | | |
| 327 | | |
| 328 | | |
| 329 | | |
| 330 | | |
| 331 | | |
| 332 | | |
| 333 | | |
| 334 | | private function foreignKeys(string $name): array |
| 335 | | { |
| 336 | | $statement = $this->pdo->prepare( |
| 337 | | 'SELECT tc.constraint_name, kcu.column_name, ccu.table_name AS referenced_table, ' |
| 338 | | . 'ccu.column_name AS referenced_column, kcu.ordinal_position, ' |
| 339 | | . 'rc.delete_rule, rc.update_rule, pc.condeferrable ' |
| 340 | | . 'FROM information_schema.table_constraints tc ' |
| 341 | | . 'JOIN information_schema.key_column_usage kcu ' |
| 342 | | . ' ON kcu.constraint_name = tc.constraint_name ' |
| 343 | | . ' AND kcu.table_schema = tc.table_schema ' |
| 344 | | . 'JOIN information_schema.constraint_column_usage ccu ' |
| 345 | | . ' ON ccu.constraint_name = tc.constraint_name ' |
| 346 | | . ' AND ccu.table_schema = tc.table_schema ' |
| 347 | | . 'JOIN information_schema.referential_constraints rc ' |
| 348 | | . ' ON rc.constraint_name = tc.constraint_name ' |
| 349 | | . ' AND rc.constraint_schema = tc.constraint_schema ' |
| 350 | | . 'JOIN pg_catalog.pg_constraint pc ' |
| 351 | | . ' ON pc.conname = tc.constraint_name ' |
| 352 | | . ' AND pc.connamespace = (SELECT oid FROM pg_catalog.pg_namespace ' |
| 353 | | . ' WHERE nspname = current_schema()) ' |
| 354 | | . 'WHERE tc.table_schema = current_schema() ' |
| 355 | | . 'AND tc.table_name = ? AND tc.constraint_type = \'FOREIGN KEY\' ' |
| 356 | | . 'ORDER BY tc.constraint_name, kcu.ordinal_position', |
| 357 | | ); |
| 358 | | $statement->execute([$name]); |
| 359 | | |
| 360 | | |
| 361 | | $rows = $statement->fetchAll(\PDO::FETCH_ASSOC); |
| 362 | | |
| 363 | | $groups = []; |
| 364 | | |
| 365 | | foreach ($rows as $row) { |
| 366 | | $constraintName = (string) $row['constraint_name']; |
| 367 | | $groups[$constraintName]['columns'][] = (string) $row['column_name']; |
| 368 | | $groups[$constraintName]['referencesTable'] = (string) $row['referenced_table']; |
| 369 | | $groups[$constraintName]['referencesColumns'][(int) $row['ordinal_position']] = (string) $row['referenced_column']; |
| 370 | | $groups[$constraintName]['onDelete'] = $row['delete_rule']; |
| 371 | | $groups[$constraintName]['onUpdate'] = $row['update_rule']; |
| 372 | | $groups[$constraintName]['deferrable'] = $row['condeferrable']; |
| 373 | | } |
| 374 | | |
| 375 | | $constraints = []; |
| 376 | | |
| 377 | | foreach ($groups as $constraintName => $group) { |
| 378 | | $constraints[] = [ |
| 379 | | 'columns' => $group['columns'], |
| 380 | | 'referencesTable' => $group['referencesTable'], |
| 381 | | 'referencesColumns' => array_values($group['referencesColumns']), |
| 382 | | 'onDelete' => $this->normalizeAction($group['onDelete']), |
| 383 | | 'onUpdate' => $this->normalizeAction($group['onUpdate']), |
| 384 | | 'deferrable' => ((int) $group['deferrable']) === 1, |
| 385 | | |
| 386 | | 'name' => $constraintName, |
| 387 | | ]; |
| 388 | | } |
| 389 | | |
| 390 | | return $constraints; |
| 391 | | } |
| 392 | | |
| 393 | | |
| 394 | | |
| 395 | | |
| 396 | | |
| 397 | | |
| 398 | | |
| 399 | | private function normalizeAction(mixed $action): ?string |
| 400 | | { |
| 401 | | $normalized = strtoupper(trim((string) $action)); |
| 402 | | |
| 403 | | return $normalized === 'NO ACTION' ? null : $normalized; |
| 404 | | } |
| 405 | | } |