-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
406 lines (380 loc) · 11.6 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
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
package httpbaselinetest
import (
"bytes"
"encoding/json"
"os"
"sort"
"strings"
"github.com/jmoiron/sqlx"
"github.com/romanyx/polluter"
"gopkg.in/yaml.v2" // same yaml used by polluter
)
type formattedDbBaseline struct {
NumRowsInserted uint64 `json:"numRowsInserted"`
NumRowsUpdated uint64 `json:"numRowsUpdated"`
NumRowsDeleted uint64 `json:"numRowsDeleted"`
RemovedRows []interface{} `json:"removedRows"`
AddedRows []interface{} `json:"addedRows"`
}
type pgStatUserTableInsUpdDel struct {
Relname string `db:"relname"`
NTupIns uint64 `db:"n_tup_ins"`
NTupUpd uint64 `db:"n_tup_upd"`
NTupDel uint64 `db:"n_tup_del"`
}
type dbTableInfo struct {
InitialTableNames []string
PgBaseline pgBaselineMap
}
type JSONTableData map[string]bool
type pgBaselineData struct {
PgInsUpdDel *pgStatUserTableInsUpdDel
BeforeTableData *JSONTableData
}
type pgBaselineMap map[string]pgBaselineData
type dbBaseline map[string]formattedDbBaseline
func getTableStats(db *sqlx.DB, tableStats *[]pgStatUserTableInsUpdDel) error {
// From https://www.postgresql.org/docs/current/monitoring-stats.html
//
// pg_stat_xact_all_tables
//
// Similar to pg_stat_all_tables, but counts actions taken so
// far within the current transaction (which are not yet
// included in pg_stat_all_tables and related views). The
// columns for numbers of live and dead rows and vacuum and
// analyze actions are not present in this view.
//
// We use pg_stat_xact_user_tables since that's all we are
// interested in
sql := `
SELECT
relname, n_tup_ins, n_tup_upd, n_tup_del
FROM
pg_stat_xact_user_tables
`
return db.Select(tableStats, sql)
}
func getTableDependencyOrder(db *sqlx.DB) ([]string, error) {
sql := `
SELECT kcu.table_name AS foreign_table,
rel_tco.table_name AS primary_table
FROM information_schema.table_constraints tco
JOIN information_schema.key_column_usage kcu
ON tco.constraint_schema = kcu.constraint_schema
AND tco.constraint_name = kcu.constraint_name
JOIN information_schema.referential_constraints rco
ON tco.constraint_schema = rco.constraint_schema
AND tco.constraint_name = rco.constraint_name
JOIN information_schema.table_constraints rel_tco
ON rco.unique_constraint_schema = rel_tco.constraint_schema
AND rco.unique_constraint_name = rel_tco.constraint_name
WHERE tco.constraint_type = 'FOREIGN KEY'
GROUP BY kcu.table_name,
rel_tco.table_name
ORDER BY kcu.table_name
`
rows, err := db.Queryx(sql)
if err != nil {
return nil, err
}
depMap := make(map[string][]string)
for rows.Next() {
cols, err := rows.SliceScan()
if err != nil {
return nil, err
}
foreignTable := string(cols[0].([]uint8))
primaryTable := string(cols[1].([]uint8))
if foreignTable == primaryTable {
continue
}
_, ok := depMap[foreignTable]
if !ok {
depMap[foreignTable] = make([]string, 0)
}
depMap[foreignTable] = append(depMap[foreignTable], primaryTable)
}
return dependencyOrder(depMap), nil
}
func dependencyOrder(depMap map[string][]string) []string {
visited := make(map[string]bool)
deps := []string{}
todo := []string{}
var tableName string
// first put all tables into todo
for tableName := range depMap {
todo = append(todo, tableName)
}
for len(todo) > 0 {
// pop the first entry
tableName, todo = todo[0], todo[1:]
// if already processed, skip
if visited[tableName] {
continue
}
tdeps, ok := depMap[tableName]
// if this table has no dependencies, all done
if !ok || len(tdeps) == 0 {
deps = append(deps, tableName)
visited[tableName] = true
continue
}
// if all dependencies are done, append to deps
allDepsVisited := true
for _, depTableName := range tdeps {
allDepsVisited = allDepsVisited && visited[depTableName]
}
if allDepsVisited {
deps = append(deps, tableName)
visited[tableName] = true
} else {
// prepend dependencies so they are processed
// next
tdeps = append(tdeps, tableName)
todo = append(tdeps, todo...)
}
}
return deps
}
func getJSONTableData(db *sqlx.DB, tableName string, jsonTableData *JSONTableData) error {
sql := `SELECT to_jsonb("` + tableName + `".*) AS json_data FROM "` +
tableName + `" ORDER BY 1`
rows, err := db.Queryx(sql)
if err != nil {
return err
}
for rows.Next() {
var jsonData string
err = rows.Scan(&jsonData)
if err != nil {
return err
}
(*jsonTableData)[jsonData] = true
}
return nil
}
func buildFormattedDbBaseline(pgInsUpDel pgStatUserTableInsUpdDel,
removedRows []string, addedRows []string) (formattedDbBaseline, error) {
removedRowsJSON := make([]interface{}, len(removedRows))
addedRowsJSON := make([]interface{}, len(addedRows))
sort.Strings(removedRows)
for i := range removedRows {
var v interface{}
err := json.Unmarshal([]byte(removedRows[i]), &v)
if err != nil {
return formattedDbBaseline{}, err
}
removedRowsJSON[i] = v
}
sort.Strings(addedRows)
for i := range addedRows {
var v interface{}
err := json.Unmarshal([]byte(addedRows[i]), &v)
if err != nil {
return formattedDbBaseline{}, err
}
addedRowsJSON[i] = v
}
fdb := formattedDbBaseline{
NumRowsInserted: pgInsUpDel.NTupIns,
NumRowsUpdated: pgInsUpDel.NTupUpd,
NumRowsDeleted: pgInsUpDel.NTupDel,
RemovedRows: removedRowsJSON,
AddedRows: addedRowsJSON,
}
return fdb, nil
}
func formatDb(fullDbBaseline map[string]formattedDbBaseline) ([]byte, error) {
// use encoder to add trailing newline
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
err := enc.Encode(fullDbBaseline)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (r *httpBaselineTestRunner) seedWithPolluter() {
f, err := os.Open(r.seedPath)
if err != nil {
r.t.Fatalf("Error opening seed file '%s': %s", r.seedPath, err)
}
defer f.Close()
p := polluter.New(polluter.PostgresEngine(r.btest.Db.DB))
err = p.Pollute(f)
if err != nil {
r.t.Fatalf("Error polluting db: %s", err)
}
}
func (r *httpBaselineTestRunner) getDbTableInfo() {
beforeTableStats := []pgStatUserTableInsUpdDel{}
err := getTableStats(r.btest.Db, &beforeTableStats)
if err != nil {
r.t.Fatalf("Error selecting user table info: %s", err)
}
r.dbTableInfo.InitialTableNames = make([]string, len(beforeTableStats))
r.dbTableInfo.PgBaseline = make(pgBaselineMap)
jsonTableDataMap := make(map[string]*JSONTableData)
for _, tableName := range r.btest.Tables {
jtd := make(JSONTableData)
err := getJSONTableData(r.btest.Db, tableName, &jtd)
if err != nil {
r.t.Fatalf("Error getting data for %s: %s", tableName, err)
}
jsonTableDataMap[tableName] = &jtd
}
for i := range beforeTableStats {
tableName := beforeTableStats[i].Relname
r.dbTableInfo.InitialTableNames[i] = tableName
jtd := jsonTableDataMap[tableName]
r.dbTableInfo.PgBaseline[tableName] = pgBaselineData{
PgInsUpdDel: &beforeTableStats[i],
BeforeTableData: jtd,
}
}
sort.Strings(r.dbTableInfo.InitialTableNames)
}
func doRegenerateSeed() bool {
return os.Getenv("REGENERATE_SEED") != ""
}
func (r *httpBaselineTestRunner) dbTestSetup() {
if doRegenerateSeed() {
// getDbTableInfo only dumps rows for the test tables,
// so fake that out by putting all tables in there temporarily
origTestTables := r.btest.Tables
allTableStats := []pgStatUserTableInsUpdDel{}
err := getTableStats(r.btest.Db, &allTableStats)
if err != nil {
r.t.Fatalf("Error selecting user table info: %s", err)
}
allTables := make([]string, len(allTableStats))
for i := range allTableStats {
allTables[i] = allTableStats[i].Relname
}
r.btest.Tables = allTables
r.getDbTableInfo()
r.btest.Tables = origTestTables
}
if r.btest.SeedFunc != nil {
err := r.btest.SeedFunc(r.btest)
if err != nil {
r.t.Fatalf("Error running SeedFunc: %s", err)
}
if doRegenerateSeed() {
r.dumpForPolluter()
}
}
if r.btest.Seed != "" && !doRegenerateSeed() {
r.seedWithPolluter()
}
r.getDbTableInfo()
}
func (r *httpBaselineTestRunner) generateDbBaseline() dbBaseline {
afterTableStats := []pgStatUserTableInsUpdDel{}
err := getTableStats(r.btest.Db, &afterTableStats)
if err != nil {
r.t.Fatalf("Error selecting user table info: %s", err)
}
afterTableNames := make([]string, len(afterTableStats))
afterTableMap := make(map[string]pgStatUserTableInsUpdDel)
for i := range afterTableStats {
tableName := afterTableStats[i].Relname
afterTableNames[i] = tableName
afterTableMap[tableName] = afterTableStats[i]
}
sort.Strings(afterTableNames)
beforeKeyString := strings.Join(r.dbTableInfo.InitialTableNames, ",")
afterKeyString := strings.Join(afterTableNames, ",")
if beforeKeyString != afterKeyString {
r.t.Errorf("Before tables: %s", beforeKeyString)
r.t.Errorf("After tables: %s", afterKeyString)
r.t.Fatal("Tables created/deleted in test")
}
fullDbBaseline := make(map[string]formattedDbBaseline)
for _, tableName := range r.dbTableInfo.InitialTableNames {
beforePgInsUpdDel := *(r.dbTableInfo.PgBaseline[tableName].PgInsUpdDel)
afterPgInsUpdDel := afterTableMap[tableName]
diffPgInsUpdDel := pgStatUserTableInsUpdDel{
Relname: tableName,
NTupIns: afterPgInsUpdDel.NTupIns - beforePgInsUpdDel.NTupIns,
NTupUpd: afterPgInsUpdDel.NTupDel - beforePgInsUpdDel.NTupDel,
NTupDel: afterPgInsUpdDel.NTupDel - beforePgInsUpdDel.NTupDel,
}
if r.dbTableInfo.PgBaseline[tableName].BeforeTableData != nil {
// BeforeTableData means this is in btest.Tables
afterJSONTableData := make(JSONTableData)
err := getJSONTableData(r.btest.Db, tableName, &afterJSONTableData)
if err != nil {
r.t.Fatalf("Error getting data for %s: %s", tableName, err)
}
removedRows := []string{}
addedRows := []string{}
btd := *(r.dbTableInfo.PgBaseline[tableName].BeforeTableData)
for row := range btd {
if !afterJSONTableData[row] {
removedRows = append(removedRows, row)
}
}
for row := range afterJSONTableData {
if !btd[row] {
addedRows = append(addedRows, row)
}
}
tableDbBaseline, err := buildFormattedDbBaseline(diffPgInsUpdDel, removedRows, addedRows)
if err != nil {
r.t.Fatalf("Error building formatted db baseline %s", err)
}
fullDbBaseline[tableName] = tableDbBaseline
} else {
if beforePgInsUpdDel != afterPgInsUpdDel {
fullDbBaseline[tableName] = formattedDbBaseline{
NumRowsInserted: diffPgInsUpdDel.NTupIns,
NumRowsDeleted: diffPgInsUpdDel.NTupDel,
NumRowsUpdated: diffPgInsUpdDel.NTupUpd,
}
}
}
}
return fullDbBaseline
}
func (r *httpBaselineTestRunner) assertNoDbChanges(fullDbBaseline map[string]formattedDbBaseline) {
if len(fullDbBaseline) != 0 {
for tableName, tableDiff := range fullDbBaseline {
r.t.Errorf("Unexpected table change for %s: %d row(s) inserted, %d row(s) updated, %d row(s) deleted",
tableName, tableDiff.NumRowsInserted,
tableDiff.NumRowsUpdated,
tableDiff.NumRowsDeleted)
}
r.t.FailNow()
}
}
func (r *httpBaselineTestRunner) dumpForPolluter() {
dbBaseline := r.generateDbBaseline()
polluterMap := make(map[string][]interface{})
for tableName := range dbBaseline {
if len(dbBaseline[tableName].AddedRows) > 0 {
polluterMap[tableName] = dbBaseline[tableName].AddedRows
}
}
tableDeps, err := getTableDependencyOrder(r.btest.Db)
if err != nil {
r.t.Fatalf("Error getting table dependency order: %s", err)
}
yamlBytes := make([]byte, 0)
for _, tableName := range tableDeps {
tableData, ok := polluterMap[tableName]
tableMap := make(map[string][]interface{})
tableMap[tableName] = tableData
if ok {
bytes, err := yaml.Marshal(tableMap)
if err != nil {
r.t.Fatalf("Error regenerating seed yaml: %s", err)
}
yamlBytes = append(yamlBytes, bytes...)
}
}
r.writeFile(r.seedPath, yamlBytes)
// reset
r.dbTableInfo = &dbTableInfo{}
}