@@ -4,62 +4,79 @@ import (
44 "context"
55 "github.com/dipdup-net/go-lib/hasura"
66 "github.com/go-pg/pg/v10"
7- "github.com/pkg/errors"
87 "reflect"
98 "strings"
109)
1110
1211func makeComments (ctx context.Context , conn PgGoConnection , model interface {}) error {
13- typ := reflect .TypeOf (model )
14-
15- // 1. go through fields
16- // 2. if tableName field -
17- // 2.1 read value from pg tab, if not exist from model type name with snake case convertion - remember as tableName
18- // 2.2 read value from pg-comment tag, if not exist continue
19- // 2.3 set comment with SQL statement
20- // 3. other
21- // 3.1 read comment from pg-comment, it not exist continue
22- // 3.2 read pg tag first value if it exist, if not - then snake case name of field and set as columnName
23- // 3.3 set comment with SQL statement
24- for i := 0 ; i < typ .NumField (); i ++ {
25- fieldType := typ .Field (i )
26- pgTag , ok := fieldType .Tag .Lookup ("pg" )
27- if ! ok {
28- continue
29- }
12+ modelType := reflect .TypeOf (model )
13+ var tableName pg.Safe
3014
31- tags := strings .Split (pgTag , "," )
15+ for i := 0 ; i < modelType .NumField (); i ++ {
16+ fieldType := modelType .Field (i )
3217
33- var name string
34- for i := range tags {
35- if i == 0 {
36- if name == "" {
37- name = hasura .ToSnakeCase (fieldType .Name )
38- } else {
39- name = tags [i ]
40- }
41- continue
18+ if fieldType .Name == "tableName" {
19+
20+ readFromModelName := true
21+ tableName , readFromModelName = getPgName (fieldType )
22+ if readFromModelName {
23+ tableName = pg .Safe (hasura .ToSnakeCase (modelType .Name ()))
4224 }
4325
44- parts := strings .Split (tags [i ], ":" )
45- if parts [0 ] == "comment" {
46- if len (parts ) != 2 {
47- return errors .Errorf ("invalid comments format: %s" , pgTag )
48- }
49- if fieldType .Name == "tableName" {
50- // typ.Name() to
51- if _ , err := conn .DB ().ExecContext (ctx , `COMMENT ON TABLE ? IS ?` , pg .Safe (typ .Name ()), parts [1 ]); err != nil {
52- return err
53- }
54- } else {
55- if _ , err := conn .DB ().ExecContext (ctx , `COMMENT ON COLUMN ?.? IS ?` , pg .Safe (typ .Name ()), pg .Safe (name ), parts [1 ]); err != nil {
56- return err
57- }
58- }
26+ pgCommentTag , ok := getPgComment (fieldType )
27+ if ! ok {
5928 continue
6029 }
30+
31+ if _ , err := conn .DB ().ExecContext (ctx ,
32+ `COMMENT ON TABLE ? IS ?` ,
33+ tableName , pgCommentTag ); err != nil {
34+ return err
35+ }
36+
37+ continue
38+ }
39+
40+ pgCommentTag , ok := getPgComment (fieldType )
41+ if ! ok {
42+ continue
43+ }
44+
45+ columnName , readFromFieldName := getPgName (fieldType )
46+ if readFromFieldName {
47+ columnName = pg .Safe (hasura .ToSnakeCase (fieldType .Name ))
48+ }
49+
50+ if _ , err := conn .DB ().ExecContext (ctx ,
51+ `COMMENT ON COLUMN ?.? IS ?` ,
52+ tableName , columnName , pgCommentTag ); err != nil {
53+ return err
6154 }
6255 }
6356
6457 return nil
6558}
59+
60+ func getPgName (fieldType reflect.StructField ) (name pg.Safe , ok bool ) {
61+ pgTag , ok := fieldType .Tag .Lookup ("pg" )
62+ if ok {
63+ tags := strings .Split (pgTag , "," )
64+
65+ if tags [0 ] != "" {
66+ name = pg .Safe (tags [0 ])
67+ ok = false
68+ }
69+ }
70+
71+ return name , ok
72+ }
73+
74+ func getPgComment (fieldType reflect.StructField ) (pg.Safe , bool ) {
75+ pgCommentTag , ok := fieldType .Tag .Lookup ("pg-comment" )
76+
77+ if ok {
78+ return pg .Safe (pgCommentTag ), ok
79+ }
80+
81+ return "" , false
82+ }
0 commit comments