-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
postgresql.go
483 lines (418 loc) · 11.2 KB
/
postgresql.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package testfixtures
import (
"database/sql"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"golang.org/x/sync/errgroup"
)
type postgreSQL struct {
baseHelper
useAlterConstraint bool
useDropConstraint bool
skipResetSequences bool
resetSequencesTo int64
tables []string
sequences []string
nonDeferrableConstraints []pgConstraint
constraints []pgConstraint
tablesChecksum map[string]string
version int
tablesHasIdentityColumnMutex sync.Mutex
tablesHasIdentityColumn map[string]bool
}
type pgConstraint struct {
tableName string
constraintName string
definition string
}
func (h *postgreSQL) init(db *sql.DB) error {
var grp errgroup.Group
grp.Go(func() error {
var err error
h.tables, err = h.tableNames(db)
return err
})
grp.Go(func() error {
var err error
h.sequences, err = h.getSequences(db)
return err
})
grp.Go(func() error {
var err error
h.nonDeferrableConstraints, err = h.getNonDeferrableConstraints(db)
return err
})
grp.Go(func() error {
var err error
h.constraints, err = h.getConstraints(db)
return err
})
grp.Go(func() error {
var err error
h.version, err = h.getMajorVersion(db)
return err
})
if err := grp.Wait(); err != nil {
return err
}
h.tablesHasIdentityColumn = make(map[string]bool)
return nil
}
func (*postgreSQL) paramType() int {
return paramTypeDollar
}
func (*postgreSQL) databaseName(q queryable) (string, error) {
var dbName string
err := q.QueryRow("SELECT current_database()").Scan(&dbName)
return dbName, err
}
func (h *postgreSQL) tableNames(q queryable) ([]string, error) {
var tables []string
const sql = `
SELECT pg_namespace.nspname || '.' || pg_class.relname
FROM pg_class
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_class.relkind = 'r'
AND pg_namespace.nspname NOT IN ('pg_catalog', 'information_schema', 'crdb_internal')
AND pg_namespace.nspname NOT LIKE 'pg_toast%'
AND pg_namespace.nspname NOT LIKE '\_timescaledb%';
`
rows, err := q.Query(sql)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var table string
if err = rows.Scan(&table); err != nil {
return nil, err
}
tables = append(tables, table)
}
if err = rows.Err(); err != nil {
return nil, err
}
return tables, nil
}
func (h *postgreSQL) getSequences(q queryable) ([]string, error) {
const sql = `
SELECT pg_namespace.nspname || '.' || pg_class.relname AS sequence_name
FROM pg_class
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE pg_class.relkind = 'S'
AND pg_namespace.nspname NOT LIKE '\_timescaledb%'
`
rows, err := q.Query(sql)
if err != nil {
return nil, err
}
defer rows.Close()
var sequences []string
for rows.Next() {
var sequence string
if err = rows.Scan(&sequence); err != nil {
return nil, err
}
sequences = append(sequences, sequence)
}
if err = rows.Err(); err != nil {
return nil, err
}
return sequences, nil
}
func (*postgreSQL) getNonDeferrableConstraints(q queryable) ([]pgConstraint, error) {
var constraints []pgConstraint
const sql = `
SELECT table_schema || '.' || table_name, constraint_name
FROM information_schema.table_constraints
WHERE constraint_type = 'FOREIGN KEY'
AND is_deferrable = 'NO'
AND table_schema <> 'crdb_internal'
AND table_schema NOT LIKE '\_timescaledb%'
`
rows, err := q.Query(sql)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var constraint pgConstraint
if err = rows.Scan(&constraint.tableName, &constraint.constraintName); err != nil {
return nil, err
}
constraints = append(constraints, constraint)
}
if err = rows.Err(); err != nil {
return nil, err
}
return constraints, nil
}
func (h *postgreSQL) getConstraints(q queryable) ([]pgConstraint, error) {
var constraints []pgConstraint
const sql = `
SELECT conrelid::regclass AS table_from, conname, pg_get_constraintdef(pg_constraint.oid)
FROM pg_constraint
INNER JOIN pg_namespace ON pg_namespace.oid = pg_constraint.connamespace
WHERE contype = 'f'
AND pg_namespace.nspname NOT IN ('pg_catalog', 'information_schema', 'crdb_internal')
AND pg_namespace.nspname NOT LIKE 'pg_toast%'
AND pg_namespace.nspname NOT LIKE '\_timescaledb%';
`
rows, err := q.Query(sql)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var constraint pgConstraint
if err = rows.Scan(
&constraint.tableName,
&constraint.constraintName,
&constraint.definition,
); err != nil {
return nil, err
}
constraints = append(constraints, constraint)
}
if err = rows.Err(); err != nil {
return nil, err
}
return constraints, nil
}
func (h *postgreSQL) dropAndRecreateConstraints(db *sql.DB, loadFn loadFunction) (err error) {
defer func() {
// Re-create constraints again after load
var b strings.Builder
for _, constraint := range h.constraints {
b.WriteString(fmt.Sprintf(
"ALTER TABLE %s ADD CONSTRAINT %s %s;",
h.quoteKeyword(constraint.tableName),
h.quoteKeyword(constraint.constraintName),
constraint.definition,
))
}
if _, err2 := db.Exec(b.String()); err2 != nil && err == nil {
err = err2
}
}()
var b strings.Builder
for _, constraint := range h.constraints {
b.WriteString(fmt.Sprintf(
"ALTER TABLE %s DROP CONSTRAINT %s;",
h.quoteKeyword(constraint.tableName),
h.quoteKeyword(constraint.constraintName),
))
}
if _, err := db.Exec(b.String()); err != nil {
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()
if err = loadFn(tx); err != nil {
return err
}
return tx.Commit()
}
func (h *postgreSQL) disableTriggers(db *sql.DB, loadFn loadFunction) (err error) {
defer func() {
var b strings.Builder
for _, table := range h.tables {
b.WriteString(fmt.Sprintf("ALTER TABLE %s ENABLE TRIGGER ALL;", h.quoteKeyword(table)))
}
if _, err2 := db.Exec(b.String()); err2 != nil && err == nil {
err = err2
}
}()
tx, err := db.Begin()
if err != nil {
return err
}
var b strings.Builder
for _, table := range h.tables {
b.WriteString(fmt.Sprintf("ALTER TABLE %s DISABLE TRIGGER ALL;", h.quoteKeyword(table)))
}
if _, err = tx.Exec(b.String()); err != nil {
return err
}
if err = loadFn(tx); err != nil {
_ = tx.Rollback()
return err
}
return tx.Commit()
}
func (h *postgreSQL) makeConstraintsDeferrable(db *sql.DB, loadFn loadFunction) (err error) {
defer func() {
// ensure constraint being not deferrable again after load
var b strings.Builder
for _, constraint := range h.nonDeferrableConstraints {
b.WriteString(fmt.Sprintf("ALTER TABLE %s ALTER CONSTRAINT %s NOT DEFERRABLE;", h.quoteKeyword(constraint.tableName), h.quoteKeyword(constraint.constraintName)))
}
if _, err2 := db.Exec(b.String()); err2 != nil && err == nil {
err = err2
}
}()
var b strings.Builder
for _, constraint := range h.nonDeferrableConstraints {
b.WriteString(fmt.Sprintf("ALTER TABLE %s ALTER CONSTRAINT %s DEFERRABLE;", h.quoteKeyword(constraint.tableName), h.quoteKeyword(constraint.constraintName)))
}
if _, err := db.Exec(b.String()); err != nil {
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()
if _, err = tx.Exec("SET CONSTRAINTS ALL DEFERRED"); err != nil {
return err
}
if err = loadFn(tx); err != nil {
return err
}
return tx.Commit()
}
func (h *postgreSQL) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
// ensure sequences being reset after load
if !h.skipResetSequences {
defer func() {
if err2 := h.resetSequences(db); err2 != nil && err == nil {
err = err2
}
}()
}
if h.useDropConstraint {
return h.dropAndRecreateConstraints(db, loadFn)
}
if h.useAlterConstraint {
return h.makeConstraintsDeferrable(db, loadFn)
}
return h.disableTriggers(db, loadFn)
}
func (h *postgreSQL) resetSequences(db *sql.DB) error {
if len(h.sequences) == 0 {
return nil
}
resetSequencesTo := h.resetSequencesTo
if resetSequencesTo == 0 {
resetSequencesTo = 10000
}
b := strings.Builder{}
for _, sequence := range h.sequences {
b.WriteString(fmt.Sprintf("SELECT SETVAL('%s', %d);", sequence, resetSequencesTo))
}
_, err := db.Exec(b.String())
return err
}
func (h *postgreSQL) isTableModified(q queryable, tableName string) (bool, error) {
oldChecksum, found := h.tablesChecksum[tableName]
if !found {
return true, nil
}
checksum, err := h.getChecksum(q, tableName)
if err != nil {
return true, err
}
return checksum != oldChecksum, nil
}
func (h *postgreSQL) computeTablesChecksum(q queryable) error {
if h.tablesChecksum != nil {
return nil
}
h.tablesChecksum = make(map[string]string, len(h.tables))
for _, t := range h.tables {
checksum, err := h.getChecksum(q, t)
if err != nil {
return err
}
h.tablesChecksum[t] = checksum
}
return nil
}
func (h *postgreSQL) getChecksum(q queryable, tableName string) (string, error) {
sqlStr := fmt.Sprintf(`
SELECT md5(CAST((json_agg(t.*)) AS TEXT))
FROM %s AS t
`,
h.quoteKeyword(tableName),
)
var checksum sql.NullString
if err := q.QueryRow(sqlStr).Scan(&checksum); err != nil {
return "", err
}
return checksum.String, nil
}
func (*postgreSQL) quoteKeyword(s string) string {
parts := strings.Split(s, ".")
for i, p := range parts {
parts[i] = fmt.Sprintf(`"%s"`, p)
}
return strings.Join(parts, ".")
}
func (h *postgreSQL) buildInsertSQL(q queryable, tableName string, columns, values []string) (string, error) {
if h.version >= 10 {
ok, err := h.tableHasIdentityColumn(q, tableName)
if err != nil {
return "", err
}
if ok {
return fmt.Sprintf(
"INSERT INTO %s (%s) OVERRIDING SYSTEM VALUE VALUES (%s)",
tableName,
strings.Join(columns, ", "),
strings.Join(values, ", "),
), nil
}
}
return h.baseHelper.buildInsertSQL(q, tableName, columns, values)
}
func (h *postgreSQL) tableHasIdentityColumn(q queryable, tableName string) (bool, error) {
defer h.tablesHasIdentityColumnMutex.Unlock()
h.tablesHasIdentityColumnMutex.Lock()
hasIdentityColumn, exists := h.tablesHasIdentityColumn[tableName]
if exists {
return hasIdentityColumn, nil
}
parts := strings.Split(tableName, ".")
tableName = parts[0][1 : len(parts[0])-1]
if len(parts) > 1 {
tableName = parts[1][1 : len(parts[1])-1]
}
query := `
SELECT COUNT(*) AS count
FROM information_schema.columns
WHERE table_name = $1
AND is_identity = 'YES'
`
var count int
if err := q.QueryRow(query, tableName).Scan(&count); err != nil {
return false, err
}
h.tablesHasIdentityColumn[tableName] = count > 0
return h.tablesHasIdentityColumn[tableName], nil
}
func (h *postgreSQL) getMajorVersion(q queryable) (int, error) {
var version string
err := q.QueryRow("SELECT VERSION()").Scan(&version)
if err != nil {
return 0, err
}
return h.parseMajorVersion(version)
}
func (*postgreSQL) parseMajorVersion(version string) (int, error) {
re := regexp.MustCompile(`\d+`)
versionNumbers := re.FindAllString(version, -1)
if len(versionNumbers) > 0 {
majorVersion, err := strconv.Atoi(versionNumbers[0])
if err != nil {
return 0, err
}
return majorVersion, nil
}
return 0, fmt.Errorf("testfixtures: could not parse major version from: %s", version)
}