-
Notifications
You must be signed in to change notification settings - Fork 2
/
connect.go
38 lines (31 loc) · 1 KB
/
connect.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
package rpckit_db
import (
"context"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
type DB struct {
DSN string
NoCache bool
Logger
// OnNotice is a callback function called when a notice response is received.
OnNotice NoticeHandler
// OnNotification is a callback function called when a notification from the LISTEN/NOTIFY system is received.
OnNotification NotificationHandler
}
// Connect makes connect to DB and returns ins handle
func (mig *Migrator) Connect(dsn string, opts ...Option) (*pgx.Conn, error) {
config, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, err
}
// config.Logger = logrusadapter.NewLogger(l)
//https://github.com/jackc/pgx/tree/master/log/zapadapter
config.OnNotice = func(c *pgconn.PgConn, n *pgconn.Notice) {
mig.ProcessNotice(n.Code, n.Message, n.Detail)
}
// TODO: statement_cache_mode = "describe"
config.BuildStatementCache = nil // disable stmt cache for `reinit pgmig`
ctx := context.Background()
return pgx.ConnectConfig(ctx, config)
}