-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Schema.php
1136 lines (1027 loc) · 39.9 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Yiisoft\Db\Pgsql;
use JsonException;
use Throwable;
use Yiisoft\Db\Constraint\CheckConstraint;
use Yiisoft\Db\Constraint\Constraint;
use Yiisoft\Db\Constraint\DefaultValueConstraint;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Constraint\IndexConstraint;
use Yiisoft\Db\Driver\Pdo\AbstractPdoSchema;
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Helper\DbArrayHelper;
use Yiisoft\Db\Pgsql\Column\ArrayColumnSchema;
use Yiisoft\Db\Pgsql\Column\BigIntColumnSchema;
use Yiisoft\Db\Pgsql\Column\BinaryColumnSchema;
use Yiisoft\Db\Pgsql\Column\BitColumnSchema;
use Yiisoft\Db\Pgsql\Column\BooleanColumnSchema;
use Yiisoft\Db\Pgsql\Column\IntegerColumnSchema;
use Yiisoft\Db\Pgsql\Column\SequenceColumnSchemaInterface;
use Yiisoft\Db\Pgsql\Column\StructuredColumnSchema;
use Yiisoft\Db\Pgsql\Column\StructuredColumnSchemaInterface;
use Yiisoft\Db\Schema\Builder\ColumnInterface;
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
use Yiisoft\Db\Schema\Column\StringColumnSchema;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Schema\TableSchemaInterface;
use function array_change_key_case;
use function array_map;
use function array_merge;
use function array_unique;
use function array_values;
use function explode;
use function is_string;
use function preg_match;
use function preg_replace;
use function str_replace;
use function str_starts_with;
use function substr;
/**
* Implements the PostgreSQL Server specific schema, supporting PostgreSQL Server version 9.6 and above.
*
* @psalm-type ColumnArray = array{
* table_schema: string,
* table_name: string,
* column_name: string,
* data_type: string,
* type_type: string|null,
* type_scheme: string|null,
* character_maximum_length: int,
* column_comment: string|null,
* modifier: int,
* is_nullable: bool,
* column_default: string|null,
* is_autoinc: bool,
* sequence_name: string|null,
* enum_values: string|null,
* numeric_precision: int|null,
* numeric_scale: int|null,
* size: string|null,
* is_pkey: bool|null,
* dimension: int
* }
* @psalm-type ConstraintArray = array<
* array-key,
* array {
* name: string,
* column_name: string,
* type: string,
* foreign_table_schema: string|null,
* foreign_table_name: string|null,
* foreign_column_name: string|null,
* on_update: string,
* on_delete: string,
* check_expr: string
* }
* >
* @psalm-type FindConstraintArray = array{
* constraint_name: string,
* column_name: string,
* foreign_table_name: string,
* foreign_table_schema: string,
* foreign_column_name: string,
* }
* @psalm-type CreateInfo = array{
* dimension?: int|string,
* columns?: array<string, ColumnSchemaInterface>
* }
*/
final class Schema extends AbstractPdoSchema
{
/**
* Define the abstract column type as `bit`.
*/
public const TYPE_BIT = 'bit';
/**
* Define the abstract column type as `array`.
*/
public const TYPE_ARRAY = 'array';
/**
* Define the abstract column type as `structured`.
*/
public const TYPE_STRUCTURED = 'structured';
/**
* The mapping from physical column types (keys) to abstract column types (values).
*
* @link https://www.postgresql.org/docs/current/datatype.html#DATATYPE-TABLE
*
* @var string[]
*/
private const TYPE_MAP = [
'bit' => self::TYPE_BIT,
'bit varying' => self::TYPE_BIT,
'varbit' => self::TYPE_BIT,
'bool' => self::TYPE_BOOLEAN,
'boolean' => self::TYPE_BOOLEAN,
'box' => self::TYPE_STRING,
'circle' => self::TYPE_STRING,
'point' => self::TYPE_STRING,
'line' => self::TYPE_STRING,
'lseg' => self::TYPE_STRING,
'polygon' => self::TYPE_STRING,
'path' => self::TYPE_STRING,
'character' => self::TYPE_CHAR,
'char' => self::TYPE_CHAR,
'bpchar' => self::TYPE_CHAR,
'character varying' => self::TYPE_STRING,
'varchar' => self::TYPE_STRING,
'text' => self::TYPE_TEXT,
'bytea' => self::TYPE_BINARY,
'cidr' => self::TYPE_STRING,
'inet' => self::TYPE_STRING,
'macaddr' => self::TYPE_STRING,
'real' => self::TYPE_FLOAT,
'float4' => self::TYPE_FLOAT,
'double precision' => self::TYPE_DOUBLE,
'float8' => self::TYPE_DOUBLE,
'decimal' => self::TYPE_DECIMAL,
'numeric' => self::TYPE_DECIMAL,
'money' => self::TYPE_MONEY,
'smallint' => self::TYPE_SMALLINT,
'int2' => self::TYPE_SMALLINT,
'int4' => self::TYPE_INTEGER,
'int' => self::TYPE_INTEGER,
'integer' => self::TYPE_INTEGER,
'bigint' => self::TYPE_BIGINT,
'int8' => self::TYPE_BIGINT,
'oid' => self::TYPE_BIGINT, // shouldn't be used. it's pg internal!
'smallserial' => self::TYPE_SMALLINT,
'serial2' => self::TYPE_SMALLINT,
'serial4' => self::TYPE_INTEGER,
'serial' => self::TYPE_INTEGER,
'bigserial' => self::TYPE_BIGINT,
'serial8' => self::TYPE_BIGINT,
'pg_lsn' => self::TYPE_BIGINT,
'date' => self::TYPE_DATE,
'interval' => self::TYPE_STRING,
'time without time zone' => self::TYPE_TIME,
'time' => self::TYPE_TIME,
'time with time zone' => self::TYPE_TIME,
'timetz' => self::TYPE_TIME,
'timestamp without time zone' => self::TYPE_TIMESTAMP,
'timestamp' => self::TYPE_TIMESTAMP,
'timestamp with time zone' => self::TYPE_TIMESTAMP,
'timestamptz' => self::TYPE_TIMESTAMP,
'abstime' => self::TYPE_TIMESTAMP,
'tsquery' => self::TYPE_STRING,
'tsvector' => self::TYPE_STRING,
'txid_snapshot' => self::TYPE_STRING,
'unknown' => self::TYPE_STRING,
'uuid' => self::TYPE_STRING,
'json' => self::TYPE_JSON,
'jsonb' => self::TYPE_JSON,
'xml' => self::TYPE_STRING,
];
/**
* @var string|null The default schema used for the current session.
*/
protected string|null $defaultSchema = 'public';
/**
* @var string|string[] Character used to quote schema, table, etc. names.
*
* An array of 2 characters can be used in case starting and ending characters are different.
*/
protected string|array $tableQuoteCharacter = '"';
public function createColumn(string $type, array|int|string $length = null): ColumnInterface
{
return new Column($type, $length);
}
/**
* Resolves the table name and schema name (if any).
*
* @param string $name The table name.
*
* @return TableSchemaInterface With resolved table, schema, etc. names.
*
* @see TableSchemaInterface
*/
protected function resolveTableName(string $name): TableSchemaInterface
{
$resolvedName = new TableSchema();
$parts = array_reverse($this->db->getQuoter()->getTableNameParts($name));
$resolvedName->name($parts[0] ?? '');
$resolvedName->schemaName($parts[1] ?? $this->defaultSchema);
$resolvedName->fullName(
$resolvedName->getSchemaName() !== $this->defaultSchema ?
implode('.', array_reverse($parts)) : $resolvedName->getName()
);
return $resolvedName;
}
/**
* Returns all schema names in the database, including the default one but not system schemas.
*
* This method should be overridden by child classes to support this feature because the default implementation
* simply throws an exception.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array All schemas name in the database, except system schemas.
*/
protected function findSchemaNames(): array
{
$sql = <<<SQL
SELECT "ns"."nspname"
FROM "pg_namespace" AS "ns"
WHERE "ns"."nspname" != 'information_schema' AND "ns"."nspname" NOT LIKE 'pg_%'
ORDER BY "ns"."nspname" ASC
SQL;
return $this->db->createCommand($sql)->queryColumn();
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findTableComment(TableSchemaInterface $tableSchema): void
{
$sql = <<<SQL
SELECT obj_description(pc.oid, 'pg_class')
FROM pg_catalog.pg_class pc
INNER JOIN pg_namespace pn ON pc.relnamespace = pn.oid
WHERE
pc.relname=:tableName AND
pn.nspname=:schemaName
SQL;
$comment = $this->db->createCommand($sql, [
':schemaName' => $tableSchema->getSchemaName(),
':tableName' => $tableSchema->getName(),
])->queryScalar();
$tableSchema->comment(is_string($comment) ? $comment : null);
}
/**
* Returns all table names in the database.
*
* This method should be overridden by child classes to support this feature because the default implementation
* simply throws an exception.
*
* @param string $schema The schema of the tables.
* Defaults to empty string, meaning the current or default schema.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array All tables name in the database. The names have NO schema name prefix.
*/
protected function findTableNames(string $schema = ''): array
{
if ($schema === '') {
$schema = $this->defaultSchema;
}
$sql = <<<SQL
SELECT c.relname AS table_name
FROM pg_class c
INNER JOIN pg_namespace ns ON ns.oid = c.relnamespace
WHERE ns.nspname = :schemaName AND c.relkind IN ('r','v','m','f', 'p')
ORDER BY c.relname
SQL;
return $this->db->createCommand($sql, [':schemaName' => $schema])->queryColumn();
}
/**
* Loads the metadata for the specified table.
*
* @param string $name The table name.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return TableSchemaInterface|null DBMS-dependent table metadata, `null` if the table doesn't exist.
*/
protected function loadTableSchema(string $name): TableSchemaInterface|null
{
$table = $this->resolveTableName($name);
$this->findTableComment($table);
if ($this->findColumns($table)) {
$this->findConstraints($table);
return $table;
}
return null;
}
/**
* Loads a primary key for the given table.
*
* @param string $tableName The table name.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return Constraint|null Primary key for the given table, `null` if the table has no primary key.
*/
protected function loadTablePrimaryKey(string $tableName): Constraint|null
{
$tablePrimaryKey = $this->loadTableConstraints($tableName, self::PRIMARY_KEY);
return $tablePrimaryKey instanceof Constraint ? $tablePrimaryKey : null;
}
/**
* Loads all foreign keys for the given table.
*
* @param string $tableName The table name.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array Foreign keys for the given table.
*
* @psaml-return array|ForeignKeyConstraint[]
*/
protected function loadTableForeignKeys(string $tableName): array
{
$tableForeignKeys = $this->loadTableConstraints($tableName, self::FOREIGN_KEYS);
return is_array($tableForeignKeys) ? $tableForeignKeys : [];
}
/**
* Loads all indexes for the given table.
*
* @param string $tableName The table name.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return IndexConstraint[] Indexes for the given table.
*/
protected function loadTableIndexes(string $tableName): array
{
$sql = <<<SQL
SELECT
"ic"."relname" AS "name",
"ia"."attname" AS "column_name",
"i"."indisunique" AS "index_is_unique",
"i"."indisprimary" AS "index_is_primary"
FROM "pg_class" AS "tc"
INNER JOIN "pg_namespace" AS "tcns"
ON "tcns"."oid" = "tc"."relnamespace"
LEFT JOIN pg_rewrite AS rw
ON tc.relkind = 'v' AND rw.ev_class = tc.oid AND rw.rulename = '_RETURN'
INNER JOIN "pg_index" AS "i"
ON "i"."indrelid" = "tc"."oid"
OR rw.ev_action IS NOT NULL
AND (SELECT regexp_matches(
rw.ev_action,
'{TARGETENTRY .*? :resorigtbl ' || "i"."indrelid" || ' :resorigcol ' || "i"."indkey"[0] || ' '
)) IS NOT NULL
INNER JOIN "pg_class" AS "ic"
ON "ic"."oid" = "i"."indexrelid"
INNER JOIN "pg_attribute" AS "ia"
ON "ia"."attrelid" = "i"."indexrelid" AND "ia"."attnum" <= cardinality("i"."indoption")
WHERE "tcns"."nspname" = :schemaName AND "tc"."relname" = :tableName
ORDER BY "ia"."attnum" ASC
SQL;
$resolvedName = $this->resolveTableName($tableName);
$indexes = $this->db->createCommand($sql, [
':schemaName' => $resolvedName->getSchemaName(),
':tableName' => $resolvedName->getName(),
])->queryAll();
/** @psalm-var array[] $indexes */
$indexes = array_map('array_change_key_case', $indexes);
$indexes = DbArrayHelper::index($indexes, null, ['name']);
$result = [];
/**
* @psalm-var object|string|null $name
* @psalm-var array<
* array-key,
* array{
* name: string,
* column_name: string,
* index_is_unique: bool,
* index_is_primary: bool
* }
* > $index
*/
foreach ($indexes as $name => $index) {
$ic = (new IndexConstraint())
->name($name)
->columnNames(DbArrayHelper::getColumn($index, 'column_name'))
->primary($index[0]['index_is_primary'])
->unique($index[0]['index_is_unique']);
$result[] = $ic;
}
return $result;
}
/**
* Loads all unique constraints for the given table.
*
* @param string $tableName The table name.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array Unique constraints for the given table.
*
* @psalm-return array|Constraint[]
*/
protected function loadTableUniques(string $tableName): array
{
$tableUniques = $this->loadTableConstraints($tableName, self::UNIQUES);
return is_array($tableUniques) ? $tableUniques : [];
}
/**
* Loads all check constraints for the given table.
*
* @param string $tableName The table name.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array Check constraints for the given table.
*
* @psaml-return array|CheckConstraint[]
*/
protected function loadTableChecks(string $tableName): array
{
$tableChecks = $this->loadTableConstraints($tableName, self::CHECKS);
return is_array($tableChecks) ? $tableChecks : [];
}
/**
* Loads all default value constraints for the given table.
*
* @param string $tableName The table name.
*
* @throws NotSupportedException
*
* @return DefaultValueConstraint[] Default value constraints for the given table.
*/
protected function loadTableDefaultValues(string $tableName): array
{
throw new NotSupportedException(__METHOD__ . ' is not supported by PostgreSQL.');
}
/**
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findViewNames(string $schema = ''): array
{
if ($schema === '') {
$schema = $this->defaultSchema;
}
$sql = <<<SQL
SELECT c.relname AS table_name
FROM pg_class c
INNER JOIN pg_namespace ns ON ns.oid = c.relnamespace
WHERE ns.nspname = :schemaName AND (c.relkind = 'v' OR c.relkind = 'm')
ORDER BY c.relname
SQL;
return $this->db->createCommand($sql, [':schemaName' => $schema])->queryColumn();
}
/**
* Collects the foreign key column details for the given table.
*
* @param TableSchemaInterface $table The table metadata
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*/
protected function findConstraints(TableSchemaInterface $table): void
{
/**
* We need to extract the constraints de hard way since:
* {@see https://www.postgresql.org/message-id/26677.1086673982@sss.pgh.pa.us}
*/
$sql = <<<SQL
SELECT
ct.conname as constraint_name,
a.attname as column_name,
fc.relname as foreign_table_name,
fns.nspname as foreign_table_schema,
fa.attname as foreign_column_name
FROM
(SELECT ct.conname, ct.conrelid, ct.confrelid, ct.conkey, ct.contype, ct.confkey,
generate_subscripts(ct.conkey, 1) AS s
FROM pg_constraint ct
) AS ct
inner join pg_class c on c.oid=ct.conrelid
inner join pg_namespace ns on c.relnamespace=ns.oid
inner join pg_attribute a on a.attrelid=ct.conrelid and a.attnum = ct.conkey[ct.s]
left join pg_class fc on fc.oid=ct.confrelid
left join pg_namespace fns on fc.relnamespace=fns.oid
left join pg_attribute fa on fa.attrelid=ct.confrelid and fa.attnum = ct.confkey[ct.s]
WHERE
ct.contype='f'
and c.relname=:tableName
and ns.nspname=:schemaName
ORDER BY
fns.nspname, fc.relname, a.attnum
SQL;
/** @psalm-var array{array{tableName: string, columns: array}} $constraints */
$constraints = [];
/** @psalm-var array<FindConstraintArray> $rows */
$rows = $this->db->createCommand($sql, [
':schemaName' => $table->getSchemaName(),
':tableName' => $table->getName(),
])->queryAll();
foreach ($rows as $constraint) {
/** @psalm-var FindConstraintArray $constraint */
$constraint = array_change_key_case($constraint);
if ($constraint['foreign_table_schema'] !== $this->defaultSchema) {
$foreignTable = $constraint['foreign_table_schema'] . '.' . $constraint['foreign_table_name'];
} else {
$foreignTable = $constraint['foreign_table_name'];
}
$name = $constraint['constraint_name'];
if (!isset($constraints[$name])) {
$constraints[$name] = [
'tableName' => $foreignTable,
'columns' => [],
];
}
$constraints[$name]['columns'][$constraint['column_name']] = $constraint['foreign_column_name'];
}
/**
* @psalm-var int|string $foreingKeyName.
* @psalm-var array{tableName: string, columns: array} $constraint
*/
foreach ($constraints as $foreingKeyName => $constraint) {
$table->foreignKey(
(string) $foreingKeyName,
array_merge([$constraint['tableName']], $constraint['columns'])
);
}
}
/**
* Gets information about given table unique indexes.
*
* @param TableSchemaInterface $table The table metadata.
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array With index and column names.
*/
protected function getUniqueIndexInformation(TableSchemaInterface $table): array
{
$sql = <<<'SQL'
SELECT
i.relname as indexname,
pg_get_indexdef(idx.indexrelid, k + 1, TRUE) AS columnname
FROM (
SELECT *, generate_subscripts(indkey, 1) AS k
FROM pg_index
) idx
INNER JOIN pg_class i ON i.oid = idx.indexrelid
INNER JOIN pg_class c ON c.oid = idx.indrelid
INNER JOIN pg_namespace ns ON c.relnamespace = ns.oid
WHERE idx.indisprimary = FALSE AND idx.indisunique = TRUE
AND c.relname = :tableName AND ns.nspname = :schemaName
ORDER BY i.relname, k
SQL;
return $this->db->createCommand($sql, [
':schemaName' => $table->getSchemaName(),
':tableName' => $table->getName(),
])->queryAll();
}
/**
* Returns all unique indexes for the given table.
*
* Each array element is of the following structure:
*
* ```php
* [
* 'IndexName1' => ['col1' [, ...]],
* 'IndexName2' => ['col2' [, ...]],
* ]
* ```
*
* @param TableSchemaInterface $table The table metadata
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array All unique indexes for the given table.
*/
public function findUniqueIndexes(TableSchemaInterface $table): array
{
$uniqueIndexes = [];
/** @psalm-var array{indexname: string, columnname: string} $row */
foreach ($this->getUniqueIndexInformation($table) as $row) {
/** @psalm-var array{indexname: string, columnname: string} $row */
$row = array_change_key_case($row);
$column = $row['columnname'];
if (str_starts_with($column, '"') && str_ends_with($column, '"')) {
/**
* postgres will quote names that aren't lowercase-only.
*
* {@see https://github.com/yiisoft/yii2/issues/10613}
*/
$column = substr($column, 1, -1);
}
$uniqueIndexes[$row['indexname']][] = $column;
}
return $uniqueIndexes;
}
/**
* Collects the metadata of table columns.
*
* @param TableSchemaInterface $table The table metadata.
*
* @throws Exception
* @throws InvalidConfigException
* @throws JsonException
* @throws Throwable
*
* @return bool Whether the table exists in the database.
*/
protected function findColumns(TableSchemaInterface $table): bool
{
$orIdentity = '';
if (version_compare($this->db->getServerVersion(), '12.0', '>=')) {
$orIdentity = 'OR a.attidentity != \'\'';
}
$sql = <<<SQL
SELECT
d.nspname AS table_schema,
c.relname AS table_name,
a.attname AS column_name,
COALESCE(td.typname, tb.typname, t.typname) AS data_type,
COALESCE(td.typtype, tb.typtype, t.typtype) AS type_type,
(SELECT nspname FROM pg_namespace WHERE oid = COALESCE(td.typnamespace, tb.typnamespace, t.typnamespace)) AS type_scheme,
a.attlen AS character_maximum_length,
pg_catalog.col_description(c.oid, a.attnum) AS column_comment,
information_schema._pg_truetypmod(a, t) AS modifier,
NOT (a.attnotnull OR t.typnotnull) AS is_nullable,
COALESCE(t.typdefault, pg_get_expr(ad.adbin, ad.adrelid)) AS column_default,
COALESCE(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval', false) $orIdentity AS is_autoinc,
pg_get_serial_sequence(quote_ident(d.nspname) || '.' || quote_ident(c.relname), a.attname)
AS sequence_name,
CASE WHEN COALESCE(td.typtype, tb.typtype, t.typtype) = 'e'::char
THEN array_to_string(
(
SELECT array_agg(enumlabel)
FROM pg_enum
WHERE enumtypid = COALESCE(td.oid, tb.oid, a.atttypid)
)::varchar[],
',')
ELSE NULL
END AS enum_values,
information_schema._pg_numeric_precision(
COALESCE(td.oid, tb.oid, a.atttypid),
information_schema._pg_truetypmod(a, t)
) AS numeric_precision,
information_schema._pg_numeric_scale(
COALESCE(td.oid, tb.oid, a.atttypid),
information_schema._pg_truetypmod(a, t)
) AS numeric_scale,
information_schema._pg_char_max_length(
COALESCE(td.oid, tb.oid, a.atttypid),
information_schema._pg_truetypmod(a, t)
) AS size,
ct.oid IS NOT NULL AS is_pkey,
COALESCE(NULLIF(a.attndims, 0), NULLIF(t.typndims, 0), (t.typcategory='A')::int) AS dimension
FROM
pg_class c
LEFT JOIN pg_attribute a ON a.attrelid = c.oid
LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_type tb ON (a.attndims > 0 OR t.typcategory='A') AND t.typelem > 0 AND t.typelem = tb.oid
OR t.typbasetype > 0 AND t.typbasetype = tb.oid
LEFT JOIN pg_type td ON t.typndims > 0 AND t.typbasetype > 0 AND tb.typelem = td.oid
LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
LEFT JOIN pg_rewrite rw ON c.relkind = 'v' AND rw.ev_class = c.oid AND rw.rulename = '_RETURN'
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p' AND a.attnum = ANY (ct.conkey)
OR rw.ev_action IS NOT NULL AND ct.contype = 'p'
AND (ARRAY(
SELECT regexp_matches(rw.ev_action, '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
))[a.attnum:a.attnum] <@ (ct.conrelid::text || ct.conkey::text[])
WHERE
a.attnum > 0 AND t.typname != '' AND NOT a.attisdropped
AND c.relname = :tableName
AND d.nspname = :schemaName
ORDER BY
a.attnum;
SQL;
$columns = $this->db->createCommand($sql, [
':schemaName' => $table->getSchemaName(),
':tableName' => $table->getName(),
])->queryAll();
if (empty($columns)) {
return false;
}
/** @psalm-var ColumnArray $info */
foreach ($columns as $info) {
/** @psalm-var ColumnArray $info */
$info = array_change_key_case($info);
$column = $this->loadColumnSchema($info);
$table->column($info['column_name'], $column);
if ($column->isPrimaryKey()) {
$table->primaryKey($info['column_name']);
if ($column instanceof SequenceColumnSchemaInterface && $table->getSequenceName() === null) {
$table->sequenceName($column->getSequenceName());
}
}
}
return true;
}
/**
* Loads the column information into a {@see ColumnSchemaInterface} object.
*
* @psalm-param ColumnArray $info Column information.
*
* @return ColumnSchemaInterface The column schema object.
*/
private function loadColumnSchema(array $info): ColumnSchemaInterface
{
$dbType = $info['data_type'];
if (!in_array($info['type_scheme'], [$this->defaultSchema, 'pg_catalog'], true)) {
$dbType = $info['type_scheme'] . '.' . $dbType;
}
$columns = [];
if ($info['type_type'] === 'c') {
$type = self::TYPE_STRUCTURED;
$structured = $this->resolveTableName($dbType);
if ($this->findColumns($structured)) {
$columns = $structured->getColumns();
}
} else {
$type = self::TYPE_MAP[$dbType] ?? self::TYPE_STRING;
}
$column = $this->createColumnSchema($type, dimension: $info['dimension'], columns: $columns);
$column->name($info['column_name']);
$column->dbType($dbType);
$column->allowNull($info['is_nullable']);
$column->autoIncrement($info['is_autoinc']);
$column->comment($info['column_comment']);
$column->enumValues($info['enum_values'] !== null
? explode(',', str_replace(["''"], ["'"], $info['enum_values']))
: null);
$column->primaryKey((bool) $info['is_pkey']);
$column->precision($info['numeric_precision']);
$column->scale($info['numeric_scale']);
$column->size($info['size'] === null ? null : (int) $info['size']);
/**
* pg_get_serial_sequence() doesn't track DEFAULT value change.
* GENERATED BY IDENTITY columns always have a null default value.
*/
$defaultValue = $info['column_default'];
if ($column instanceof SequenceColumnSchemaInterface) {
if (
$defaultValue !== null
&& preg_match("/^nextval\('([^']+)/", $defaultValue, $matches) === 1
) {
$column->sequenceName($matches[1]);
} elseif ($info['sequence_name'] !== null) {
$column->sequenceName($this->resolveTableName($info['sequence_name'])->getFullName());
}
} elseif ($column instanceof ArrayColumnSchema) {
/** @var ColumnSchemaInterface $arrayColumn */
$arrayColumn = $column->getColumn();
$arrayColumn->dbType($dbType);
$arrayColumn->enumValues($column->getEnumValues());
$arrayColumn->precision($info['numeric_precision']);
$arrayColumn->scale($info['numeric_scale']);
$arrayColumn->size($info['size'] === null ? null : (int) $info['size']);
}
$column->defaultValue($this->normalizeDefaultValue($defaultValue, $column));
if ($column instanceof StructuredColumnSchemaInterface) {
/** @psalm-var array|null $defaultValue */
$defaultValue = $column->getDefaultValue();
if (is_array($defaultValue)) {
foreach ($column->getColumns() as $structuredColumnName => $structuredColumn) {
$structuredColumn->defaultValue($defaultValue[$structuredColumnName] ?? null);
}
}
}
return $column;
}
protected function getColumnPhpType(string $type, bool $isUnsigned = false): string
{
return match ($type) {
self::TYPE_BIT => self::PHP_TYPE_INTEGER,
self::TYPE_STRUCTURED => self::PHP_TYPE_ARRAY,
default => parent::getColumnPhpType($type, $isUnsigned),
};
}
protected function createColumnSchemaFromPhpType(string $phpType, string $type): ColumnSchemaInterface
{
return match ($phpType) {
self::PHP_TYPE_STRING => $type === SchemaInterface::TYPE_BIGINT
? new BigIntColumnSchema($type, $phpType)
: new StringColumnSchema($type, $phpType),
self::PHP_TYPE_INTEGER => new IntegerColumnSchema($type, $phpType),
self::PHP_TYPE_BOOLEAN => new BooleanColumnSchema($type, $phpType),
self::PHP_TYPE_RESOURCE => new BinaryColumnSchema($type, $phpType),
default => parent::createColumnSchemaFromPhpType($phpType, $type),
};
}
/**
* Converts column's default value according to {@see ColumnSchema::phpType} after retrieval from the database.
*
* @param string|null $defaultValue The default value retrieved from the database.
* @param ColumnSchemaInterface $column The column schema object.
*
* @return mixed The normalized default value.
*/
private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaInterface $column): mixed
{
if (
$defaultValue === null
|| $column->isPrimaryKey()
|| str_starts_with($defaultValue, 'NULL::')
) {
return null;
}
if ($column->getType() === self::TYPE_BOOLEAN && in_array($defaultValue, ['true', 'false'], true)) {
return $defaultValue === 'true';
}
if (
in_array($column->getType(), [self::TYPE_TIMESTAMP, self::TYPE_DATE, self::TYPE_TIME], true)
&& in_array(strtoupper($defaultValue), ['NOW()', 'CURRENT_TIMESTAMP', 'CURRENT_DATE', 'CURRENT_TIME'], true)
) {
return new Expression($defaultValue);
}
$value = preg_replace("/^B?['(](.*?)[)'](?:::[^:]+)?$/s", '$1', $defaultValue);
$value = str_replace("''", "'", $value);
if ($column->getType() === self::TYPE_BINARY && str_starts_with($value, '\\x')) {
return hex2bin(substr($value, 2));
}
return $column->phpTypecast($value);
}
/**
* Loads multiple types of constraints and returns the specified ones.
*
* @param string $tableName The table name.
* @param string $returnType The return type:
* - primaryKey
* - foreignKeys
* - uniques
* - checks
*
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @return array|Constraint|null Constraints.
*
* @psalm-return CheckConstraint[]|Constraint[]|ForeignKeyConstraint[]|Constraint|null
*/
private function loadTableConstraints(string $tableName, string $returnType): array|Constraint|null
{
$sql = <<<SQL
SELECT
"c"."conname" AS "name",
"a"."attname" AS "column_name",
"c"."contype" AS "type",
"ftcns"."nspname" AS "foreign_table_schema",
"ftc"."relname" AS "foreign_table_name",
"fa"."attname" AS "foreign_column_name",
"c"."confupdtype" AS "on_update",
"c"."confdeltype" AS "on_delete",
pg_get_constraintdef("c"."oid") AS "check_expr"
FROM "pg_class" AS "tc"
INNER JOIN "pg_namespace" AS "tcns"
ON "tcns"."oid" = "tc"."relnamespace"
INNER JOIN "pg_attribute" AS "a"
ON "a"."attrelid" = "tc"."oid"
LEFT JOIN pg_rewrite AS rw
ON "tc"."relkind" = 'v' AND "rw"."ev_class" = "tc"."oid" AND "rw"."rulename" = '_RETURN'
INNER JOIN "pg_constraint" AS "c"
ON "c"."conrelid" = "tc"."oid" AND "a"."attnum" = ANY ("c"."conkey")
OR "rw"."ev_action" IS NOT NULL AND "c"."conrelid" != 0
AND (ARRAY(
SELECT regexp_matches("rw"."ev_action", '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
))["a"."attnum":"a"."attnum"] <@ ("c"."conrelid"::text || "c"."conkey"::text[])
LEFT JOIN "pg_class" AS "ftc"
ON "ftc"."oid" = "c"."confrelid"
LEFT JOIN "pg_namespace" AS "ftcns"
ON "ftcns"."oid" = "ftc"."relnamespace"
LEFT JOIN "pg_attribute" "fa"
ON "fa"."attrelid" = "c"."confrelid" AND "fa"."attnum" = ANY ("c"."confkey")
WHERE "tcns"."nspname" = :schemaName AND "tc"."relname" = :tableName
ORDER BY "a"."attnum" ASC, "fa"."attnum" ASC
SQL;
/** @psalm-var string[] $actionTypes */
$actionTypes = [
'a' => 'NO ACTION',