Skip to content

Commit 365b473

Browse files
authored
Merge pull request #14 from dipdup-io/GO-80-add-pg-comment-tag
GO 80 add pg comment tag
2 parents 8ddb8c6 + 8b02566 commit 365b473

File tree

5 files changed

+137
-211
lines changed

5 files changed

+137
-211
lines changed

database/pg.go

Lines changed: 25 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package database
22

3+
//go:generate mockgen -destination ../mocks/mock_SchemeCommenter.go -package mocks github.com/dipdup-net/go-lib/database SchemeCommenter
4+
35
import (
46
"context"
57
"fmt"
@@ -8,68 +10,9 @@ import (
810
"github.com/pkg/errors"
911
)
1012

11-
type PgGoConnection interface {
12-
DB() PgDB
13-
}
14-
15-
type PgDB interface {
16-
/*
17-
Begin() (*pg.Tx, error)
18-
BeginContext(ctx context.Context) (*pg.Tx, error)
19-
RunInTransaction(ctx context.Context, fn func(*pg.Tx) error) error
20-
AddQueryHook(hook pg.QueryHook)
21-
beforeQuery(ctx context.Context, ormDB orm.DB, model interface{}, query interface{}, params []interface{}, fmtedQuery []byte) (context.Context, *QueryEvent, error)
22-
afterQuery(ctx context.Context, event *pg.QueryEvent, res pg.Result, err error) error
23-
afterQueryFromIndex(ctx context.Context, event *pg.QueryEvent, hookIndex int) error
24-
//startup(c context.Context, cn *pool.Conn, user string, password string, database string, appName string) error
25-
//enableSSL(c context.Context, cn *pool.Conn, tlsConf *tls.Config) error
26-
//auth(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, user string, password string) error
27-
//logStartupNotice(rd *pool.ReaderContext) error
28-
//authCleartext(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, password string) error
29-
//authMD5(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, user string, password string) error
30-
//authSASL(c context.Context, cn *pool.Conn, rd *pool.ReaderContext, user string, password string) error
31-
PoolStats() *pg.PoolStats
32-
//clone() *baseDB
33-
//withPool(p pool.Pooler) *baseDB
34-
//WithTimeout(d time.Duration) *baseDB
35-
//WithParam(param string, value interface{}) *baseDB
36-
Param(param string) interface{}
37-
retryBackoff(retry int) time.Duration
38-
//getConn(ctx context.Context) (*pool.Conn, error)
39-
//initConn(ctx context.Context, cn *pool.Conn) error
40-
//releaseConn(ctx context.Context, cn *pool.Conn, err error)
41-
//withConn(ctx context.Context, fn func(context.Context, *pool.Conn) error) error
42-
shouldRetry(err error) bool
43-
Close() error
44-
Exec(query interface{}, params ...interface{}) (res pg.Result, err error)
45-
*/
46-
ExecContext(c context.Context, query interface{}, params ...interface{}) (pg.Result, error)
47-
/*
48-
//exec(ctx context.Context, query interface{}, params ...interface{}) (pg.Result, error)
49-
ExecOne(query interface{}, params ...interface{}) (pg.Result, error)
50-
ExecOneContext(ctx context.Context, query interface{}, params ...interface{}) (pg.Result, error)
51-
execOne(c context.Context, query interface{}, params ...interface{}) (pg.Result, error)
52-
Query(model interface{}, query interface{}, params ...interface{}) (res pg.Result, err error)
53-
QueryContext(c context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
54-
query(ctx context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
55-
QueryOne(model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
56-
QueryOneContext(ctx context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
57-
queryOne(ctx context.Context, model interface{}, query interface{}, params ...interface{}) (pg.Result, error)
58-
CopyFrom(r io.Reader, query interface{}, params ...interface{}) (res pg.Result, err error)
59-
//copyFrom(ctx context.Context, cn *pool.Conn, r io.Reader, query interface{}, params ...interface{}) (res Result, err error)
60-
CopyTo(w io.Writer, query interface{}, params ...interface{}) (res pg.Result, err error)
61-
//copyTo(ctx context.Context, cn *pool.Conn, w io.Writer, query interface{}, params ...interface{}) (res Result, err error)
62-
Ping(ctx context.Context) error
63-
Model(model ...interface{}) *pg.Query
64-
ModelContext(c context.Context, model ...interface{}) *pg.Query
65-
Formatter() orm.QueryFormatter
66-
//cancelRequest(processID int32, secretKey int32) error
67-
//simpleQuery(c context.Context, cn *pool.Conn, wb *pool.WriteBuffer) (*result, error)
68-
//simpleQueryData(c context.Context, cn *pool.Conn, model interface{}, wb *pool.WriteBuffer) (*result, error)
69-
Prepare(q string) (*pg.Stmt, error)
70-
//prepare(c context.Context, cn *pool.Conn, q string) (string, []types.ColumnInfo, error)
71-
//closeStmt(c context.Context, cn *pool.Conn, name string) error
72-
*/
13+
type SchemeCommenter interface {
14+
MakeTableComment(ctx context.Context, name string, comment string) error
15+
MakeColumnComment(ctx context.Context, tableName string, columnName string, comment string) error
7316
}
7417

7518
// PgGo -
@@ -83,7 +26,7 @@ func NewPgGo() *PgGo {
8326
}
8427

8528
// DB -
86-
func (db *PgGo) DB() PgDB {
29+
func (db *PgGo) DB() *pg.DB {
8730
return db.conn
8831
}
8932

@@ -148,3 +91,22 @@ func (db *PgGo) DeleteState(s *State) error {
14891
_, err := db.conn.Model(s).Where("index_name = ?", s.IndexName).Delete()
14992
return err
15093
}
94+
95+
func (db *PgGo) MakeTableComment(ctx context.Context, name string, comment string) error {
96+
_, err := db.conn.ExecContext(ctx,
97+
`COMMENT ON TABLE ? IS ?`,
98+
pg.Safe(name),
99+
pg.Safe(comment))
100+
101+
return err
102+
}
103+
104+
func (db *PgGo) MakeColumnComment(ctx context.Context, tableName string, columnName string, comment string) error {
105+
_, err := db.conn.ExecContext(ctx,
106+
`COMMENT ON COLUMN ?.? IS ?`,
107+
pg.Safe(tableName),
108+
pg.Safe(columnName),
109+
pg.Safe(comment))
110+
111+
return err
112+
}

database/pgComment.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package database
33
import (
44
"context"
55
"github.com/dipdup-net/go-lib/hasura"
6-
"github.com/go-pg/pg/v10"
76
"reflect"
87
"strings"
98
)
109

11-
func makeComments(ctx context.Context, conn PgGoConnection, models ...interface{}) error {
10+
func makeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}) error {
1211
for _, model := range models {
1312
modelType := reflect.TypeOf(model)
14-
var tableName pg.Safe
13+
var tableName string
1514

1615
for i := 0; i < modelType.NumField(); i++ {
1716
fieldType := modelType.Field(i)
@@ -20,17 +19,15 @@ func makeComments(ctx context.Context, conn PgGoConnection, models ...interface{
2019
var ok bool
2120
tableName, ok = getPgName(fieldType)
2221
if !ok {
23-
tableName = pg.Safe(hasura.ToSnakeCase(modelType.Name()))
22+
tableName = hasura.ToSnakeCase(modelType.Name())
2423
}
2524

2625
pgCommentTag, ok := getPgComment(fieldType)
2726
if !ok {
2827
continue
2928
}
3029

31-
if _, err := conn.DB().ExecContext(ctx,
32-
`COMMENT ON TABLE ? IS ?`,
33-
tableName, pgCommentTag); err != nil {
30+
if err := sc.MakeTableComment(ctx, tableName, pgCommentTag); err != nil {
3431
return err
3532
}
3633

@@ -44,20 +41,18 @@ func makeComments(ctx context.Context, conn PgGoConnection, models ...interface{
4441

4542
columnName, ok := getPgName(fieldType)
4643
if !ok {
47-
columnName = pg.Safe(hasura.ToSnakeCase(fieldType.Name))
44+
columnName = hasura.ToSnakeCase(fieldType.Name)
4845
}
4946

50-
if _, err := conn.DB().ExecContext(ctx,
51-
`COMMENT ON COLUMN ?.? IS ?`,
52-
tableName, columnName, pgCommentTag); err != nil {
47+
if err := sc.MakeColumnComment(ctx, tableName, columnName, pgCommentTag); err != nil {
5348
return err
5449
}
5550
}
5651
}
5752
return nil
5853
}
5954

60-
func getPgName(fieldType reflect.StructField) (name pg.Safe, ok bool) {
55+
func getPgName(fieldType reflect.StructField) (name string, ok bool) {
6156
pgTag, ok := fieldType.Tag.Lookup("pg")
6257
if !ok {
6358
return "", false
@@ -66,18 +61,18 @@ func getPgName(fieldType reflect.StructField) (name pg.Safe, ok bool) {
6661
tags := strings.Split(pgTag, ",")
6762

6863
if tags[0] != "" {
69-
name = pg.Safe(tags[0])
64+
name = tags[0]
7065
return name, ok
7166
}
7267

7368
return "", false
7469
}
7570

76-
func getPgComment(fieldType reflect.StructField) (pg.Safe, bool) {
71+
func getPgComment(fieldType reflect.StructField) (string, bool) {
7772
pgCommentTag, ok := fieldType.Tag.Lookup("pg-comment")
7873

7974
if ok {
80-
return pg.Safe(pgCommentTag), ok
75+
return pgCommentTag, ok
8176
}
8277

8378
return "", false

0 commit comments

Comments
 (0)