forked from cjfinnell/pgverify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
verify.go
262 lines (200 loc) · 6.98 KB
/
verify.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
package pgverify
import (
"context"
"github.com/jackc/pgx/pgtype"
"github.com/jackc/pgx/v4"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.uber.org/multierr"
)
// Verify runs all verification tests for the given table, configured by
// the supplied Options.
func Verify(ctx context.Context, targets []*pgx.ConnConfig, opts ...Option) (*Results, error) {
c := NewConfig(opts...)
return c.Verify(ctx, targets)
}
// Verify runs all verification tests for the given table.
func (c Config) Verify(ctx context.Context, targets []*pgx.ConnConfig) (*Results, error) {
var finalResults *Results
if err := c.Validate(); err != nil {
return finalResults, err
}
c.Logger.Infof("Verifying %d targets", len(targets))
// First check that we can connect to every specified target database.
targetNames := make([]string, len(targets))
conns := make(map[int]*pgx.Conn)
for i, target := range targets {
pgxLoggerFields := logrus.Fields{
"component": "pgx",
"host": targets[i].Host,
"port": targets[i].Port,
"database": targets[i].Database,
"user": targets[i].User,
}
if len(c.Aliases) == len(targets) {
targetNames[i] = c.Aliases[i]
pgxLoggerFields["alias"] = c.Aliases[i]
} else {
targetNames[i] = targets[i].Host
}
target.Logger = &pgxLogger{c.Logger.WithFields(pgxLoggerFields)}
target.LogLevel = pgx.LogLevelError
conn, err := pgx.ConnectConfig(ctx, target)
if err != nil {
return finalResults, err
}
defer conn.Close(ctx)
conns[i] = conn
}
finalResults = NewResults(targetNames, c.TestModes)
// Then query each target database in parallel to generate table hashes.
var doneChannels []chan struct{}
for i, conn := range conns {
done := make(chan struct{})
go c.runTestsOnTarget(ctx, targetNames[i], conn, finalResults, done)
doneChannels = append(doneChannels, done)
}
for _, done := range doneChannels {
<-done
}
// Compare final results
reportErrors := finalResults.CheckForErrors()
if len(reportErrors) > 0 {
return finalResults, multierr.Combine(reportErrors...)
}
c.Logger.Info("Verification successful")
return finalResults, nil
}
func (c Config) runTestsOnTarget(ctx context.Context, targetName string, conn *pgx.Conn, finalResults *Results, done chan struct{}) {
logger := c.Logger.WithField("target", targetName)
schemaTableHashes, err := c.fetchTargetTableNames(ctx, conn)
if err != nil {
logger.WithError(err).Error("failed to fetch target tables")
close(done)
return
}
schemaTableHashes = c.runTestQueriesOnTarget(ctx, logger, conn, schemaTableHashes)
finalResults.AddResult(targetName, schemaTableHashes)
logger.Info("Table hashes computed")
close(done)
}
func (c Config) fetchTargetTableNames(ctx context.Context, conn *pgx.Conn) (SingleResult, error) {
schemaTableHashes := make(SingleResult)
rows, err := conn.Query(ctx, buildGetTablesQuery(c.IncludeSchemas, c.ExcludeSchemas, c.IncludeTables, c.ExcludeTables))
if err != nil {
return schemaTableHashes, errors.Wrap(err, "failed to query for tables")
}
for rows.Next() {
var schema, table pgtype.Text
if err := rows.Scan(&schema, &table); err != nil {
return schemaTableHashes, errors.Wrap(err, "failed to scan row data for table names")
}
if _, ok := schemaTableHashes[schema.String]; !ok {
schemaTableHashes[schema.String] = make(map[string]map[string]string)
}
schemaTableHashes[schema.String][table.String] = make(map[string]string)
for _, testMode := range c.TestModes {
schemaTableHashes[schema.String][table.String][testMode] = defaultErrorOutput
}
}
return schemaTableHashes, nil
}
func (c Config) validColumnTarget(columnName string) bool {
if len(c.IncludeColumns) == 0 {
for _, excludedColumn := range c.ExcludeColumns {
if excludedColumn == columnName {
return false
}
}
return true
}
for _, includedColumn := range c.IncludeColumns {
if includedColumn == columnName {
return true
}
}
return false
}
func (c Config) runTestQueriesOnTarget(ctx context.Context, logger *logrus.Entry, conn *pgx.Conn, schemaTableHashes SingleResult) SingleResult {
for schemaName, tables := range schemaTableHashes {
for tableName := range tables {
tableLogger := logger.WithField("table", tableName).WithField("schema", schemaName)
tableLogger.Info("Computing hash")
rows, err := conn.Query(ctx, buildGetColumsQuery(schemaName, tableName))
if err != nil {
tableLogger.WithError(err).Error("Failed to query column names, data types")
continue
}
allTableColumns := make(map[string]column)
for rows.Next() {
var columnName, dataType, constraintName, constraintType pgtype.Text
err := rows.Scan(&columnName, &dataType, &constraintName, &constraintType)
if err != nil {
tableLogger.WithError(err).Error("Failed to parse column names, data types from query response")
continue
}
existing, ok := allTableColumns[columnName.String]
if ok {
existing.constraints = append(existing.constraints, constraintType.String)
allTableColumns[columnName.String] = existing
} else {
allTableColumns[columnName.String] = column{columnName.String, dataType.String, []string{constraintType.String}}
}
}
var tableColumns []column
var primaryKeyColumnNames []string
for _, col := range allTableColumns {
if col.IsPrimaryKey() {
primaryKeyColumnNames = append(primaryKeyColumnNames, col.name)
}
if c.validColumnTarget(col.name) {
tableColumns = append(tableColumns, col)
}
}
if len(primaryKeyColumnNames) == 0 {
tableLogger.Error("No primary keys found")
continue
}
tableLogger.WithFields(logrus.Fields{
"primary_keys": primaryKeyColumnNames,
"columns": tableColumns,
}).Info("Determined columns to hash")
for _, testMode := range c.TestModes {
testLogger := tableLogger.WithField("test", testMode)
var query string
switch testMode {
case TestModeFull:
query = buildFullHashQuery(c, schemaName, tableName, tableColumns)
case TestModeBookend:
query = buildBookendHashQuery(c, schemaName, tableName, tableColumns, c.BookendLimit)
case TestModeSparse:
query = buildSparseHashQuery(c, schemaName, tableName, tableColumns, c.SparseMod)
case TestModeRowCount:
query = buildRowCountQuery(schemaName, tableName)
}
testLogger.Debugf("Generated query: %s", query)
testOutput, err := runTestOnTable(ctx, conn, query)
if err != nil {
testLogger.WithError(err).Error("Failed to compute hash")
continue
}
schemaTableHashes[schemaName][tableName][testMode] = testOutput
testLogger.Infof("Hash computed: %s", testOutput)
}
}
}
return schemaTableHashes
}
func runTestOnTable(ctx context.Context, conn *pgx.Conn, query string) (string, error) {
row := conn.QueryRow(ctx, query)
var testOutput pgtype.Text
if err := row.Scan(&testOutput); err != nil {
switch err {
case pgx.ErrNoRows:
return "no rows", nil
default:
return "", errors.Wrap(err, "failed to scan test output")
}
}
return testOutput.String, nil
}