/
snapshot.go
345 lines (306 loc) · 10.7 KB
/
snapshot.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
/*******************************************************************************
*
* Copyright 2021 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package easypg
import (
"database/sql"
"fmt"
"sort"
"strings"
"testing"
"time"
)
//NOTE: This file contains various private types for taking and diffing
// database snapshots and serializing them into SQL statements. The public API
// for these types is in `testhelpers.go`.
////////////////////////////////////////////////////////////////////////////////
// type dbSnapshot
// dbSnapshot is a map with table names as keys and table snapshots as values.
type dbSnapshot map[string]tableSnapshot
const (
listAllTablesQuery = `
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public' AND table_name != 'schema_migrations'
ORDER BY table_name COLLATE "C"
`
listKeyColumnsQuery = `
SELECT table_name, column_name FROM information_schema.key_column_usage
WHERE table_schema = 'public' AND table_name != 'schema_migrations' AND position_in_unique_constraint IS NULL
`
listColumnDefaultsQuery = `
SELECT table_name, column_name, is_nullable, column_default FROM information_schema.columns
WHERE table_schema = 'public' AND table_name != 'schema_migrations'
`
)
func newDBSnapshot(t *testing.T, db *sql.DB) dbSnapshot {
t.Helper()
// list all tables
var tableNames []string
rows, err := db.Query(listAllTablesQuery)
failOnErr(t, err)
for rows.Next() {
var name string
failOnErr(t, rows.Scan(&name))
tableNames = append(tableNames, name)
}
failOnErr(t, rows.Err())
failOnErr(t, rows.Close()) //nolint:sqlclosecheck
// list key columns for all tables
keyColumnNames := make(map[string][]string)
rows, err = db.Query(listKeyColumnsQuery)
failOnErr(t, err)
for rows.Next() {
var (
tableName string
columnName string
)
failOnErr(t, rows.Scan(&tableName, &columnName))
keyColumnNames[tableName] = append(keyColumnNames[tableName], columnName)
}
failOnErr(t, rows.Err())
failOnErr(t, rows.Close()) //nolint:sqlclosecheck
// list column default values for all tables
columnDefaults := make(map[string]map[string]string)
rows, err = db.Query(listColumnDefaultsQuery)
failOnErr(t, err)
for rows.Next() {
var (
tableName string
columnName string
isNullableStr string // either "NO" or "YES"
defaultExpr *string
)
failOnErr(t, rows.Scan(&tableName, &columnName, &isNullableStr, &defaultExpr))
if columnDefaults[tableName] == nil {
columnDefaults[tableName] = make(map[string]string)
}
switch {
case defaultExpr == nil || *defaultExpr == "":
if isNullableStr == "YES" {
columnDefaults[tableName][columnName] = "NULL"
} else {
// no default value and NOT NULL, i.e. a value must always be supplied explicitly
continue
}
case strings.Contains(*defaultExpr, "nextval"):
// default value is computed from a sequence object and must always be snapshotted
continue
default:
columnDefaults[tableName][columnName] = computeValueOfSQLExpression(t, db, *defaultExpr).Serialized
}
}
failOnErr(t, rows.Err())
failOnErr(t, rows.Close()) //nolint:sqlclosecheck
// snapshot all tables
result := make(dbSnapshot, len(tableNames))
for _, tableName := range tableNames {
result[tableName] = newTableSnapshot(t, db, tableName, keyColumnNames[tableName], columnDefaults[tableName])
}
return result
}
// ToSQL returns a set of SQL statements that reproduce this snapshot when
// starting from `prev`. If `prev` is nil, only INSERT statements will be
// returned.
func (d dbSnapshot) ToSQL(prev dbSnapshot) string {
tableNames := make([]string, len(d))
for tableName := range d {
tableNames = append(tableNames, tableName)
}
sort.Strings(tableNames)
results := make([]string, 0, len(tableNames))
for _, tableName := range tableNames {
var result string
tPrev, exists := prev[tableName]
if exists {
result = d[tableName].ToSQL(tableName, &tPrev)
} else {
result = d[tableName].ToSQL(tableName, nil)
}
if result != "" {
results = append(results, result)
}
}
return strings.Join(results, "\n")
}
////////////////////////////////////////////////////////////////////////////////
// type tableSnapshot
// tableSnapshot contains the state of a table.
type tableSnapshot struct {
ColumnNames []string
KeyColumnNames []string
ColumnDefaults map[string]string
// The map key is computed by rowSnapshot.Key().
Rows map[string]rowSnapshot
}
func newTableSnapshot(t *testing.T, db *sql.DB, tableName string, keyColumnNames []string, columnDefaults map[string]string) tableSnapshot {
t.Helper()
rows, err := db.Query(`SELECT * FROM ` + tableName) //nolint:gosec // cannot provide tableName as bind parameter
failOnErr(t, err)
columnNames, err := rows.Columns()
failOnErr(t, err)
// if there is no primary key or uniqueness constraint, use all columns as key
// (this means that diffs will only ever show INSERT and DELETE, not UPDATE)
if len(keyColumnNames) == 0 {
keyColumnNames = columnNames
}
// sort key columns in the same order as the columns themselves (this plays
// into sorting of keys and thus sorting of rows later on)
idxOfColumnName := make(map[string]int, len(columnNames))
for idx, columnName := range columnNames {
idxOfColumnName[columnName] = idx
}
sort.Slice(keyColumnNames, func(i, j int) bool {
return idxOfColumnName[keyColumnNames[i]] < idxOfColumnName[keyColumnNames[j]]
})
result := tableSnapshot{
ColumnNames: columnNames,
KeyColumnNames: keyColumnNames,
ColumnDefaults: columnDefaults,
Rows: make(map[string]rowSnapshot),
}
scanTarget := make([]any, len(columnNames))
for idx := range scanTarget {
scanTarget[idx] = &sqlValueSerializer{}
}
for rows.Next() {
failOnErr(t, rows.Scan(scanTarget...))
row := make(rowSnapshot, len(columnNames))
for idx, columnName := range columnNames {
row[columnName] = scanTarget[idx].(*sqlValueSerializer).Serialized
}
result.Rows[row.Key(result.KeyColumnNames)] = row
}
failOnErr(t, rows.Err())
failOnErr(t, rows.Close()) //nolint:sqlclosecheck
return result
}
// ToSQL returns a set of SQL statements that reproduce this snapshot when
// starting from `prev`. If `prev` is nil, only INSERT statements will be
// returned.
func (t tableSnapshot) ToSQL(tableName string, prev *tableSnapshot) string {
allRowKeys := make([]string, 0, len(t.Rows))
for key := range t.Rows {
allRowKeys = append(allRowKeys, key)
}
if prev != nil {
for key := range prev.Rows {
if _, exists := t.Rows[key]; !exists {
allRowKeys = append(allRowKeys, key)
}
}
}
sort.Strings(allRowKeys)
results := make([]string, len(allRowKeys))
for idx, key := range allRowKeys {
if prev == nil || prev.Rows[key] == nil {
results[idx] = t.Rows[key].ToSQLInsert(tableName, t.ColumnNames, t.ColumnDefaults)
continue
}
currRow := t.Rows[key]
if currRow == nil {
results[idx] = fmt.Sprintf("DELETE FROM %s WHERE %s;\n", tableName, key)
continue
}
columnDiff := currRow.ToSQLUpdateSet(t.ColumnNames, prev.Rows[key])
if columnDiff != "" {
results[idx] = fmt.Sprintf("UPDATE %s SET %s WHERE %s;\n", tableName, columnDiff, key)
}
}
return strings.Join(results, "")
}
////////////////////////////////////////////////////////////////////////////////
// type rowSnapshot
// rowSnapshot is a map with column names as keys and SQL literals as values.
type rowSnapshot map[string]string
// Key returns a serialization of this row's key column values as a SQL
// condition (e.g. `foo_id = 1 AND name = 'bar'`).
func (r rowSnapshot) Key(keyColumnNames []string) string {
exprs := make([]string, len(keyColumnNames))
for idx, columnName := range keyColumnNames {
exprs[idx] = fmt.Sprintf("%s = %s", columnName, r[columnName])
}
return strings.Join(exprs, " AND ")
}
// ToSQLInsert renders an INSERT statement that reproduces this row.
func (r rowSnapshot) ToSQLInsert(tableName string, columnNames []string, columnDefaults map[string]string) string {
displayColumnNames := make([]string, 0, len(columnNames))
displayValues := make([]string, 0, len(columnNames))
for _, columnName := range columnNames {
// values are only displayed if they deviate from the column's default value (or if there is no default value)
if columnDefaults[columnName] == "" || columnDefaults[columnName] != r[columnName] {
displayColumnNames = append(displayColumnNames, columnName)
displayValues = append(displayValues, r[columnName])
}
}
return fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s);\n",
tableName,
strings.Join(displayColumnNames, ", "),
strings.Join(displayValues, ", "),
)
}
// ToSQLUpdateSet renders the SET part of an UPDATE statement that produces this row out of `prev`.
func (r rowSnapshot) ToSQLUpdateSet(columnNames []string, prev rowSnapshot) string {
var results []string
for _, columnName := range columnNames {
value := r[columnName]
if prev[columnName] != value {
results = append(results, fmt.Sprintf("%s = %s", columnName, value))
}
}
return strings.Join(results, ", ")
}
////////////////////////////////////////////////////////////////////////////////
// type sqlValueSerializer
type sqlValueSerializer struct {
Serialized string
}
// Scan implements the sql.Scanner interface.
func (s *sqlValueSerializer) Scan(src any) error {
switch val := src.(type) {
case int64:
s.Serialized = fmt.Sprintf("%#v", val)
case float64:
s.Serialized = fmt.Sprintf("%#v", val)
case bool:
s.Serialized = "FALSE"
if val {
s.Serialized = "TRUE"
}
case []byte:
s.Serialized = fmt.Sprintf("'%s'", string(val))
case string:
s.Serialized = fmt.Sprintf("'%s'", val)
case time.Time:
s.Serialized = fmt.Sprintf("%#v", val.Unix())
default:
s.Serialized = "NULL"
}
return nil
}
////////////////////////////////////////////////////////////////////////////////
// func computeValueOfSQLExpression
var sqlExpressionCache = make(map[string]sqlValueSerializer)
func computeValueOfSQLExpression(t *testing.T, db *sql.DB, expr string) (result sqlValueSerializer) {
t.Helper()
result, exists := sqlExpressionCache[expr]
if !exists {
failOnErr(t, db.QueryRow("SELECT "+expr).Scan(&result))
sqlExpressionCache[expr] = result
}
return result
}