/
inspect.go
995 lines (942 loc) · 28.6 KB
/
inspect.go
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
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package postgres
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"ariga.io/atlas/sql/internal/sqlx"
"ariga.io/atlas/sql/schema"
)
// A diff provides a PostgreSQL implementation for schema.Inspector.
type inspect struct{ conn }
var _ schema.Inspector = (*inspect)(nil)
// InspectRealm returns schema descriptions of all resources in the given realm.
func (i *inspect) InspectRealm(ctx context.Context, opts *schema.InspectRealmOption) (*schema.Realm, error) {
schemas, err := i.schemas(ctx, opts)
if err != nil {
return nil, err
}
r := schema.NewRealm(schemas...).SetCollation(i.collate)
r.Attrs = append(r.Attrs, &CType{V: i.ctype})
if len(schemas) == 0 || !sqlx.ModeInspectRealm(opts).Is(schema.InspectTables) {
return r, nil
}
if err := i.inspectTables(ctx, r, nil); err != nil {
return nil, err
}
sqlx.LinkSchemaTables(schemas)
return r, nil
}
// InspectSchema returns schema descriptions of the tables in the given schema.
// If the schema name is empty, the result will be the attached schema.
func (i *inspect) InspectSchema(ctx context.Context, name string, opts *schema.InspectOptions) (s *schema.Schema, err error) {
schemas, err := i.schemas(ctx, &schema.InspectRealmOption{Schemas: []string{name}})
if err != nil {
return nil, err
}
switch n := len(schemas); {
case n == 0:
return nil, &schema.NotExistError{Err: fmt.Errorf("postgres: schema %q was not found", name)}
case n > 1:
return nil, fmt.Errorf("postgres: %d schemas were found for %q", n, name)
}
r := schema.NewRealm(schemas...).SetCollation(i.collate)
r.Attrs = append(r.Attrs, &CType{V: i.ctype})
if sqlx.ModeInspectSchema(opts).Is(schema.InspectTables) {
if err := i.inspectTables(ctx, r, opts); err != nil {
return nil, err
}
sqlx.LinkSchemaTables(schemas)
}
return r.Schemas[0], nil
}
func (i *inspect) inspectTables(ctx context.Context, r *schema.Realm, opts *schema.InspectOptions) error {
if err := i.tables(ctx, r, opts); err != nil {
return err
}
for _, s := range r.Schemas {
if len(s.Tables) == 0 {
continue
}
if err := i.columns(ctx, s); err != nil {
return err
}
if err := i.indexes(ctx, s); err != nil {
return err
}
if err := i.partitions(s); err != nil {
return err
}
if err := i.fks(ctx, s); err != nil {
return err
}
if err := i.checks(ctx, s); err != nil {
return err
}
}
return nil
}
// table returns the table from the database, or a NotExistError if the table was not found.
func (i *inspect) tables(ctx context.Context, realm *schema.Realm, opts *schema.InspectOptions) error {
var (
args []interface{}
query = fmt.Sprintf(tablesQuery, nArgs(0, len(realm.Schemas)))
)
for _, s := range realm.Schemas {
args = append(args, s.Name)
}
if opts != nil && len(opts.Tables) > 0 {
for _, t := range opts.Tables {
args = append(args, t)
}
query = fmt.Sprintf(tablesQueryArgs, nArgs(0, len(realm.Schemas)), nArgs(len(realm.Schemas), len(opts.Tables)))
}
rows, err := i.QueryContext(ctx, query, args...)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var tSchema, name, comment, partattrs, partstart, partexprs sql.NullString
if err := rows.Scan(&tSchema, &name, &comment, &partattrs, &partstart, &partexprs); err != nil {
return fmt.Errorf("scan table information: %w", err)
}
if !sqlx.ValidString(tSchema) || !sqlx.ValidString(name) {
return fmt.Errorf("invalid schema or table name: %q.%q", tSchema.String, name.String)
}
s, ok := realm.Schema(tSchema.String)
if !ok {
return fmt.Errorf("schema %q was not found in realm", tSchema.String)
}
t := &schema.Table{Name: name.String}
s.AddTables(t)
if sqlx.ValidString(comment) {
t.SetComment(comment.String)
}
if sqlx.ValidString(partattrs) {
t.AddAttrs(&Partition{
start: partstart.String,
attrs: partattrs.String,
exprs: partexprs.String,
})
}
}
return rows.Close()
}
// columns queries and appends the columns of the given table.
func (i *inspect) columns(ctx context.Context, s *schema.Schema) error {
rows, err := i.querySchema(ctx, columnsQuery, s)
if err != nil {
return fmt.Errorf("postgres: querying schema %q columns: %w", s.Name, err)
}
defer rows.Close()
for rows.Next() {
if err := i.addColumn(s, rows); err != nil {
return fmt.Errorf("postgres: %w", err)
}
}
if err := rows.Close(); err != nil {
return err
}
if err := i.enumValues(ctx, s); err != nil {
return err
}
return nil
}
// addColumn scans the current row and adds a new column from it to the table.
func (i *inspect) addColumn(s *schema.Schema, rows *sql.Rows) error {
var (
typid, maxlen, precision, timeprecision, scale, seqstart, seqinc sql.NullInt64
table, name, typ, nullable, defaults, udt, identity, genidentity, genexpr, charset, collate, comment, typtype sql.NullString
)
if err := rows.Scan(
&table, &name, &typ, &nullable, &defaults, &maxlen, &precision, &timeprecision, &scale, &charset,
&collate, &udt, &identity, &seqstart, &seqinc, &genidentity, &genexpr, &comment, &typtype, &typid,
); err != nil {
return err
}
t, ok := s.Table(table.String)
if !ok {
return fmt.Errorf("table %q was not found in schema", table.String)
}
c := &schema.Column{
Name: name.String,
Type: &schema.ColumnType{
Raw: typ.String,
Null: nullable.String == "YES",
},
}
c.Type.Type = columnType(&columnDesc{
typ: typ.String,
size: maxlen.Int64,
udt: udt.String,
scale: scale.Int64,
typtype: typtype.String,
typid: typid.Int64,
precision: precision.Int64,
timePrecision: &timeprecision.Int64,
})
if defaults.Valid {
c.Default = defaultExpr(c, defaults.String)
}
if identity.String == "YES" {
c.Attrs = append(c.Attrs, &Identity{
Generation: genidentity.String,
Sequence: &Sequence{
Start: seqstart.Int64,
Increment: seqinc.Int64,
},
})
}
if sqlx.ValidString(genexpr) {
c.Attrs = append(c.Attrs, &schema.GeneratedExpr{
Expr: genexpr.String,
})
}
if sqlx.ValidString(comment) {
c.SetComment(comment.String)
}
if sqlx.ValidString(charset) {
c.SetCharset(charset.String)
}
if sqlx.ValidString(collate) {
c.SetCollation(collate.String)
}
t.Columns = append(t.Columns, c)
return nil
}
func columnType(c *columnDesc) schema.Type {
var typ schema.Type
switch t := c.typ; strings.ToLower(t) {
case TypeBigInt, TypeInt8, TypeInt, TypeInteger, TypeInt4, TypeSmallInt, TypeInt2:
typ = &schema.IntegerType{T: t}
case TypeBit, TypeBitVar:
typ = &BitType{T: t, Len: c.size}
case TypeBool, TypeBoolean:
typ = &schema.BoolType{T: t}
case TypeBytea:
typ = &schema.BinaryType{T: t}
case TypeCharacter, TypeChar, TypeCharVar, TypeVarChar, TypeText:
// A `character` column without length specifier is equivalent to `character(1)`,
// but `varchar` without length accepts strings of any size (same as `text`).
typ = &schema.StringType{T: t, Size: int(c.size)}
case TypeCIDR, TypeInet, TypeMACAddr, TypeMACAddr8:
typ = &NetworkType{T: t}
case TypeCircle, TypeLine, TypeLseg, TypeBox, TypePath, TypePolygon, TypePoint:
typ = &schema.SpatialType{T: t}
case TypeDate:
typ = &schema.TimeType{T: t}
case TypeTime, TypeTimeWOTZ, TypeTimeTZ, TypeTimeWTZ, TypeTimestamp,
TypeTimestampTZ, TypeTimestampWTZ, TypeTimestampWOTZ:
p := defaultTimePrecision
if c.timePrecision != nil {
p = int(*c.timePrecision)
}
typ = &schema.TimeType{T: t, Precision: &p}
case TypeInterval:
// TODO: get 'interval_type' from query above before implementing.
typ = &schema.UnsupportedType{T: t}
case TypeReal, TypeDouble, TypeFloat4, TypeFloat8:
typ = &schema.FloatType{T: t, Precision: int(c.precision)}
case TypeJSON, TypeJSONB:
typ = &schema.JSONType{T: t}
case TypeMoney:
typ = &CurrencyType{T: t}
case TypeDecimal, TypeNumeric:
typ = &schema.DecimalType{T: t, Precision: int(c.precision), Scale: int(c.scale)}
case TypeSmallSerial, TypeSerial, TypeBigSerial, TypeSerial2, TypeSerial4, TypeSerial8:
typ = &SerialType{T: t, Precision: int(c.precision)}
case TypeUUID:
typ = &UUIDType{T: t}
case TypeXML:
typ = &XMLType{T: t}
case TypeArray:
// Note that for ARRAY types, the 'udt_name' column holds the array type
// prefixed with '_'. For example, for 'integer[]' the result is '_int',
// and for 'text[N][M]' the result is also '_text'. That's because, the
// database ignores any size or multi-dimensions constraints.
typ = &ArrayType{T: strings.TrimPrefix(c.udt, "_") + "[]"}
case TypeUserDefined:
typ = &UserDefinedType{T: c.udt}
// The `typtype` column is set to 'e' for enum types, and the
// values are filled in batch after the rows above is closed.
// https://www.postgresql.org/docs/current/catalog-pg-type.html
if c.typtype == "e" {
typ = &enumType{T: c.udt, ID: c.typid}
}
default:
typ = &schema.UnsupportedType{T: t}
}
return typ
}
// enumValues fills enum columns with their values from the database.
func (i *inspect) enumValues(ctx context.Context, s *schema.Schema) error {
var (
args []interface{}
ids = make(map[int64][]*schema.EnumType)
query = "SELECT enumtypid, enumlabel FROM pg_enum WHERE enumtypid IN (%s)"
)
for _, t := range s.Tables {
for _, c := range t.Columns {
if enum, ok := c.Type.Type.(*enumType); ok {
if _, ok := ids[enum.ID]; !ok {
args = append(args, enum.ID)
}
// Convert the intermediate type to the
// standard schema.EnumType.
e := &schema.EnumType{T: enum.T}
c.Type.Type = e
c.Type.Raw = enum.T
ids[enum.ID] = append(ids[enum.ID], e)
}
}
}
if len(ids) == 0 {
return nil
}
rows, err := i.QueryContext(ctx, fmt.Sprintf(query, nArgs(0, len(args))), args...)
if err != nil {
return fmt.Errorf("postgres: querying enum values: %w", err)
}
defer rows.Close()
for rows.Next() {
var (
id int64
v string
)
if err := rows.Scan(&id, &v); err != nil {
return fmt.Errorf("postgres: scanning enum label: %w", err)
}
for _, enum := range ids[id] {
enum.Values = append(enum.Values, v)
}
}
return nil
}
// indexes queries and appends the indexes of the given table.
func (i *inspect) indexes(ctx context.Context, s *schema.Schema) error {
rows, err := i.querySchema(ctx, indexesQuery, s)
if err != nil {
return fmt.Errorf("postgres: querying schema %q indexes: %w", s.Name, err)
}
defer rows.Close()
if err := i.addIndexes(s, rows); err != nil {
return err
}
return rows.Err()
}
// addIndexes scans the rows and adds the indexes to the table.
func (i *inspect) addIndexes(s *schema.Schema, rows *sql.Rows) error {
names := make(map[string]*schema.Index)
for rows.Next() {
var (
uniq, primary bool
table, name, typ string
desc, nullsfirst, nullslast sql.NullBool
column, contype, pred, expr, comment, options sql.NullString
)
if err := rows.Scan(&table, &name, &typ, &column, &primary, &uniq, &contype, &pred, &expr, &desc, &nullsfirst, &nullslast, &comment, &options); err != nil {
return fmt.Errorf("postgres: scanning indexes for schema %q: %w", s.Name, err)
}
t, ok := s.Table(table)
if !ok {
return fmt.Errorf("table %q was not found in schema", table)
}
idx, ok := names[name]
if !ok {
idx = &schema.Index{
Name: name,
Unique: uniq,
Table: t,
Attrs: []schema.Attr{
&IndexType{T: typ},
},
}
if sqlx.ValidString(comment) {
idx.Attrs = append(idx.Attrs, &schema.Comment{Text: comment.String})
}
if sqlx.ValidString(contype) {
idx.Attrs = append(idx.Attrs, &ConType{T: contype.String})
}
if sqlx.ValidString(pred) {
idx.Attrs = append(idx.Attrs, &IndexPredicate{P: pred.String})
}
if sqlx.ValidString(options) {
p, err := newIndexStorage(options.String)
if err != nil {
return err
}
idx.Attrs = append(idx.Attrs, p)
}
names[name] = idx
if primary {
t.PrimaryKey = idx
} else {
t.Indexes = append(t.Indexes, idx)
}
}
part := &schema.IndexPart{SeqNo: len(idx.Parts) + 1, Desc: desc.Bool}
if nullsfirst.Bool || nullslast.Bool {
part.Attrs = append(part.Attrs, &IndexColumnProperty{
NullsFirst: nullsfirst.Bool,
NullsLast: nullslast.Bool,
})
}
switch {
case sqlx.ValidString(column):
part.C, ok = t.Column(column.String)
if !ok {
return fmt.Errorf("postgres: column %q was not found for index %q", column.String, idx.Name)
}
part.C.Indexes = append(part.C.Indexes, idx)
case sqlx.ValidString(expr):
part.X = &schema.RawExpr{
X: expr.String,
}
default:
return fmt.Errorf("postgres: invalid part for index %q", idx.Name)
}
idx.Parts = append(idx.Parts, part)
}
return nil
}
// partitions builds the partition each table in the schema.
func (i *inspect) partitions(s *schema.Schema) error {
for _, t := range s.Tables {
var d Partition
if !sqlx.Has(t.Attrs, &d) {
continue
}
switch s := strings.ToLower(d.start); s {
case "r":
d.T = PartitionTypeRange
case "l":
d.T = PartitionTypeList
case "h":
d.T = PartitionTypeHash
default:
return fmt.Errorf("postgres: unexpected partition strategy %q", s)
}
idxs := strings.Split(strings.TrimSpace(d.attrs), " ")
if len(idxs) == 0 {
return fmt.Errorf("postgres: no columns/expressions were found in partition key for column %q", t.Name)
}
for i := range idxs {
switch idx, err := strconv.Atoi(idxs[i]); {
case err != nil:
return fmt.Errorf("postgres: faild parsing partition key index %q", idxs[i])
// An expression.
case idx == 0:
j := sqlx.ExprLastIndex(d.exprs)
if j == -1 {
return fmt.Errorf("postgres: no expression found in partition key: %q", d.exprs)
}
d.Parts = append(d.Parts, &PartitionPart{
X: &schema.RawExpr{X: d.exprs[:j+1]},
})
d.exprs = strings.TrimPrefix(d.exprs[j+1:], ", ")
// A column at index idx-1.
default:
if idx > len(t.Columns) {
return fmt.Errorf("postgres: unexpected column index %d", idx)
}
d.Parts = append(d.Parts, &PartitionPart{
C: t.Columns[idx-1],
})
}
}
schema.ReplaceOrAppend(&t.Attrs, &d)
}
return nil
}
// fks queries and appends the foreign keys of the given table.
func (i *inspect) fks(ctx context.Context, s *schema.Schema) error {
rows, err := i.querySchema(ctx, fksQuery, s)
if err != nil {
return fmt.Errorf("postgres: querying schema %q foreign keys: %w", s.Name, err)
}
defer rows.Close()
if err := sqlx.SchemaFKs(s, rows); err != nil {
return fmt.Errorf("postgres: %w", err)
}
return rows.Err()
}
// checks queries and appends the check constraints of the given table.
func (i *inspect) checks(ctx context.Context, s *schema.Schema) error {
rows, err := i.querySchema(ctx, checksQuery, s)
if err != nil {
return fmt.Errorf("postgres: querying schema %q check constraints: %w", s.Name, err)
}
defer rows.Close()
if err := i.addChecks(s, rows); err != nil {
return err
}
return rows.Err()
}
// addChecks scans the rows and adds the checks to the table.
func (i *inspect) addChecks(s *schema.Schema, rows *sql.Rows) error {
names := make(map[string]*schema.Check)
for rows.Next() {
var (
noInherit bool
table, name, column, clause, indexes string
)
if err := rows.Scan(&table, &name, &clause, &column, &indexes, &noInherit); err != nil {
return fmt.Errorf("postgres: scanning check: %w", err)
}
t, ok := s.Table(table)
if !ok {
return fmt.Errorf("table %q was not found in schema", table)
}
if _, ok := t.Column(column); !ok {
return fmt.Errorf("postgres: column %q was not found for check %q", column, name)
}
check, ok := names[name]
if !ok {
check = &schema.Check{Name: name, Expr: clause, Attrs: []schema.Attr{&CheckColumns{}}}
if noInherit {
check.Attrs = append(check.Attrs, &NoInherit{})
}
names[name] = check
t.Attrs = append(t.Attrs, check)
}
c := check.Attrs[0].(*CheckColumns)
c.Columns = append(c.Columns, column)
}
return nil
}
// schemas returns the list of the schemas in the database.
func (i *inspect) schemas(ctx context.Context, opts *schema.InspectRealmOption) ([]*schema.Schema, error) {
var (
args []interface{}
query = schemasQuery
)
if opts != nil {
switch n := len(opts.Schemas); {
case n == 1 && opts.Schemas[0] == "":
query = fmt.Sprintf(schemasQueryArgs, "= CURRENT_SCHEMA()")
case n == 1 && opts.Schemas[0] != "":
query = fmt.Sprintf(schemasQueryArgs, "= $1")
args = append(args, opts.Schemas[0])
case n > 0:
query = fmt.Sprintf(schemasQueryArgs, "IN ("+nArgs(0, len(opts.Schemas))+")")
for _, s := range opts.Schemas {
args = append(args, s)
}
}
}
rows, err := i.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("postgres: querying schemas: %w", err)
}
defer rows.Close()
var schemas []*schema.Schema
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
return nil, err
}
schemas = append(schemas, &schema.Schema{
Name: name,
})
}
if err := rows.Close(); err != nil {
return nil, err
}
return schemas, nil
}
func (i *inspect) querySchema(ctx context.Context, query string, s *schema.Schema) (*sql.Rows, error) {
args := []interface{}{s.Name}
for _, t := range s.Tables {
args = append(args, t.Name)
}
return i.QueryContext(ctx, fmt.Sprintf(query, nArgs(1, len(s.Tables))), args...)
}
func nArgs(start, n int) string {
var b strings.Builder
for i := 1; i <= n; i++ {
if i > 1 {
b.WriteString(", ")
}
b.WriteByte('$')
b.WriteString(strconv.Itoa(start + i))
}
return b.String()
}
func defaultExpr(c *schema.Column, x string) schema.Expr {
switch {
case sqlx.IsLiteralBool(x), sqlx.IsLiteralNumber(x), sqlx.IsQuoted(x, '\''):
return &schema.Literal{V: x}
default:
// Try casting or fallback to raw expressions (e.g. column text[] has the default of '{}':text[]).
if v, ok := canConvert(c.Type, x); ok {
return &schema.Literal{V: v}
}
return &schema.RawExpr{X: x}
}
}
func canConvert(t *schema.ColumnType, x string) (string, bool) {
r := t.Raw
if t, ok := t.Type.(*ArrayType); ok {
r = t.T
}
i := strings.Index(x, "::"+r)
if i == -1 || !sqlx.IsQuoted(x[:i], '\'') {
return "", false
}
q := x[0:i]
x = x[1 : i-1]
switch t.Type.(type) {
case *schema.BoolType:
if sqlx.IsLiteralBool(x) {
return x, true
}
case *schema.DecimalType, *schema.IntegerType, *schema.FloatType:
if sqlx.IsLiteralNumber(x) {
return x, true
}
case *ArrayType, *schema.BinaryType, *schema.JSONType, *NetworkType, *schema.SpatialType, *schema.StringType, *schema.TimeType, *UUIDType, *XMLType:
return q, true
}
return "", false
}
type (
// CType describes the character classification setting (LC_CTYPE).
CType struct {
schema.Attr
V string
}
// UserDefinedType defines a user-defined type attribute.
UserDefinedType struct {
schema.Type
T string
}
// enumType represents an enum type. It serves aa intermediate representation of a Postgres enum type,
// to temporary save TypeID and TypeName of an enum column until the enum values can be extracted.
enumType struct {
schema.Type
T string // Type name.
ID int64 // Type id.
Values []string
}
// ArrayType defines an array type.
// https://www.postgresql.org/docs/current/arrays.html
ArrayType struct {
schema.Type
T string
}
// BitType defines a bit type.
// https://www.postgresql.org/docs/current/datatype-bit.html
BitType struct {
schema.Type
T string
Len int64
}
// A NetworkType defines a network type.
// https://www.postgresql.org/docs/current/datatype-net-types.html
NetworkType struct {
schema.Type
T string
Len int64
}
// A CurrencyType defines a currency type.
CurrencyType struct {
schema.Type
T string
}
// A SerialType defines a serial type.
SerialType struct {
schema.Type
T string
Precision int
}
// A UUIDType defines a UUID type.
UUIDType struct {
schema.Type
T string
}
// A XMLType defines an XML type.
XMLType struct {
schema.Type
T string
}
// ConType describes constraint type.
// https://www.postgresql.org/docs/current/catalog-pg-constraint.html
ConType struct {
schema.Attr
T string // c, f, p, u, t, x.
}
// Sequence defines (the supported) sequence options.
// https://www.postgresql.org/docs/current/sql-createsequence.html
Sequence struct {
Start, Increment int64
}
// Identity defines an identity column.
Identity struct {
schema.Attr
Generation string // ALWAYS, BY DEFAULT.
Sequence *Sequence
}
// IndexType represents an index type.
// https://www.postgresql.org/docs/current/indexes-types.html
IndexType struct {
schema.Attr
T string // BTREE, BRIN, HASH, GiST, SP-GiST, GIN.
}
// IndexPredicate describes a partial index predicate.
// https://www.postgresql.org/docs/current/catalog-pg-index.html
IndexPredicate struct {
schema.Attr
P string
}
// IndexColumnProperty describes an index column property.
// https://www.postgresql.org/docs/current/functions-info.html#FUNCTIONS-INFO-INDEX-COLUMN-PROPS
IndexColumnProperty struct {
schema.Attr
// NullsFirst defaults to true for DESC indexes.
NullsFirst bool
// NullsLast defaults to true for ASC indexes.
NullsLast bool
}
// IndexStorageParams describes index storage parameters add with the WITH clause.
// https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
IndexStorageParams struct {
schema.Attr
// AutoSummarize defines the authsummarize storage parameter.
AutoSummarize bool
// PagesPerRange defines pages_per_range storage
// parameter for BRIN indexes. Defaults to 128.
PagesPerRange int64
}
// NoInherit attribute defines the NO INHERIT flag for CHECK constraint.
// https://www.postgresql.org/docs/current/catalog-pg-constraint.html
NoInherit struct {
schema.Attr
}
// CheckColumns attribute hold the column named used by the CHECK constraints.
// This attribute is added on inspection for internal usage and has no meaning
// on migration.
CheckColumns struct {
schema.Attr
Columns []string
}
// Partition defines the spec of a partitioned table.
Partition struct {
schema.Attr
// T defines the type/strategy of the partition.
// Can be one of: RANGE, LIST, HASH.
T string
// Partition parts. The additional attributes
// on each part can be used to control collation.
Parts []*PartitionPart
// Internal info returned from pg_partitioned_table.
start, attrs, exprs string
}
// An PartitionPart represents an index part that
// can be either an expression or a column.
PartitionPart struct {
X schema.Expr
C *schema.Column
Attrs []schema.Attr
}
)
// newIndexStorage parses and returns the index storage parameters.
func newIndexStorage(opts string) (*IndexStorageParams, error) {
params := &IndexStorageParams{}
for _, p := range strings.Split(strings.Trim(opts, "{}"), ",") {
kv := strings.Split(p, "=")
if len(kv) != 2 {
return nil, fmt.Errorf("invalid index storage parameter: %s", p)
}
switch kv[0] {
case "autosummarize":
b, err := strconv.ParseBool(kv[1])
if err != nil {
return nil, fmt.Errorf("failed parsing autosummarize %q: %w", kv[1], err)
}
params.AutoSummarize = b
case "pages_per_range":
i, err := strconv.ParseInt(kv[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed parsing pages_per_range %q: %w", kv[1], err)
}
params.PagesPerRange = i
}
}
return params, nil
}
const (
// Query to list runtime parameters.
paramsQuery = `SELECT setting FROM pg_settings WHERE name IN ('lc_collate', 'lc_ctype', 'server_version_num') ORDER BY name`
// Query to list database schemas.
schemasQuery = "SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema', 'pg_catalog', 'pg_toast') AND schema_name NOT LIKE 'pg_%temp_%' ORDER BY schema_name"
// Query to list specific database schemas.
schemasQueryArgs = "SELECT schema_name FROM information_schema.schemata WHERE schema_name %s ORDER BY schema_name"
// Query to list table information.
tablesQuery = `
SELECT
t1.table_schema,
t1.table_name,
pg_catalog.obj_description(t2.oid, 'pg_class') AS comment,
t3.partattrs AS partition_attrs,
t3.partstrat AS partition_strategy,
pg_get_expr(t3.partexprs, t3.partrelid) AS partition_exprs
FROM
INFORMATION_SCHEMA.TABLES AS t1
JOIN pg_catalog.pg_class AS t2 ON t2.oid = to_regclass(quote_ident(t1.table_schema) || '.' || quote_ident(t1.table_name))::oid
LEFT JOIN pg_catalog.pg_partitioned_table AS t3 ON t3.partrelid = t2.oid
WHERE
t1.table_type = 'BASE TABLE'
AND t1.table_schema IN (%s)
ORDER BY
t1.table_schema, t1.table_name
`
tablesQueryArgs = `
SELECT
t1.table_schema,
t1.table_name,
pg_catalog.obj_description(t2.oid, 'pg_class') AS comment,
t3.partattrs AS partition_attrs,
t3.partstrat AS partition_strategy,
pg_get_expr(t3.partexprs, t3.partrelid) AS partition_exprs
FROM
INFORMATION_SCHEMA.TABLES AS t1
JOIN pg_catalog.pg_class AS t2 ON t2.oid = to_regclass(quote_ident(t1.table_schema) || '.' || quote_ident(t1.table_name))::oid
LEFT JOIN pg_catalog.pg_partitioned_table AS t3 ON t3.partrelid = t2.oid
WHERE
t1.table_type = 'BASE TABLE'
AND t1.table_schema IN (%s)
AND t1.table_name IN (%s)
ORDER BY
t1.table_schema, t1.table_name
`
// Query to list table columns.
columnsQuery = `
SELECT
t1.table_name,
t1.column_name,
t1.data_type,
t1.is_nullable,
t1.column_default,
t1.character_maximum_length,
t1.numeric_precision,
t1.datetime_precision,
t1.numeric_scale,
t1.character_set_name,
t1.collation_name,
t1.udt_name,
t1.is_identity,
t1.identity_start,
t1.identity_increment,
t1.identity_generation,
t1.generation_expression,
col_description(to_regclass(quote_ident("table_schema") || '.' || quote_ident("table_name"))::oid, "ordinal_position") AS comment,
t2.typtype,
t2.oid
FROM
"information_schema"."columns" AS t1
LEFT JOIN pg_catalog.pg_type AS t2
ON t1.udt_name = t2.typname
WHERE
table_schema = $1 AND table_name IN (%s)
ORDER BY
t1.table_name, t1.ordinal_position
`
// Query to list table indexes.
indexesQuery = `
SELECT
t.relname AS table_name,
i.relname AS index_name,
am.amname AS index_type,
a.attname AS column_name,
idx.indisprimary AS primary,
idx.indisunique AS unique,
c.contype AS constraint_type,
pg_get_expr(idx.indpred, idx.indrelid) AS predicate,
pg_get_indexdef(idx.indexrelid, idx.ord, false) AS expression,
pg_index_column_has_property(idx.indexrelid, a.attnum, 'desc') AS desc,
pg_index_column_has_property(idx.indexrelid, a.attnum, 'nulls_first') AS nulls_first,
pg_index_column_has_property(idx.indexrelid, a.attnum, 'nulls_last') AS nulls_last,
obj_description(to_regclass($1 || i.relname)::oid) AS comment,
i.reloptions AS options
FROM
(
select
*,
generate_series(1,array_length(i.indkey,1)) as ord,
unnest(i.indkey) AS key
from pg_index i
) idx
JOIN pg_class i ON i.oid = idx.indexrelid
JOIN pg_class t ON t.oid = idx.indrelid
JOIN pg_namespace n ON n.oid = t.relnamespace
LEFT JOIN pg_constraint c ON idx.indexrelid = c.conindid
LEFT JOIN pg_attribute a ON (a.attrelid, a.attnum) = (idx.indrelid, idx.key)
JOIN pg_am am ON am.oid = i.relam
WHERE
n.nspname = $1
AND t.relname IN (%s)
AND COALESCE(c.contype, '') <> 'f'
ORDER BY
table_name, index_name, idx.ord
`
fksQuery = `
SELECT
t1.constraint_name,
t1.table_name,
t2.column_name,
t1.table_schema,
t3.table_name AS referenced_table_name,
t3.column_name AS referenced_column_name,
t3.table_schema AS referenced_schema_name,
t4.update_rule,
t4.delete_rule
FROM
information_schema.table_constraints t1
JOIN information_schema.key_column_usage t2
ON t1.constraint_name = t2.constraint_name
AND t1.table_schema = t2.constraint_schema
JOIN information_schema.constraint_column_usage t3
ON t1.constraint_name = t3.constraint_name
AND t1.table_schema = t3.constraint_schema
JOIN information_schema.referential_constraints t4
ON t1.constraint_name = t4.constraint_name
AND t1.table_schema = t4.constraint_schema
WHERE
t1.constraint_type = 'FOREIGN KEY'
AND t1.table_schema = $1
AND t1.table_name IN (%s)
ORDER BY
t1.constraint_name,
t2.ordinal_position
`
// Query to list table check constraints.
checksQuery = `
SELECT
rel.relname AS table_name,
t1.conname AS constraint_name,
pg_get_expr(t1.conbin, to_regclass(quote_ident(nsp.nspname) || '.' || quote_ident(rel.relname))::oid) as expression,
t2.attname as column_name,
t1.conkey as column_indexes,
t1.connoinherit as no_inherit
FROM
pg_constraint t1
JOIN pg_attribute t2
ON t2.attrelid = t1.conrelid
AND t2.attnum = ANY (t1.conkey)
JOIN pg_class rel
ON rel.oid = t1.conrelid
JOIN pg_namespace nsp
ON nsp.oid = t1.connamespace
WHERE
t1.contype = 'c'
AND nsp.nspname = $1
AND rel.relname IN (%s)
ORDER BY
t1.conname, array_position(t1.conkey, t2.attnum)
`
)