forked from vmware-archive/pg2mysql
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrator.go
296 lines (244 loc) · 7.13 KB
/
migrator.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
package pg2mysql
import (
"database/sql"
"errors"
"fmt"
"strings"
)
type Migrator interface {
Migrate() error
}
func NewMigrator(src, dst DB, truncateFirst bool, workers int, watcher MigratorWatcher) Migrator {
return &migrator{
src: src,
dst: dst,
truncateFirst: truncateFirst,
workers: workers,
watcher: watcher,
}
}
type migrator struct {
src, dst DB
truncateFirst bool
workers int
watcher MigratorWatcher
}
func (m *migrator) Migrate() error {
srcSchema, err := BuildSchema(m.src)
if err != nil {
return fmt.Errorf("failed to build source schema: %s", err)
}
m.watcher.WillDisableConstraints()
err = m.dst.DisableConstraints()
if err != nil {
return fmt.Errorf("failed to disable constraints: %s", err)
}
m.watcher.DidDisableConstraints()
defer func() {
m.watcher.WillEnableConstraints()
err = m.dst.EnableConstraints()
if err != nil {
m.watcher.EnableConstraintsDidFailWithError(err)
} else {
m.watcher.EnableConstraintsDidFinish()
}
}()
jobs := make(chan *Table, len(srcSchema.Tables))
results := make(chan error, len(srcSchema.Tables))
for w := 1; w <= m.workers; w++ {
go migrateWorker(w, m, jobs, results)
}
for _, table := range srcSchema.Tables {
jobs <- table
}
close(jobs)
migrationErrs := new(strings.Builder)
for _ = range srcSchema.Tables {
err := <-results
if err != nil {
migrationErrs.WriteString(fmt.Sprintf(" %s\n", err))
}
}
if migrationErrs.Len() > 0 {
return fmt.Errorf("migration failed with error(s):\n%s", migrationErrs)
}
return nil
}
func migrateWorker(id int, m *migrator, jobs <-chan *Table, results chan<- error) {
for j := range jobs {
var watcher MigratorWatcher
buffer := new(strings.Builder)
if m.workers > 1 {
watcher = NewPrinter(buffer) // this currently breaks the idea of a MigratorWatcher
} else {
watcher = m.watcher
}
err := migrateTable(m.truncateFirst, watcher, m.src, m.dst, j)
if buffer.Len() > 0 {
fmt.Print(buffer.String())
}
results <- err
}
}
func migrateTable(truncate bool, watcher MigratorWatcher, src DB, dst DB, table *Table) error {
var err error
if truncate {
watcher.WillTruncateTable(table.Name)
_, err := dst.DB().Exec(fmt.Sprintf("TRUNCATE TABLE %s", table.Name))
if err != nil {
return fmt.Errorf("failed truncating: %s", err)
}
watcher.TruncateTableDidFinish(table.Name)
}
var recordsInserted int64
watcher.TableMigrationDidStart(table.Name)
if table.HasColumn("id") {
recordsInserted, err = migrateWithIDs(src, dst, table)
if err != nil {
return fmt.Errorf("failed migrating table with ids: %s", err)
}
} else {
preparedStmt, err := preparedBulkInsert(dst, table, 1)
if err != nil {
return fmt.Errorf("failed creating prepared bulk insert statement: %s", err)
}
err = EachMissingRow(src, dst, table, func(scanArgs []interface{}) error {
err := insert(preparedStmt, scanArgs)
if err != nil {
return fmt.Errorf("failed to insert into %s: %s\n", table.Name, err)
}
recordsInserted++
return nil
})
if err != nil {
return fmt.Errorf("failed migrating table without ids: %s", err)
}
}
watcher.TableMigrationDidFinish(table.Name, recordsInserted)
return nil
}
func migrateWithIDs(src DB, dst DB, table *Table) (int64, error) {
columnNamesForSelect := make([]string, len(table.Columns))
batchSize := 1000
var scanArgs, scanValues, values []interface{}
var preparedStmt *sql.Stmt
var preparedStmtSize int // number of rows the prepared statment can handle
var count int64 // number of rows inserted
for i := range table.Columns {
columnNamesForSelect[i] = table.Columns[i].Name
}
// find ids already in dst
rows, err := dst.DB().Query(fmt.Sprintf("SELECT id FROM %s", table.Name))
if err != nil {
return 0, fmt.Errorf("failed to select id from rows: %s", err)
}
var dstIDs []interface{}
for rows.Next() {
var id interface{}
if err = rows.Scan(&id); err != nil {
return 0, fmt.Errorf("failed to scan id from row: %s", err)
}
dstIDs = append(dstIDs, id)
}
if err = rows.Err(); err != nil {
return 0, fmt.Errorf("failed iterating through rows: %s", err)
}
if err = rows.Close(); err != nil {
return 0, fmt.Errorf("failed closing rows: %s", err)
}
// select data for ids to migrate from src
stmt := fmt.Sprintf(
"SELECT %s FROM %s",
strings.Join(columnNamesForSelect, ","),
table.Name,
)
if len(dstIDs) > 0 {
placeholders := make([]string, len(dstIDs))
for i := range dstIDs {
placeholders[i] = fmt.Sprintf("$%d", i+1)
}
stmt = fmt.Sprintf("%s WHERE id NOT IN (%s)", stmt, strings.Join(placeholders, ","))
}
rows, err = src.DB().Query(stmt, dstIDs...)
if err != nil {
return 0, fmt.Errorf("failed to select rows: %s", err)
}
tx, err := dst.DB().Begin()
if err != nil {
return 0, fmt.Errorf("failed begin transaction: %s", err)
}
defer tx.Commit()
hasNext := rows.Next()
for hasNext {
scanArgs = make([]interface{}, len(table.Columns))
scanValues = make([]interface{}, len(table.Columns))
for i := range table.Columns {
scanArgs[i] = &scanValues[i]
}
if err = rows.Scan(scanArgs...); err != nil {
return count, fmt.Errorf("failed to scan row: %s", err)
}
values = append(values, scanArgs...)
hasNext = rows.Next()
size := len(values) / len(table.Columns)
if size >= batchSize || !hasNext {
if size != preparedStmtSize || preparedStmt == nil {
preparedStmt, err = preparedBulkInsert(dst, table, size)
preparedStmtSize = size
if err != nil {
return count, fmt.Errorf("failed creating prepared bulk insert statement for batch: %s", err)
}
}
err = insert(tx.Stmt(preparedStmt), values)
values = nil
if err != nil {
return count, fmt.Errorf("failed to insert into %s: %s\n", table.Name, err)
}
count += int64(size)
}
}
if err = rows.Err(); err != nil {
return count, fmt.Errorf("failed iterating through rows: %s", err)
}
if err = rows.Close(); err != nil {
return count, fmt.Errorf("failed closing rows: %s", err)
}
return count, nil
}
func insert(stmt *sql.Stmt, values []interface{}) error {
result, err := stmt.Exec(values...)
if err != nil {
return fmt.Errorf("failed to exec stmt: %s", err)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("failed getting rows affected by insert: %s", err)
}
if rowsAffected == 0 {
return errors.New("no rows affected by insert")
}
return nil
}
func preparedBulkInsert(db DB, table *Table, size int) (*sql.Stmt, error) {
columnNamesForInsert := make([]string, len(table.Columns))
placeholders := make([]string, len(table.Columns))
values := make([]string, size)
for i := range table.Columns {
columnNamesForInsert[i] = fmt.Sprintf("`%s`", table.Columns[i].Name)
placeholders[i] = "?"
}
holders := strings.Join(placeholders, ",")
for i := 0; i < size; i++ {
values[i] = fmt.Sprintf("(%s)", holders)
}
preparedStmt, err := db.DB().Prepare(fmt.Sprintf(
"INSERT INTO %s (%s) VALUES %s",
table.Name,
strings.Join(columnNamesForInsert, ","),
strings.Join(values, ","),
))
if err != nil {
return preparedStmt, fmt.Errorf("failed creating prepared statement: %s", err)
}
return preparedStmt, nil
}