forked from polothy/pg2mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatcher.go
152 lines (119 loc) · 3.3 KB
/
watcher.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
package pg2mysql
import (
"fmt"
"io"
"os"
"strings"
)
//go:generate counterfeiter . VerifierWatcher
type VerifierWatcher interface {
TableVerificationDidStart(tableName string)
TableVerificationDidFinish(tableName string, missingRows int64, missingIDs []string)
TableVerificationDidFinishWithError(tableName string, err error)
}
//go:generate counterfeiter . MigratorWatcher
type MigratorWatcher interface {
WillBuildSchema()
DidBuildSchema()
WillDisableConstraints()
DidDisableConstraints()
WillEnableConstraints()
EnableConstraintsDidFinish()
EnableConstraintsDidFailWithError(err error)
WillTruncateTable(tableName string)
TruncateTableDidFinish(tableName string)
TableMigrationDidStart(tableName string)
TableMigrationDidFinish(tableName string, recordsInserted int64)
DidMigrateRow(tableName string)
DidFailToMigrateRowWithError(tableName string, err error)
}
func NewStdoutPrinter() *StdoutPrinter {
return NewPrinter(os.Stdout)
}
func NewPrinter(w io.Writer) *StdoutPrinter {
return &StdoutPrinter{
writer: w,
}
}
type StdoutPrinter struct {
writer io.Writer
}
func (s *StdoutPrinter) write(format string, a ...interface{}) {
fmt.Fprintf(s.writer, format, a...)
}
func (s *StdoutPrinter) writeln(format string, a ...interface{}) {
s.write(format, a...)
s.write("\n")
}
func (s *StdoutPrinter) TableVerificationDidStart(tableName string) {
s.write("Verifying table %s...", tableName)
}
func (s *StdoutPrinter) TableVerificationDidFinish(tableName string, missingRows int64, missingIDs []string) {
if missingRows != 0 {
if missingRows == 1 {
s.writeln("\n\tFAILED: 1 row missing")
} else {
s.write("\n\tFAILED: %d rows missing\n", missingRows)
}
if missingIDs != nil {
s.write("\tMissing IDs: %v\n", strings.Join(missingIDs, ","))
}
} else {
s.done()
}
}
func (s *StdoutPrinter) done() {
s.writeln("OK")
}
func (s *StdoutPrinter) TableVerificationDidFinishWithError(tableName string, err error) {
s.write("failed: %s", err)
}
func (s *StdoutPrinter) WillBuildSchema() {
s.write("Building schema...")
}
func (s *StdoutPrinter) DidBuildSchema() {
s.done()
}
func (s *StdoutPrinter) WillDisableConstraints() {
s.write("Disabling constraints...")
}
func (s *StdoutPrinter) DidDisableConstraints() {
s.done()
}
func (s *StdoutPrinter) DidFailToDisableConstraints(err error) {
s.done()
}
func (s *StdoutPrinter) WillEnableConstraints() {
s.write("Enabling constraints...")
}
func (s *StdoutPrinter) EnableConstraintsDidFailWithError(err error) {
s.write("failed: %s", err)
}
func (s *StdoutPrinter) EnableConstraintsDidFinish() {
s.done()
}
func (s *StdoutPrinter) WillTruncateTable(tableName string) {
s.write("Truncating %s...", tableName)
}
func (s *StdoutPrinter) TruncateTableDidFinish(tableName string) {
s.done()
}
func (s *StdoutPrinter) TableMigrationDidStart(tableName string) {
s.write("Migrating %s...", tableName)
}
func (s *StdoutPrinter) TableMigrationDidFinish(tableName string, recordsInserted int64) {
switch recordsInserted {
case 0:
s.writeln("OK (0 records inserted)")
case 1:
s.writeln("OK\n inserted 1 row")
default:
s.write("OK\n inserted %d rows\n", recordsInserted)
}
}
func (s *StdoutPrinter) DidMigrateRow(tableName string) {
s.write(".")
}
func (s *StdoutPrinter) DidFailToMigrateRowWithError(tableName string, err error) {
s.write("x")
}