-
Notifications
You must be signed in to change notification settings - Fork 18
/
tarantool_migration.go
507 lines (464 loc) · 14.8 KB
/
tarantool_migration.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package Tt
import (
"errors"
"strings"
"github.com/alitto/pond"
"github.com/tarantool/go-tarantool/v2"
"github.com/kokizzu/gotro/A"
"github.com/kokizzu/gotro/L"
"github.com/kokizzu/gotro/S"
"github.com/kokizzu/gotro/X"
)
var DEBUG = false
var MIGRATION_THREAD = 8
var MIGRATION_QUEUE = 1000
var MIGRATION_REC_PER_BATCH = uint64(20_000)
var ErrMiscaonfiguration = errors.New(`misconfiguration`)
func Descr(args ...any) {
if DEBUG {
L.ParentDescribe(args...)
}
}
type TableName string
// types
type DataType string
const (
// https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#data-types
Unsigned DataType = `unsigned`
String DataType = `string`
Double DataType = `double`
Integer DataType = `integer`
Boolean DataType = `boolean`
Array DataType = `array`
//Any DataType = `any`
//Number DataType = `number` // use double instead
//Decimal DataType = `decimal` // unsupported
//DateTime DataType = `datetime` // unsupported
//Uuid DataType = `string` // `uuid` // https://github.com/tarantool/go-tarantool/v2/issues/90
//Scalar DataType = `scalar`
//Map DataType = `map`
//ArrayFloat DataType = `array`
//ArrayUnsigned DataType = `array`
//ArrayInteger DataType = `array`
)
var TypeToConst = map[DataType]string{
Unsigned: `Tt.Unsigned`,
String: `Tt.String`,
Integer: `Tt.Integer`,
Double: `Tt.Double`,
Boolean: `Tt.Boolean`,
Array: `Tt.Array`,
}
var TypeToGoType = map[DataType]string{
//Uuid: `string`,
Unsigned: `uint64`,
String: `string`,
Integer: `int64`,
Double: `float64`,
Boolean: `bool`,
Array: `[]any`,
}
var TypeToGoEmptyValue = map[DataType]string{
Unsigned: `0`,
String: "``",
Integer: `0`,
Double: `0`,
Boolean: `false`,
Array: `[]any{}`,
}
var TypeToGoNilValue = map[DataType]string{
Unsigned: `0`,
String: "``",
Integer: `0`,
Double: `0`,
Boolean: `false`,
Array: `nil`,
}
var TypeToConvertFunc = map[DataType]string{
Unsigned: `X.ToU`,
String: `X.ToS`,
Integer: `X.ToI`,
Double: `X.ToF`,
Boolean: `X.ToBool`,
Array: `X.ToArr`,
}
var Type2TarantoolDefault = map[DataType]string{
Unsigned: `0`,
String: `''`,
Integer: `0`,
Boolean: `false`,
Double: `0`,
Array: `[]`,
}
// misc
const Engine = `engine`
type EngineType string
const (
Vinyl EngineType = `vinyl`
Memtx EngineType = `memtx`
)
const BoxSpacePrefix = `box.space.`
const IfNotExists = `if_not_exists`
const IdCol = `id`
type TableProp struct {
Fields []Field
// indexes
Unique1 string
Unique2 string
Unique3 string
Uniques []string // multicolumn unique
Indexes []string
Spatial string
Engine EngineType
HiddenFields []string
AutoIncrementId bool // "id" column will be used to generate sequence, can only be created at beginning
GenGraphqlType bool
// hook
PreReformatMigrationHook func(*Adapter)
PreUnique1MigrationHook func(*Adapter)
PreUnique2MigrationHook func(*Adapter)
PreUnique3MigrationHook func(*Adapter)
PreUniquesMigrationHook func(*Adapter)
PreSpatialMigrationHook func(*Adapter)
AutoCensorFields []string // fields to automatically censor
}
type Field struct { // https://godoc.org/gopkg.in/vmihailenco/msgpack.v2#pkg-examples
Name string `msgpack:"name"`
Type DataType `msgpack:"type"`
}
func (f Field) KeyRenderer() func(string) string {
switch f.Type {
case Unsigned:
return func(structProp string) string {
return "tarantool.UintKey{I:uint(" + structProp + `)}`
}
case Integer:
return func(structProp string) string {
return "tarantool.IntKey{I:int(" + structProp + `)}`
}
case String:
return func(structProp string) string {
return "tarantool.StringKey{S:" + structProp + `}`
}
}
return func(structProp string) string {
return `DataTypeNotPossibleToBeAKey:` + string(f.Type) + `:` + structProp
}
}
type NullableField struct {
Name string `msgpack:"name"`
Type DataType `msgpack:"type"`
IsNullable bool `msgpack:"is_nullable"`
}
type Index struct {
Parts []string `msgpack:"parts"`
IfNotExists bool `msgpack:"if_not_exists"`
Unique bool `msgpack:"unique"`
Sequence string `msgpack:"sequence,omitempty"`
Type string `msgpack:"type,omitempty"`
}
type MSX map[string]any
func (a *Adapter) UpsertTable(tableName TableName, prop *TableProp) bool {
Descr(`---UpsertTable-Start-` + tableName + `----------------------------------------------------`)
defer Descr(`---UpsertTable-End-` + tableName + `-------------------------------------------------------`)
if prop.Engine == `` {
prop.Engine = Vinyl
}
Descr(`CreateSpace ` + tableName)
if !a.CreateSpace(string(tableName), prop.Engine) {
return false
}
if prop.PreReformatMigrationHook != nil {
Descr(`PreReformatMigrationHook`)
prop.PreReformatMigrationHook(a)
}
Descr(`ReformatTable ` + tableName)
if !a.ReformatTable(string(tableName), prop.Fields) {
return false // failed to create table
}
// create one field unique index
a.ExecBoxSpace(string(tableName)+`:format`, A.X{})
if prop.AutoIncrementId {
if len(prop.Fields) < 1 || prop.Fields[0].Name != IdCol || prop.Fields[0].Type != Unsigned {
panic(`must create Unsigned id field on first field to use AutoIncrementId`)
}
seqName := string(tableName) + `_` + IdCol
Descr(`box.schema.sequence.create ` + seqName)
a.ExecTarantoolVerbose(`box.schema.sequence.create`, A.X{
seqName,
})
Descr(string(tableName) + `:create_index ` + seqName)
a.ExecBoxSpace(string(tableName)+`:create_index`, A.X{
IdCol, Index{
Sequence: seqName,
Parts: []string{IdCol},
IfNotExists: true,
Unique: true,
},
})
}
if prop.PreUnique3MigrationHook != nil {
Descr(`PreUnique3MigrationHook`)
prop.PreUnique3MigrationHook(a)
}
// only create unique if not "id"
if prop.Unique1 != `` && !(prop.AutoIncrementId && prop.Unique1 == IdCol) {
Descr(string(tableName) + `:create_index ` + prop.Unique1)
a.ExecBoxSpace(string(tableName)+`:create_index`, A.X{
prop.Unique1, Index{Parts: []string{prop.Unique1}, IfNotExists: true, Unique: true},
})
if prop.Unique2 != `` && prop.Unique1 == prop.Unique2 {
panic(`Unique1 and Unique2 must be unique`)
}
if prop.Unique3 != `` && prop.Unique1 == prop.Unique3 {
panic(`Unique1 and Unique3 must be unique`)
}
}
if prop.PreUnique2MigrationHook != nil {
prop.PreUnique2MigrationHook(a)
}
if prop.Unique2 != `` && !(prop.AutoIncrementId && prop.Unique2 == IdCol) {
Descr(string(tableName) + `:create_index ` + prop.Unique2)
a.ExecBoxSpace(string(tableName)+`:create_index`, A.X{
prop.Unique2, Index{Parts: []string{prop.Unique2}, IfNotExists: true, Unique: true},
})
if prop.Unique3 != `` && prop.Unique2 == prop.Unique3 {
panic(`Unique2 and Unique3 must be unique`)
}
}
if prop.PreUnique3MigrationHook != nil {
Descr(`PreUnique3MigrationHook`)
prop.PreUnique3MigrationHook(a)
}
if prop.Unique3 != `` && !(prop.AutoIncrementId && prop.Unique3 == IdCol) {
Descr(string(tableName) + `:create_index ` + prop.Unique3)
a.ExecBoxSpace(string(tableName)+`:create_index`, A.X{
prop.Unique3, Index{Parts: []string{prop.Unique3}, IfNotExists: true, Unique: true},
})
}
if prop.PreUniquesMigrationHook != nil {
Descr(`PreUniquesMigrationHook`)
prop.PreUniquesMigrationHook(a)
}
// create multi-field unique index: [col1, col2] will named col1__col2
if len(prop.Uniques) > 1 {
Descr(string(tableName) + `:create_index ` + strings.Join(prop.Uniques, `__`))
a.ExecBoxSpace(string(tableName)+`:create_index`, A.X{
strings.Join(prop.Uniques, `__`), Index{Parts: prop.Uniques, IfNotExists: true, Unique: true},
})
}
if prop.PreSpatialMigrationHook != nil {
Descr(`PreSpatialMigrationHook`)
prop.PreSpatialMigrationHook(a)
}
// create spatial index (only works for memtx)
if prop.Spatial != `` {
Descr(string(tableName) + `:create_index ` + prop.Spatial)
a.ExecBoxSpace(string(tableName)+`:create_index`, A.X{
prop.Spatial, Index{Parts: []string{prop.Spatial}, IfNotExists: true, Type: `RTREE`},
})
}
// create other indexes
for _, index := range prop.Indexes {
Descr(string(tableName) + `:create_index ` + index)
//a.ExecBoxSpace(tableName+`.index.`+index+`:drop`, AX{index}) // TODO: remove this when index fixed
a.ExecBoxSpace(string(tableName)+`:create_index`, A.X{
index, Index{Parts: []string{index}, IfNotExists: true},
})
}
// need refresh index after migrate
// https://github.com/tarantool/go-tarantool/v2/pull/259#pullrequestreview-1242058107
a.Connection = a.Reconnect()
return true
}
// ignore return value
func (a *Adapter) ExecTarantool(funcName string, params A.X) bool {
return a.ExecTarantoolVerbose(funcName, params) == ``
}
func (a *Adapter) ExecTarantoolVerbose(funcName string, params A.X) string {
Descr(funcName)
Descr(params)
res, err := a.Connection.Do(tarantool.NewCallRequest(funcName).Args(params)).Get()
if err != nil {
if len(params) > 0 {
errStr := err.Error()
if errStr == `Space '`+X.ToS(params[0])+`' already exists` ||
errStr == `Sequence '`+X.ToS(params[0])+`' already exists` {
L.IsError(err, `ExecTarantool failed: `+funcName)
return err.Error()
}
}
if len(params) == 0 {
L.IsError(err, `ExecTarantool failed: `+funcName)
return err.Error()
}
}
Descr(res)
return ``
}
// ignore return value
func (a *Adapter) ExecBoxSpace(funcName string, params A.X) bool {
return a.ExecBoxSpaceVerbose(funcName, params) == ``
}
func (a *Adapter) ExecBoxSpaceVerbose(funcName string, params A.X) string {
Descr(funcName)
Descr(params)
res, err := a.Connection.Do(tarantool.NewCallRequest(BoxSpacePrefix + funcName).Args(params)).Get()
if L.IsError(err, `ExecBoxSpace failed: `+funcName) {
L.Describe(params)
return err.Error()
}
Descr(res)
return ``
}
// CallBoxSpace ignore return value
func (a *Adapter) CallBoxSpace(funcName string, params A.X) (rows [][]any) {
Descr(funcName)
Descr(params)
res, err := a.Connection.Do(tarantool.NewCallRequest(BoxSpacePrefix + funcName).Args(params)).Get()
Descr(res)
if L.IsError(err, `ExecBoxSpace failed: `+funcName) {
L.Describe(params)
return
}
rows = make([][]any, 0)
for _, row := range res {
if row, ok := row.([]any); ok {
rows = append(rows, row)
}
}
return
}
func (a *Adapter) DropTable(tableName string) bool {
return a.ExecBoxSpace(tableName+`:drop`, A.X{})
}
func (a *Adapter) TruncateTable(tableName string) bool {
return a.ExecBoxSpace(tableName+`:truncate`, A.X{})
}
func (a *Adapter) ReformatTable(tableName string, fields []Field) bool {
// check old schema
a.Connection = a.Reconnect() // need reconnect after creating space or a.Schema.Spaces will be empty
schema, err := tarantool.GetSchema(a.Connection)
if L.IsError(err, `GetSchema failed`) {
return false
}
table := schema.Spaces[tableName]
if len(table.Fields) == 0 { // new table, create anyway
return a.ExecBoxSpace(tableName+`:format`, A.X{
&fields,
}) // failed to create table
}
// table already exists
var newFields []NullableField
nullFields := map[string]DataType{}
haveIdField := false
for idx, newField := range fields { // diff and create nullable field
origField := table.FieldsById[uint32(idx)]
if origField.Type == string(newField.Type) {
newFields = append(newFields, NullableField{Name: newField.Name, Type: newField.Type})
} else {
newFields = append(newFields, NullableField{Name: newField.Name, Type: newField.Type, IsNullable: true})
nullFields[newField.Name] = newField.Type
}
if newField.Name == IdCol {
haveIdField = true
}
}
res := a.ExecBoxSpaceVerbose(tableName+`:format`, A.X{
&newFields,
})
if res != `` {
L.Describe(res)
}
a.Connection = a.Reconnect()
// update all column
if len(nullFields) > 0 {
updateCols := []string{}
for col, typ := range nullFields {
defaultvalue := Type2TarantoolDefault[typ]
if defaultvalue == `` {
panic(`please set Type2TarantoolDefault for: ` + typ)
}
updateCols = append(updateCols, dq(col)+` = `+defaultvalue)
}
updateQuery := `UPDATE ` + dq(tableName) + ` SET ` + A.StrJoin(updateCols, `, `)
if !haveIdField {
a.ExecSql(updateQuery) // update whole thing since there's no id field
} else {
var count uint64
a.QuerySql(`SELECT COUNT(*) FROM `+dq(tableName), func(row []any) {
count = X.ToU(row[0])
})
pool := pond.New(MIGRATION_THREAD, MIGRATION_QUEUE)
for i := uint64(0); i <= count; i += MIGRATION_REC_PER_BATCH {
i := i // for go <1.21
pool.Submit(func() {
if i+MIGRATION_REC_PER_BATCH >= count { // remaining
a.ExecSql(updateQuery + ` WHERE "id" >= ` + X.ToS(i))
} else {
a.ExecSql(updateQuery + ` WHERE "id" >= ` + X.ToS(i) + ` AND "id" < ` + X.ToS(i+MIGRATION_REC_PER_BATCH))
}
Descr(`Overwrite nulls ` + tableName + ` ` + X.ToS(i) + ` to ` + X.ToS(i+MIGRATION_REC_PER_BATCH) + ` of ` + X.ToS(count))
})
}
pool.StopAndWait()
a.ExecSql(updateQuery + ` WHERE "id" >= ` + X.ToS(count)) // update once more just in case
L.Print(`if next step is is with error 'Tuple field N (xxx) required by space format is missing', run this query: ` + updateQuery + ` WHERE "id" >= ` + X.ToS(count))
// can be caused by insert happened during migration
}
}
return a.ExecBoxSpace(tableName+`:format`, A.X{
&fields,
})
}
func (a *Adapter) CreateSpace(tableName string, engine EngineType) bool {
err := a.ExecTarantoolVerbose(`box.schema.space.create`, A.X{
tableName,
map[string]any{
Engine: string(engine),
//IfNotExists: true,
},
})
if err == `` || S.StartsWith(err, `unsupported Lua type 'function'`) {
return true // ignore
}
return true
}
func (a *Adapter) MigrateTables(tables map[TableName]*TableProp) {
for name, props := range tables {
Descr(name, props)
// validity check
if props.Engine == Vinyl && props.Spatial != `` {
L.PanicIf(ErrMiscaonfiguration, `spatial index is not supported in vinyl engine`)
}
a.UpsertTable(name, props)
}
}
func CheckTarantoolTables(tConn *Adapter, tables map[TableName]*TableProp) {
for tableName := range tables {
tableName := string(tableName)
res, err := tConn.Connection.Do(tarantool.NewCallRequest(`box.space.` + tableName + `:format`).Args([]any{})).Get()
L.PanicIf(err, `please run TABLE migration on %v for tarantool`, tableName)
if len(res) != 1 {
L.Panic(`please run FIELDS migration on %v for tarantool`, tableName)
}
columnNameTypes := map[string]string{}
if len(res) > 0 {
if rows, ok := res[0].([]any); ok {
for _, row := range rows {
m, ok := row.(map[any]any)
if !ok {
L.Panic(`please run FIELD migration on %v for tarantool: %v`, tableName, row)
}
columnNameTypes[X.ToS(m[`name`])] = X.ToS(m[`type`])
}
}
}
for _, field := range tables[TableName(tableName)].Fields {
existingType := columnNameTypes[field.Name]
if existingType != string(field.Type) {
L.Panic(`please run FIELD_TYPE %v migration on %v for tarantool: %v <> %v`, field.Name, tableName, existingType, field.Type)
}
}
}
}