-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
252 lines (205 loc) · 6.1 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
package duty
import (
"context"
"database/sql"
"flag"
"fmt"
"time"
"github.com/exaring/otelpgx"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/api"
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/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
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 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.NewSqlLogger(logger.GetLogger("db")),
}
}
// 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
}
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(config api.Config) error {
db, err := NewDB(config.ConnectionString)
if err != nil {
return err
}
defer db.Close()
if err := migrate.RunMigrations(db, config); 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 dutyContext.Context) (bool, error) {
var count int
if err := ctx.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(config api.Config) (*dutyContext.Context, error) {
db, pool, err := SetupDB(config)
if err != nil {
return nil, err
}
dutyctx := dutyContext.NewContext(context.Background()).WithDB(db, pool)
setStatementTimeouts(dutyctx, config)
migrationsRan, err := HasMigrationsRun(dutyctx)
if err != nil {
return nil, fmt.Errorf("error querying migration logs: %w", err)
}
if !migrationsRan {
return nil, fmt.Errorf("database migrations have not been run")
}
return &dutyctx, nil
}
// SetupDB runs migrations for the connection and returns a gorm.DB and a pgxpool.Pool
func SetupDB(config api.Config) (gormDB *gorm.DB, pgxpool *pgxpool.Pool, err error) {
logger.Infof("Connecting to %s", config)
pgxpool, err = NewPgxPool(config.ConnectionString)
if err != nil {
return
}
conn, err := pgxpool.Acquire(context.TODO())
if err != nil {
return
}
defer conn.Release()
if err := conn.Ping(context.Background()); err != nil {
return nil, nil, fmt.Errorf("error pinging database: %w", err)
}
cfg := DefaultGormConfig()
if config.LogName != "" {
cfg.Logger = dutyGorm.NewSqlLogger(logger.GetLogger(config.LogName))
}
gormDB, err = NewGorm(config.ConnectionString, cfg)
if err != nil {
return
}
if config.Migrate() {
if err = Migrate(config); err != nil {
return
}
}
return
}
func setStatementTimeouts(ctx dutyContext.Context, config api.Config) {
postgrestTimeout := ctx.Properties().Duration("db.postgrest.timeout", 1*time.Minute)
if err := ctx.DB().Raw(fmt.Sprintf(`ALTER ROLE %s SET statement_timeout = '%0fs'`, config.Postgrest.DBRole, postgrestTimeout.Seconds())).Error; err != nil {
logger.Errorf(err.Error())
}
if config.Postgrest.AnonDBRole != "" {
if err := ctx.DB().Raw(fmt.Sprintf(`ALTER ROLE %s SET statement_timeout = '%0fs'`, config.Postgrest.AnonDBRole, postgrestTimeout.Seconds())).Error; err != nil {
logger.Errorf(err.Error())
}
}
statementTimeout := ctx.Properties().Duration("db.connection.timeout", 1*time.Hour)
if username := config.GetUsername(); username != "" {
if err := ctx.DB().Raw(fmt.Sprintf(`ALTER ROLE %s SET statement_timeout = '%0fs'`, username, statementTimeout.Seconds())).Error; err != nil {
logger.Errorf(err.Error())
}
}
}