/
db.go
259 lines (213 loc) · 6.59 KB
/
db.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
package duty
import (
"context"
"database/sql"
"errors"
"flag"
"fmt"
"net/url"
"time"
"github.com/exaring/otelpgx"
"github.com/flanksource/commons/logger"
dutyContext "github.com/flanksource/duty/context"
"github.com/flanksource/duty/drivers"
dutyGorm "github.com/flanksource/duty/gorm"
"github.com/flanksource/duty/migrate"
"github.com/flanksource/duty/tracing"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/spf13/pflag"
gormpostgres "gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var pool *pgxpool.Pool
var DefaultQueryTimeout = 30 * time.Second
// LogLevel is the log level for gorm logger
var LogLevel string
func Now() clause.Expr {
return gorm.Expr("NOW()")
}
func Delete(ctx dutyContext.Context, model Table) error {
return ctx.DB().Model(model).UpdateColumn("deleted_at", Now()).Error
}
type Table interface {
TableName() string
}
func BindFlags(flags *pflag.FlagSet) {
flags.StringVar(&LogLevel, "db-log-level", "error", "Set gorm logging level. trace, debug & info")
}
func BindGoFlags() {
flag.StringVar(&LogLevel, "db-log-level", "error", "Set gorm logging level. trace, debug & info")
}
func DefaultGormConfig() *gorm.Config {
return &gorm.Config{
FullSaveAssociations: true,
NowFunc: func() time.Time {
return time.Now().UTC()
},
Logger: dutyGorm.NewGormLogger(LogLevel),
}
}
// creates a new Gorm DB connection using the global pgx connection pool, must be called after NewPgxPool
func NewGorm(connection string, config *gorm.Config) (*gorm.DB, error) {
db, err := NewDB(connection)
if err != nil {
return nil, err
}
Gorm, err := gorm.Open(
gormpostgres.New(gormpostgres.Config{Conn: db}),
config,
)
if err != nil {
return nil, err
}
if err := Gorm.Use(tracing.NewPlugin()); err != nil {
return nil, fmt.Errorf("error setting up tracing: %w", err)
}
return Gorm, nil
}
func getConnection(connection string) (string, error) {
pgxConfig, err := drivers.ParseURL(connection)
if err != nil {
return connection, err
} else if pgxConfig != nil {
return stdlib.RegisterConnConfig(pgxConfig), nil
}
return connection, nil
}
func NewDB(connection string) (*sql.DB, error) {
conn, err := getConnection(connection)
if err != nil {
return nil, err
}
return sql.Open("pgx", conn)
}
func NewPgxPool(connection string) (*pgxpool.Pool, error) {
connection, err := getConnection(connection)
if err != nil {
return nil, err
}
pgUrl, err := url.Parse(connection)
if err == nil {
logger.Infof("Connecting to %s", pgUrl.Redacted())
}
config, err := pgxpool.ParseConfig(connection)
if err != nil {
return nil, err
}
config.ConnConfig.Tracer = otelpgx.NewTracer(
otelpgx.WithIncludeQueryParameters(),
// This option is required to enable the WithSpanNameFunc
otelpgx.WithTrimSQLInSpanName(),
otelpgx.WithSpanNameFunc(func(stmt string) string {
// Trim span name after 80 chars
maxL := 80
if len(stmt) < maxL {
maxL = len(stmt)
}
return stmt[:maxL]
}),
)
// prevent deadlocks from concurrent queries
if config.MaxConns < 20 {
config.MaxConns = 20
}
pool, err = pgxpool.NewWithConfig(context.Background(), config)
if err != nil {
return nil, err
}
row := pool.QueryRow(context.Background(), "SELECT pg_size_pretty(pg_database_size($1));", config.ConnConfig.Database)
var size string
if err := row.Scan(&size); err != nil {
return nil, err
}
logger.Infof("Initialized DB: %s (%s)", config.ConnConfig.Host, size)
return pool, nil
}
func Migrate(connection string, opts *migrate.MigrateOptions) error {
db, err := NewDB(connection)
if err != nil {
return err
}
defer db.Close()
migrateOptions := opts
if migrateOptions == nil {
migrateOptions = &migrate.MigrateOptions{}
}
if err := migrate.RunMigrations(db, connection, *migrateOptions); err != nil {
return err
}
// Reload postgrest schema after migrations
if _, err := db.Exec(`NOTIFY pgrst, 'reload schema'`); err != nil {
return err
}
return nil
}
// HasMigrationsRun performs a rudimentary check to see if the migrations have
// run at least once.
func HasMigrationsRun(ctx context.Context, pool *pgxpool.Pool) (bool, error) {
var count int
if err := pool.QueryRow(ctx, "SELECT COUNT(*) FROM migration_logs WHERE path = '099_optimize.sql'").Scan(&count); err != nil {
return false, err
}
return count > 0, nil
}
func InitDB(connection string, migrateOpts *migrate.MigrateOptions) (*dutyContext.Context, error) {
db, pool, err := SetupDB(connection, migrateOpts)
if err != nil {
return nil, err
}
dutyctx := dutyContext.NewContext(context.Background()).WithDB(db, pool)
statementTimeout := dutyctx.Properties().String("connection.statement_timeout", "60min")
postgrestStatmentTimeout := dutyctx.Properties().String("connection.postgrest.statement_timeout", "60s")
setStatementTimeouts(dutyctx, dutyctx.Pool(), connection, statementTimeout, postgrestStatmentTimeout)
return &dutyctx, nil
}
// SetupDB runs migrations for the connection and returns a gorm.DB and a pgxpool.Pool
func SetupDB(connection string, migrateOpts *migrate.MigrateOptions) (gormDB *gorm.DB, pgxpool *pgxpool.Pool, err error) {
pgxpool, err = NewPgxPool(connection)
if err != nil {
return
}
conn, err := pgxpool.Acquire(context.TODO())
if err != nil {
return
}
defer conn.Release()
gormDB, err = NewGorm(connection, DefaultGormConfig())
if err != nil {
return
}
if err = Migrate(connection, migrateOpts); err != nil {
return
}
return
}
func setStatementTimeouts(ctx context.Context, pool *pgxpool.Pool, connection string, connStatementTimeout, postgrestStatementTimeout string) {
if _, err := pool.Exec(ctx, fmt.Sprintf(`ALTER ROLE postgrest_api SET statement_timeout = '%s'`, postgrestStatementTimeout)); err != nil {
logger.Errorf("failed to set statement timeout for role postgrest_api: %v", err)
}
if _, err := pool.Exec(ctx, fmt.Sprintf(`ALTER ROLE postgrest_anon SET statement_timeout = '%s'`, postgrestStatementTimeout)); err != nil {
logger.Errorf("failed to set statement timeout for role postgrest_anon: %v", err)
}
parsedConn, err := url.Parse(connection)
if err != nil {
return
}
user := parsedConn.User.Username()
if user != "" && connStatementTimeout != "" {
if _, err := pool.Exec(ctx, fmt.Sprintf(`ALTER ROLE %s SET statement_timeout = '%s'`, user, connStatementTimeout)); err != nil {
logger.Errorf("failed to set statement timeout for role %q: %v", user, err)
}
}
}
func IsForeignKeyError(err error) bool {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
return pgErr.Code == pgerrcode.ForeignKeyViolation
}
return false
}