-
Notifications
You must be signed in to change notification settings - Fork 4
/
db_information.go
881 lines (742 loc) · 26.2 KB
/
db_information.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
package pg
import (
"context"
"database/sql"
"fmt"
"reflect"
"sort"
"strings"
"github.com/kataras/pg/desc"
)
// CreateSchema creates the database schema by executing a series of SQL commands in a transaction.
func (db *DB) CreateSchema(ctx context.Context) error {
createDumpSQL, err := db.CreateSchemaDumpSQL(ctx)
if err != nil {
return err
}
return db.InTransaction(ctx, func(db *DB) error {
_, err = db.Exec(ctx, createDumpSQL)
if err != nil {
return fmt.Errorf("%w:\n%s", err, createDumpSQL)
}
return nil
})
}
type sqlDumperFunc func(context.Context, *strings.Builder) error
// CreateSchemaDumpSQL dumps the SQL commands for creating the database schema.
func (db *DB) CreateSchemaDumpSQL(ctx context.Context) (string, error) {
var dumpers = []sqlDumperFunc{
db.createDatabaseSchemaDump,
db.createExtensionsDump,
db.createTablesDump,
db.createFunctionsAndTriggersDump,
}
b := new(strings.Builder)
for _, d := range dumpers {
if err := d(ctx, b); err != nil {
return "", err
}
}
return b.String(), nil
}
// createDatabaseSchema creates the database schema.
func (db *DB) createDatabaseSchemaDump(_ context.Context, b *strings.Builder) error {
query := `CREATE SCHEMA IF NOT EXISTS ` + db.searchPath + `;`
b.WriteString(query)
return nil
}
// createExtensions creates the necessary PostgreSQL extensions for the database schema.
func (db *DB) createExtensionsDump(_ context.Context, b *strings.Builder) error {
if db.schema.HasColumnType(desc.UUID) || db.schema.HasPassword() {
query := `CREATE EXTENSION IF NOT EXISTS pgcrypto;`
b.WriteString(query)
}
if db.schema.HasColumnType(desc.CIText) {
query := `CREATE EXTENSION IF NOT EXISTS citext;`
b.WriteString(query)
}
if db.schema.HasColumnType(desc.HStore) {
query := `CREATE EXTENSION IF NOT EXISTS hstore;`
b.WriteString(query)
}
return nil
}
// createTables creates the tables for the database schema.
func (db *DB) createTablesDump(ctx context.Context, b *strings.Builder) error {
tables := db.schema.Tables(desc.TableTypeBase)
if len(tables) == 0 {
return nil // if no tables are defined, there is nothing to create.
}
for _, td := range tables {
if err := db.createTableDump(ctx, b, td); err != nil {
return fmt.Errorf("%s: %w", td.Name, err)
}
}
// 2nd loop, so order doesn't matter.
for _, td := range tables {
if err := db.createTableForeignKeysDump(ctx, b, td); err != nil {
return fmt.Errorf("%s: foreign keys: %w", td.Name, err)
}
}
return nil
}
// createTable creates a table in the database according to the given table definition.
func (db *DB) createTableDump(_ context.Context, b *strings.Builder, td *desc.Table) error {
if td.IsReadOnly() {
return nil
}
query := desc.BuildCreateTableQuery(td)
b.WriteString(query)
return nil
}
// createTableForeignKeys creates the foreign keys for the given table definition.
func (db *DB) createTableForeignKeysDump(_ context.Context, b *strings.Builder, td *desc.Table) error {
if td.IsReadOnly() {
return nil
}
queries := desc.BuildAlterTableForeignKeysQueries(td) // these run on transaction on the caller level.
for _, query := range queries {
b.WriteString(query)
}
return nil
}
// createFunctionsAndTriggers creates the functions and triggers for the database schema.
func (db *DB) createFunctionsAndTriggersDump(ctx context.Context, b *strings.Builder) error {
if db.schema.SetTimestampTriggerName == "" || db.schema.UpdatedAtColumnName == "" {
// Do not register triggers if the end-developer disabled this feature by
// setting these fields to empty.
return nil
}
var (
createSetTimestampFunctionQuery = fmt.Sprintf(`CREATE OR REPLACE FUNCTION trigger_%s()
RETURNS TRIGGER AS $$
BEGIN
NEW.%s = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;`, db.schema.SetTimestampTriggerName, db.schema.UpdatedAtColumnName)
createSetTimestampTriggerQueryTmpl = `CREATE TRIGGER %s
BEFORE UPDATE ON %s
FOR EACH ROW
EXECUTE PROCEDURE trigger_%s();`
)
var (
setTimestampFunctionCreated bool
)
triggers, err := db.ListTriggers(ctx)
if err != nil {
return fmt.Errorf("list triggers: %w", err)
}
tables := db.schema.Tables(desc.TableTypeBase)
tablesLoop:
for _, td := range tables {
if td.IsReadOnly() {
continue
}
var setTimestampTriggerCreated bool
for _, trigger := range triggers {
if trigger.Name == db.schema.SetTimestampTriggerName && trigger.TableName == td.Name {
setTimestampTriggerCreated = true
continue tablesLoop
}
}
for _, column := range td.Columns {
if !setTimestampTriggerCreated {
if column.Name == db.schema.UpdatedAtColumnName && column.Type.IsTime() {
if !setTimestampFunctionCreated { // global function.
b.WriteString(createSetTimestampFunctionQuery)
setTimestampFunctionCreated = true
}
// Create the trigger for each table.
query := fmt.Sprintf(createSetTimestampTriggerQueryTmpl, db.schema.SetTimestampTriggerName, td.Name, db.schema.SetTimestampTriggerName)
b.WriteString(query)
setTimestampTriggerCreated = true
continue
}
}
}
}
return nil
}
// CheckSchema checks if the database schema matches the expected table definitions by querying the information schema and
// comparing the results.
func (db *DB) CheckSchema(ctx context.Context) error {
tableNames := db.schema.TableNames(desc.DatabaseTableTypes...)
if len(tableNames) == 0 {
return nil // if no tables are defined, there is nothing to check.
}
tables, err := db.ListTables(ctx, ListTablesOptions{
TableNames: tableNames,
})
if err != nil {
return err
}
if len(tables) != len(tableNames) {
return fmt.Errorf("expected %d tables, got %d", len(tableNames), len(tables))
}
// var fixQueries []string
for _, table := range tables {
tableName := table.Name
td, err := db.schema.GetByTableName(tableName)
if err != nil {
return err // this should never happen as we get the table names from the schema.
}
if td.Description == "" {
td.Description = table.Description
}
for _, col := range table.Columns {
column := td.GetColumnByName(col.Name) // get code column.
if column == nil {
return fmt.Errorf("column %q in table %q not found in schema", col.Name, tableName)
}
// if column.Unique { // modify it, so checks are correct.
// column.UniqueIndex = fmt.Sprintf("%s_%s_key", tableName, column.Name)
// column.Unique = false
// }
if expected, got := strings.ToLower(col.FieldTagString(false)), strings.ToLower(column.FieldTagString(false)); expected != got {
// if strings.Contains(expected, "nullable") && !strings.Contains(got, "nullable") {
// // database has nullable, but code doesn't.
// fixQuery := fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s SET NOT NULL;`, tableName, col.Name)
// fixQueries = append(fixQueries, fixQuery)
// } else {
return fmt.Errorf("column %q in table %q has wrong field tag: db:\n%s\nvs code:\n%s", col.Name, tableName, expected, got)
// }
}
if column.Description == "" {
column.Description = col.Description
}
}
}
// Maybe a next feature but we must be very careful, skip it for now and ofc move it to a different developer-driven method:
// if len(fixQueries) > 0 {
// return db.InTransaction(ctx, func(db *DB) error {
// for _, fixQuery := range fixQueries {
// // fmt.Println(fixQuery)
// _, err = db.Exec(ctx, fixQuery)
// if err != nil {
// return err
// }
// }
// return nil
// })
// }
return nil // return nil if no mismatch is found
}
// DeleteSchema drops the database schema.
func (db *DB) DeleteSchema(ctx context.Context) error {
query := `DROP SCHEMA IF EXISTS "` + db.searchPath + `" CASCADE;`
_, err := db.Exec(ctx, query)
return err
}
// IsAutoVacuumEnabled returns true if autovacuum is enabled for the database.
//
// Read more about autovacuum at: https://www.postgresql.org/docs/current/runtime-config-autovacuum.html.
func (db *DB) IsAutoVacuumEnabled(ctx context.Context) (enabled bool, err error) {
query := `SHOW autovacuum;`
err = db.QueryRow(ctx, query).Scan(&enabled)
return
}
// DisableAutoVacuum disables autovacuum for the whole database.
func (db *DB) DisableAutoVacuum(ctx context.Context) error {
query := `ALTER DATABASE "` + db.ConnectionOptions.Database + `" SET autovacuum = off;`
_, err := db.Exec(ctx, query)
return err
}
// DisableTableAutoVacuum disables autovacuum for a specific table.
func (db *DB) DisableTableAutoVacuum(ctx context.Context, tableName string) error {
query := `ALTER TABLE "` + tableName + `" SET (autovacuum_enabled = false);`
_, err := db.Exec(ctx, query)
return err
}
// GetVersion returns the version number of the PostgreSQL database as a string.
func (db *DB) GetVersion(ctx context.Context) (string, error) {
query := `SELECT version();`
var version string
// Query the database to retrieve the version string
err := db.QueryRow(ctx, query).Scan(&version)
if err != nil {
return "", err // return an empty string and the error if the query fails
}
// Parse the version string to extract the version number
start := strings.Index(version, "PostgreSQL ")
if start == -1 {
// return an error if the version string does not contain "PostgreSQL"
return "", fmt.Errorf("could not find PostgreSQL version in version string: %s", version)
}
start += len("PostgreSQL ") // move the start index to the beginning of the version number
end := strings.Index(version[start:], " ")
if end == -1 {
// return an error if the version string does not have a space after the version number
return "", fmt.Errorf("could not find end of version number in version string: %s", version)
}
end += start // move the end index to the end of the version number
versionNumber := version[start : end-1] // -1 to remove the trailing comma. Slice the version string to get only the version number
versionNumber = strings.TrimSuffix(versionNumber, ".")
return versionNumber, nil // return the version number and nil as no error occurred
}
// SizeInfo is a struct which contains the size information (for individual table or the whole database).
type SizeInfo struct {
SizePretty string `json:"size_pretty"`
// The on-disk size in bytes of one fork of that relation.
// A fork is a variant of the main data file that stores additional information,
// such as the free space map, the visibility map, or the initialization fork.
// By default, this is the size of the main data fork only.
Size float64 `json:"size"`
SizeTotalPretty string `json:"size_total_pretty"`
// The total on-disk space used for that table, including all associated indexes. This is equivalent to pg_table_size + pg_indexes_size.
SizeTotal float64 `json:"size_total"`
}
// TableSizeInfo is a struct which contains the table size information used as an output parameter of the `db.ListTableSizes` method.
type TableSizeInfo struct {
TableName string `json:"table_name"`
SizeInfo
}
// ListTableSizes lists the disk size of tables (not only the registered ones) in the database.
func (db *DB) ListTableSizes(ctx context.Context) ([]TableSizeInfo, error) {
query := `SELECT
table_name,
pg_size_pretty(pg_relation_size(quote_ident(table_name))) AS size_pretty,
pg_relation_size(quote_ident(table_name)) AS size,
pg_size_pretty(pg_total_relation_size(quote_ident(table_name))) AS size_total_pretty,
pg_total_relation_size(quote_ident(table_name)) AS size_total
FROM information_schema.tables
WHERE table_schema = $1
ORDER BY 3 DESC;`
return scanQuery[TableSizeInfo](ctx, db, func(rows Rows) (t TableSizeInfo, err error) {
err = rows.Scan(&t.TableName, &t.SizePretty, &t.Size, &t.SizeTotalPretty, &t.SizeTotal)
return
}, query, db.searchPath)
}
// GetSize returns the sum of size of all the database tables.
func (db *DB) GetSize(ctx context.Context) (t SizeInfo, err error) {
query := `SELECT
pg_size_pretty(SUM(size)) AS size_pretty,
SUM(size) AS size, pg_size_pretty(SUM(size_total)) AS size_total_pretty,
SUM(size_total) AS size_total
FROM (
SELECT
table_name,
pg_relation_size(quote_ident(table_name)) AS size,
pg_total_relation_size(quote_ident(table_name)) AS size_total
FROM information_schema.tables
WHERE table_schema = $1
ORDER BY 1 DESC
) f;`
err = db.QueryRow(ctx, query, db.searchPath).Scan(&t.SizePretty, &t.Size, &t.SizeTotalPretty, &t.SizeTotal)
return
}
// MapTypeFilter is a map of expressions inputs text to field type.
// It's a TableFilter.
//
// Example on LsitTableOptions of the ListTables method:
//
// Filter: pg.MapTypeFilter{
// "customer_profiles.fields.jsonb": entity.Fields{},
// },
type MapTypeFilter map[string]any
var _ desc.TableFilter = MapTypeFilter{}
// FilterTable implements the TableFilter interface.
func (m MapTypeFilter) FilterTable(t *Table) bool {
expressions := make(desc.Expressions, 0, len(m))
for k, v := range m {
expressions = append(expressions, desc.NewExpression(k, reflect.TypeOf(v)))
}
return expressions.FilterTable(t)
}
// ListTableOptions are the options for listing tables.
type ListTablesOptions struct {
TableNames []string
Filter desc.TableFilter // Filter allows to customize the StructName and its Column field types.
}
// ListTables returns a list of converted table definitions from the remote database schema.
func (db *DB) ListTables(ctx context.Context, opts ListTablesOptions) ([]*desc.Table, error) {
columns, err := db.ListColumns(ctx, opts.TableNames...)
if err != nil {
return nil, err
}
var (
tableNamesOrdered = make([]string, 0)
tableDescriptionsOrdered = make([]string, 0)
tableTypesOrdered = make([]desc.TableType, 0)
tableColumnsMap = make(map[string][]*desc.Column)
)
for _, column := range columns {
t, ok := tableColumnsMap[column.TableName]
if !ok {
tableNamesOrdered = append(tableNamesOrdered, column.TableName)
tableDescriptionsOrdered = append(tableDescriptionsOrdered, column.TableDescription)
tableTypesOrdered = append(tableTypesOrdered, column.TableType)
}
tableColumnsMap[column.TableName] = append(t, column)
}
tables := make([]*desc.Table, 0, len(tableNamesOrdered))
for i, tableName := range tableNamesOrdered {
columns := tableColumnsMap[tableName]
table := &desc.Table{
RegisteredPosition: i,
StructName: desc.ToStructName(tableName),
StructType: nil,
SearchPath: db.searchPath,
Name: tableName,
Description: tableDescriptionsOrdered[i],
Type: tableTypesOrdered[i],
}
table.AddColumns(columns...)
filter := opts.Filter
if filter != nil {
if ok := filter.FilterTable(table); !ok {
continue // skip this table.
}
}
tables = append(tables, table)
}
// Sort so "parent" tables are going first to the list.
sort.SliceStable(tables, func(i, j int) bool {
tb1 := tables[i]
tb2 := tables[j]
if tb2.IsReadOnly() {
return true // tb1 comes first.
}
if tb1.IsReadOnly() {
return false
}
return !strings.Contains(tb1.Name, "_")
})
return tables, nil
}
// ListColumns returns a list of columns definitions for the given table names.
func (db *DB) ListColumns(ctx context.Context, tableNames ...string) ([]*desc.Column, error) {
basicInfos, err := db.ListColumnsInformationSchema(ctx, tableNames...)
if err != nil {
return nil, err
}
constraints, err := db.ListConstraints(ctx, tableNames...)
if err != nil {
return nil, err
}
uniqueIndexes, err := db.ListUniqueIndexes(ctx, tableNames...)
if err != nil {
return nil, err
}
columns := make([]*desc.Column, 0, len(basicInfos))
for _, basicInfo := range basicInfos {
var column desc.Column
basicInfo.BuildColumn(&column)
for _, constraint := range constraints {
if constraint.TableName == column.TableName && constraint.ColumnName == column.Name {
constraint.BuildColumn(&column)
}
}
uniqueIndexLoop:
for _, uniqueIndex := range uniqueIndexes {
if uniqueIndex.TableName == column.TableName {
for _, columnName := range uniqueIndex.Columns {
if columnName == column.Name {
column.Unique = false
column.UniqueIndex = uniqueIndex.IndexName
break uniqueIndexLoop
}
}
}
}
// No need to put index types on these type of columns, postgres manages these.
if column.PrimaryKey || column.Unique || column.UniqueIndex != "" {
column.Index = desc.InvalidIndex
}
columns = append(columns, &column)
}
return columns, nil
}
// ListConstraints returns a list of constraint definitions in the database schema by querying the pg_constraint table and.
func (db *DB) ListConstraints(ctx context.Context, tableNames ...string) ([]*desc.Constraint, error) {
if tableNames == nil {
tableNames = make([]string, 0)
}
query := `SELECT
cl.relname AS table_name,
a.attname AS column_name,
con.conname AS constraint_name,
con.contype AS constraint_type,
pg_get_constraintdef(con.oid) AS constraint_definition,
COALESCE(am.amname, '') AS index_type
FROM
pg_catalog.pg_class cl
JOIN
pg_catalog.pg_namespace n ON n.oid = cl.relnamespace
JOIN
pg_catalog.pg_attribute a ON a.attrelid = cl.oid
JOIN
pg_catalog.pg_constraint con ON con.conrelid = cl.oid AND a.attnum = ANY (con.conkey)
LEFT JOIN
pg_catalog.pg_index idx ON idx.indrelid = cl.oid AND idx.indexrelid = con.conindid
LEFT JOIN
pg_catalog.pg_class i ON i.oid = idx.indexrelid
LEFT JOIN
pg_catalog.pg_am am ON am.oid = i.relam
WHERE
n.nspname = $1 AND
( CARDINALITY($2::varchar[]) = 0 OR cl.relname = ANY($2::varchar[]) )
-- ORDER BY
-- cl.relname,
-- a.attnum
UNION ALL
SELECT
tablename AS table_name,
'' AS column_name, -- retrieved by definition
indexname AS constraint_name,
'i' AS constraint_type,
indexdef AS constraint_definition,
'' AS index_type -- retrieved by definition
FROM
pg_indexes i
WHERE
schemaname = $1 AND
( CARDINALITY($2::varchar[]) = 0 OR tablename = ANY($2::varchar[]) ) AND
indexdef NOT LIKE '%UNIQUE%' -- don't collect unique indexes here, they are (or should be) collected in the first part of the query OR by the ListUniqueIndexes.
ORDER BY table_name, column_name;`
/*
table_name column_name constraint_name constraint_type constraint_definition index_type
blog_posts blog_posts_blog_id_fkey i CREATE INDEX blog_posts_blog_id_fkey ON public.blog_posts USING btree (blog_id)
blog_posts blog_id blog_posts_blog_id_fkey f FOREIGN KEY (blog_id) REFERENCES blogs(id) ON DELETE CASCADE DEFERRABLE
blog_posts id blog_posts_pkey p PRIMARY KEY (id) btree
blog_posts read_time_minutes blog_posts_read_time_minutes_check c CHECK ((read_time_minutes > 0))
blog_posts source_url uk_blog_post u UNIQUE (title, source_url) btree
blog_posts title uk_blog_post u UNIQUE (title, source_url) btree
blogs id blogs_pkey p PRIMARY KEY (id) btree
customers customers_name_idx i CREATE INDEX customers_name_idx ON public.customers USING btree (name)
customers cognito_user_id customer_unique_idx u UNIQUE (cognito_user_id, email) btree
customers email customer_unique_idx u UNIQUE (cognito_user_id, email) btree
customers id customers_pkey p PRIMARY KEY (id) btree
*/
// Execute the query using db.Query and pass in the search path as a parameter
rows, err := db.Query(ctx, query, db.searchPath, tableNames)
if err != nil {
return nil, err // return nil and the error if the query fails
}
defer rows.Close() // close the rows instance when done
// constraintNames := make(map[string]struct{})
cs := make([]*desc.Constraint, 0) // create an empty slice to store the constraint definitions
for rows.Next() { // loop over the rows returned by the query
var (
c desc.Constraint
constraintDefinition string
)
if err = rows.Scan(
&c.TableName,
&c.ColumnName,
&c.ConstraintName,
&c.ConstraintType,
&constraintDefinition,
&c.IndexType,
); err != nil {
return nil, err
}
// if _, exists := constraintNames[c.ConstraintName]; !exists {
// constraintNames[c.ConstraintName] = struct{}{}
c.Build(constraintDefinition)
cs = append(cs, &c)
// }
}
if err = rows.Err(); err != nil {
return nil, err
}
return cs, nil
}
// ListUniqueIndexes returns a list of unique indexes in the database schema by querying the pg_index table and
// filtering the results to only include unique indexes.
func (db *DB) ListUniqueIndexes(ctx context.Context, tableNames ...string) ([]*desc.UniqueIndex, error) {
if tableNames == nil {
tableNames = make([]string, 0)
}
query := `SELECT
-- n.nspname AS schema_name,
t.relname AS table_name,
i.relname AS index_name,
array_agg(a.attname ORDER BY a.attnum) AS index_columns
FROM pg_index p
JOIN pg_class t ON t.oid = p.indrelid -- the table
JOIN pg_class i ON i.oid = p.indexrelid -- the index
JOIN pg_namespace n ON n.oid = t.relnamespace -- the schema
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(p.indkey) -- the columns
WHERE n.nspname = $1
AND ( CARDINALITY($2::varchar[]) = 0 OR t.relname = ANY($2::varchar[]) )
AND p.indisunique -- only unique indexes
AND NOT p.indisprimary -- not primary keys
AND NOT EXISTS ( -- not created by a constraint
SELECT 1 FROM pg_constraint c
WHERE c.conindid = p.indexrelid
)
GROUP BY n.nspname, t.relname, i.relname;`
/*
public customer_allergies customer_allergy {customer_id,allergy_id}
public customer_cheat_foods customer_cheat_food {customer_id,food_id}
public customer_devices customer_devices_unique {customer_id,type}
*/
// Execute the query using db.Query and pass in the search path as a parameter
rows, err := db.Query(ctx, query, db.searchPath, tableNames)
if err != nil {
return nil, err // return nil and the error if the query fails
}
defer rows.Close() // close the rows instance when done
cs := make([]*desc.UniqueIndex, 0) // create an empty slice to store the unique index definitions
for rows.Next() { // loop over the rows returned by the query
var (
tableName string
indexName string
columns []string
)
if err = rows.Scan(
&tableName,
&indexName,
&columns,
); err != nil {
return nil, err
}
c := desc.UniqueIndex{
TableName: tableName,
IndexName: indexName,
Columns: columns,
}
cs = append(cs, &c)
}
if err = rows.Err(); err != nil {
return nil, err
}
return cs, nil
}
// ListTriggers returns a list of triggers in the database for a given set of tables
// The method takes a context and returns a slice of Trigger pointers, and an error if any.
func (db *DB) ListTriggers(ctx context.Context) ([]*desc.Trigger, error) {
query := `SELECT
event_object_catalog,
event_object_schema,
trigger_name,
event_manipulation,
event_object_table,
action_statement,
action_orientation,
action_timing FROM information_schema.triggers
WHERE event_object_catalog = $1 AND event_object_table = ANY($2) ORDER BY event_object_table;`
rows, err := db.Query(ctx, query, db.ConnectionOptions.Config.Database, db.schema.TableNames())
if err != nil {
return nil, err
}
defer rows.Close()
triggers := make([]*desc.Trigger, 0)
for rows.Next() {
var trigger desc.Trigger
err = rows.Scan(
&trigger.Catalog,
&trigger.SearchPath,
&trigger.Name,
&trigger.Manipulation,
&trigger.TableName,
&trigger.ActionStatement,
&trigger.ActionOrientation,
&trigger.ActionTiming,
)
if err != nil {
return nil, err
}
triggers = append(triggers, &trigger)
}
if err := rows.Err(); err != nil {
return nil, err
}
return triggers, nil
}
// ListColumnsInformationSchema returns a list of basic columns information for the given table names or all.
func (db *DB) ListColumnsInformationSchema(ctx context.Context, tableNames ...string) ([]*desc.ColumnBasicInfo, error) {
if tableNames == nil {
tableNames = make([]string, 0)
}
columns := make([]*desc.ColumnBasicInfo, 0)
query := `SELECT
c.table_name,
obj_description(p.attrelid::regclass) as table_description,
t.table_type,
c.column_name,
c.ordinal_position,
col_description(p.attrelid::regclass, p.attnum) as column_description,
c.column_default,
pg_catalog.format_type(p.atttypid, p.atttypmod) AS data_type,
CASE WHEN c.is_nullable = 'YES' THEN true ELSE false END AS is_nullable,
CASE WHEN c.is_identity = 'YES' THEN true ELSE false END AS is_identity,
CASE WHEN c.is_generated = 'ALWAYS' THEN true ELSE false END AS is_generated
FROM information_schema.columns c
JOIN information_schema.tables t ON t.table_catalog = c.table_catalog AND t.table_schema = c.table_schema AND t.table_name = c.table_name
JOIN pg_catalog.pg_attribute p ON p.attrelid = (c.table_schema || '.' || c.table_name)::regclass AND p.attname = c.column_name
WHERE
c.table_catalog = $1 AND
c.table_schema = $2 AND
( CARDINALITY($3::varchar[]) = 0 OR c.table_name = ANY($3::varchar[]) )
ORDER BY table_name, ordinal_position;`
rows, err := db.Query(ctx, query, db.ConnectionOptions.Database, db.searchPath, tableNames)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var (
tableName string
tableDescription sql.NullString
fullTableType string
columnName string
ordinalPosition int // for FieldIndex and OrdinalPosition.
columnDescription sql.NullString
columnDefault sql.NullString
fullDataType string
isNullable bool
isIdentity bool
isGenerated bool
)
if err := rows.Scan(
&tableName,
&tableDescription,
&fullTableType,
&columnName,
&ordinalPosition,
&columnDescription,
&columnDefault,
&fullDataType,
&isNullable,
&isIdentity,
&isGenerated,
); err != nil {
return nil, err
}
tableDesc := tableDescription.String
if tableDesc != "" {
if tableDesc[len(tableDesc)-1] != '.' {
tableDesc += "."
}
}
columnDesc := columnDescription.String
if columnDesc != "" {
if columnDesc[len(columnDesc)-1] != '.' {
columnDesc += "."
}
}
tableType := desc.ParseTableType(fullTableType)
dataType, dataTypeArgument := desc.ParseDataType(fullDataType)
column := &desc.ColumnBasicInfo{
TableName: tableName,
TableDescription: tableDesc,
TableType: tableType,
Name: columnName,
OrdinalPosition: ordinalPosition,
Description: columnDesc,
Default: columnDefault.String,
DataType: dataType,
DataTypeArgument: dataTypeArgument,
IsNullable: isNullable,
IsIdentity: isIdentity,
IsGenerated: isGenerated,
}
columns = append(columns, column)
}
if err = rows.Err(); err != nil {
return nil, err
}
return columns, nil
}