-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.go
426 lines (370 loc) · 9.52 KB
/
database.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package go_test_pg
import (
"context"
"crypto/md5"
"database/sql"
"encoding/binary"
"encoding/hex"
"fmt"
"log"
"math/rand"
"os"
"sync"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/jackc/pgx/v5/tracelog"
"github.com/pkg/errors"
)
const defaultTimeout = 30 * time.Second
type Fixture struct {
Query string
Params []interface{}
}
type Pgpool struct {
// BaseName is the prefix of template and temporary databases.
// Default is dbtestpg.
BaseName string
// Name of schema file. If empty, create empty database.
SchemaFile string // schema file name
// If true, skip all database tests.
Skip bool
m sync.RWMutex
err error
tmpl string
rnd *rand.Rand
}
// WithFixtures creates database from template database, and initializes it
// with fixtures from `fixtures` array
func (p *Pgpool) WithFixtures(t testing.TB, fixtures []Fixture) *pgxpool.Pool {
pool := p.WithEmpty(t)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
for i, f := range fixtures {
if _, err := pool.Exec(ctx, f.Query, f.Params...); err != nil {
t.Fatalf(
"can't load fixture at idx %v: %+v",
i, errors.WithStack(err),
)
}
}
return pool
}
// WithStdFixtures creates database from template database, and initializes it
// with fixtures from `fixtures` array
func (p *Pgpool) WithStdFixtures(t testing.TB, fixtures []Fixture) *sql.DB {
db := p.WithStdEmpty(t)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
for i, f := range fixtures {
if _, err := db.ExecContext(ctx, f.Query, f.Params...); err != nil {
t.Fatalf("can't load fixture at idx %v: %+v",
i, errors.WithStack(err))
}
}
return db
}
// WithSQLs creates database from template database, and initializes it
// with fixtures from `sqls` array
func (p *Pgpool) WithSQLs(t testing.TB, sqls []string) *pgxpool.Pool {
pool := p.WithEmpty(t)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
for i, s := range sqls {
if _, err := pool.Exec(ctx, s); err != nil {
t.Fatalf(
"can't load fixture at idx %v: %+v",
i, errors.WithStack(err),
)
}
}
return pool
}
// WithStdSQLs creates database from template database, and initializes it
// with fixtures from `sqls` array
func (p *Pgpool) WithStdSQLs(t testing.TB, sqls []string) *sql.DB {
db := p.WithStdEmpty(t)
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
for i, s := range sqls {
if _, err := db.ExecContext(ctx, s); err != nil {
t.Fatalf("can't load fixture at idx %v: %+v",
i, errors.WithStack(err))
}
}
return db
}
func (p *Pgpool) getTmpl(t testing.TB) string {
t.Helper()
if p.Skip {
t.Skip("Skip database tests")
}
p.m.RLock()
err := p.err
tmpl := p.tmpl
p.m.RUnlock()
if err != nil {
t.Fatal(err)
}
if tmpl != "" {
return tmpl
}
p.m.Lock()
p.rnd = rand.New(rand.NewSource(time.Now().UnixNano() + int64(os.Getpid())))
p.tmpl, p.err = p.createTemplateDB()
err = p.err
p.m.Unlock()
if err != nil {
t.Fatalf("%+v", err)
}
return p.tmpl
}
// Register pgx.ConnConfig with std driver.
// Return connection string for database/sql and error.
func (p *Pgpool) registerStdConfig(t testing.TB,
dbName string) (string, error) {
connConfig, err := pgx.ParseConfig("")
if err != nil {
return "", errors.WithStack(err)
}
connConfig.Tracer = &tracelog.TraceLog{
Logger: newLogger(t),
LogLevel: tracelog.LogLevelTrace,
}
connConfig.Database = dbName
return stdlib.RegisterConnConfig(connConfig), nil
}
func (p *Pgpool) createRndDB(t testing.TB) (string, error) {
tmpl := p.getTmpl(t)
dbName := fmt.Sprintf("%v_%v", tmpl, p.rnd.Int31())
return dbName, p.createDB(dbName, tmpl)
}
func (p *Pgpool) createRndDBPool(
t testing.TB) (pool *pgxpool.Pool, dbName string) {
var err error
dbName, err = p.createRndDB(t)
if err != nil {
t.Fatal(err)
}
var cfg *pgxpool.Config
cfg, err = pgxpool.ParseConfig("")
if err != nil {
_ = dropDB(dbName)
t.Fatal(err)
}
cfg.ConnConfig.Database = dbName
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
pool, err = pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
_ = dropDB(dbName)
t.Fatal()
}
return pool, dbName
}
func withNewConnection(
dbName string,
fn func(context.Context, *pgx.Conn) error,
) (err error) {
var cfg *pgx.ConnConfig
cfg, err = pgx.ParseConfig("")
if err != nil {
return errors.WithStack(err)
}
if dbName != "" {
cfg.Database = dbName
}
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
conn, err := pgx.ConnectConfig(ctx, cfg)
if err != nil {
return errors.WithStack(err)
}
defer func() {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
err2 := conn.Close(ctx)
cancel()
if err2 != nil {
if err == nil {
err = errors.WithStack(err2)
} else {
log.Printf("error closing DB connection: %v", err2)
}
}
}()
err = fn(ctx, conn)
return err
}
func dropDB(dbName string) error {
return withNewConnection(
"",
func(ctx context.Context, conn *pgx.Conn) error {
_, err := conn.Exec(ctx, "DROP DATABASE "+quote(dbName))
return errors.WithStack(err)
},
)
}
// WithEmpty creates empty database from template database, that was
// created from `schema` file.
func (p *Pgpool) WithEmpty(t testing.TB) *pgxpool.Pool {
pool, dbName := p.createRndDBPool(t)
t.Cleanup(func() {
acquiredConns := pool.Stat().AcquiredConns()
if acquiredConns > 0 {
t.Fatalf(
"unreleased connections exists: %v, can't drop database %v",
acquiredConns, dbName,
)
}
pool.Close()
err := dropDB(dbName)
if err != nil {
t.Errorf("Can't drop DB %v: %v", dbName, err)
}
})
return pool
}
// WithStdEmpty creates empty database from template database, that was
// created from `schema` file.
func (p *Pgpool) WithStdEmpty(t testing.TB) *sql.DB {
db, cleanupFn := p.newStdDBWithCleanup(t)
if cleanupFn != nil {
t.Cleanup(func() {
if err := cleanupFn(); err != nil {
t.Error(err)
}
})
}
return db
}
func (p *Pgpool) newStdDBWithCleanup(
t testing.TB) (db *sql.DB, cleanupFn func() error) {
dbName, err := p.createRndDB(t)
if err != nil {
t.Fatal(err)
return nil, nil
}
connString, err := p.registerStdConfig(t, dbName)
if err != nil {
_ = dropDB(dbName)
t.Fatal(err)
return nil, nil
}
db, err = sql.Open("pgx", connString)
if err != nil {
_ = dropDB(dbName)
t.Fatal(err)
return nil, nil
}
cleanupFn = func() error {
stats := db.Stats()
if stats.InUse > 0 {
return errors.Errorf(
"unreleased connections exists: %v, can't drop database %v",
stats.InUse, dbName)
}
err := db.Close()
if err != nil {
return errors.Errorf("Can't close DB %v: %v", dbName, err)
}
err = dropDB(dbName)
if err != nil {
return errors.Errorf("Can't drop DB %v: %v", dbName, err)
}
return nil
}
return db, cleanupFn
}
func (p *Pgpool) createDB(name, tmplName string) error {
query := `CREATE DATABASE ` + quote(name)
if tmplName != "" {
query += ` WITH TEMPLATE ` + quote(tmplName)
}
return withNewConnection(
"",
func(ctx context.Context, conn *pgx.Conn) error {
_, err := conn.Exec(ctx, query)
return errors.WithStack(err)
},
)
}
// Creates template db, populates with SQLs from schema file and return name
// of the new database. If database is exists, just return its name.
func (p *Pgpool) createTemplateDB() (string, error) {
if p.SchemaFile == "" {
return "template1", nil
}
schemaSql, err := os.ReadFile(p.SchemaFile)
if err != nil {
return "", errors.WithStack(err)
}
checksum := md5.Sum(schemaSql)
schemaHex := hex.EncodeToString(checksum[:])
baseName := "dbtestpg"
if p.BaseName != "" {
baseName = p.BaseName
}
tmplDbName := fmt.Sprintf("%v_%v", baseName, schemaHex)
// ID of the advisory lock. Lock would be taken on master database (not
// on database we are going to create) and prevent from parallel creation
// of the same database from separate processes.
lockID := int64(binary.BigEndian.Uint64(checksum[:8]))
err = withNewConnection(
"",
func(ctx context.Context, conn *pgx.Conn) error {
var dbExists bool
err := conn.QueryRow(ctx,
`SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname = $1)`,
tmplDbName).Scan(&dbExists)
if err != nil {
return errors.WithStack(err)
}
if dbExists {
return nil
}
// If we need to create a database, take an advisory lock on
// master database to prevent parallel creation of databases
// from several test processes.
_, err = conn.Exec(ctx, `SELECT pg_advisory_lock($1)`, lockID)
if err != nil {
return errors.WithStack(err)
}
// Check again for database existence. Database may be created
// in parallel process while waiting for the lock.
err = conn.QueryRow(ctx,
`SELECT EXISTS(SELECT 1 FROM pg_database WHERE datname = $1)`,
tmplDbName).Scan(&dbExists)
if err != nil {
return errors.WithStack(err)
}
if dbExists {
return nil
}
_, err = conn.Exec(ctx, `CREATE DATABASE `+quote(tmplDbName))
if err != nil {
return errors.WithStack(err)
}
err = withNewConnection(
tmplDbName,
func(ctx context.Context, conn *pgx.Conn) error {
_, err = conn.Exec(ctx, string(schemaSql))
return errors.WithStack(err)
},
)
if err != nil {
_ = dropDB(tmplDbName)
return err
}
return nil
},
)
if err != nil {
return "", err
}
return tmplDbName, nil
}
func quote(name string) string {
return pgx.Identifier{name}.Sanitize()
}