-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrator.go
390 lines (340 loc) · 11.7 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
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
//go:generate mockgen -destination=internal/mocks/mock_db_for_migrator.go -package=mocks github.com/miyamo2/dynmgrm DBForMigrator
//go:generate mockgen -destination=internal/mocks/mock_base_migrator.go -package=mocks github.com/miyamo2/dynmgrm BaseMigrator
package dynmgrm
import (
"errors"
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"reflect"
"slices"
"strings"
)
var ErrDynmgrmAreNotSupported = errors.New("dynmgrm are not supported this operation")
// CapacityUnitsSpecifier could specify WCUs and RCU
type CapacityUnitsSpecifier interface {
WCU() int
RCU() int
}
// TableClass is the type of table class
type TableClass int
func (t TableClass) String() string {
switch t {
case TableClassStandard:
return "STANDARD"
case TableClassStandardIA:
return "STANDARD_IA"
}
return ""
}
// TableClassStandard and TableClassStandardIA are the supported table classes
const (
TableClassStandard TableClass = iota
TableClassStandardIA
)
// TableClassSpecifier could specify table class.
type TableClassSpecifier interface {
TableClass() TableClass
}
type DBForMigrator interface {
AddError(err error) error
Exec(sql string, values ...interface{}) (tx *gorm.DB)
}
// compatibility check
var _ gorm.Migrator = (*Migrator)(nil)
type BaseMigrator interface {
gorm.Migrator
RunWithValue(value interface{}, fc func(stmt *gorm.Statement) error) error
CurrentTable(stmt *gorm.Statement) interface{}
}
// Migrator is gorm.Migrator implementation for dynamodb
type Migrator struct {
db DBForMigrator
base BaseMigrator
}
func (m Migrator) CurrentDatabase() string {
return ""
}
func (m Migrator) AutoMigrate(dst ...interface{}) error {
return ErrDynmgrmAreNotSupported
}
func (m Migrator) FullDataTypeOf(field *schema.Field) clause.Expr {
return m.base.FullDataTypeOf(field)
}
func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
return []string{}
}
func (m Migrator) CreateTable(models ...interface{}) error {
for _, model := range models {
err := m.base.RunWithValue(model, func(stmt *gorm.Statement) (err error) {
var (
wcu, rcu int
tableClass string
)
if ws, ok := model.(CapacityUnitsSpecifier); ok {
wcu = ws.WCU()
rcu = ws.RCU()
}
if tcs, ok := model.(TableClassSpecifier); ok {
tableClass = tcs.TableClass().String()
}
rv := reflect.ValueOf(model)
var rt reflect.Type
switch rv.Kind() {
case reflect.Pointer:
rt = reflect.TypeOf(reflect.ValueOf(model).Elem().Interface())
default:
rt = reflect.TypeOf(model)
}
ddlBuilder := strings.Builder{}
ddlBuilder.WriteString(fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s`, m.currentTable(stmt)))
td := newDynmgrmTableDefine(rt)
// `CREATE TABLE` are proprietary PartiQL syntax by btnguyen2k/godynamo
// This is why place holder/bind variables are not used.
ddlBuilder.WriteString(fmt.Sprintf(` WITH PK=%s:%s`, td.PK.Name, td.PK.DataType))
if skn := td.SK.Name; skn != "" {
ddlBuilder.WriteString(fmt.Sprintf(`, WITH SK=%s:%s`, skn, td.SK.DataType))
}
opts := make([]string, 0, 7)
for k, v := range td.LSI {
lsi := fmt.Sprintf(`WITH LSI=%s:%s:%s`, k, v.SK.Name, v.SK.DataType)
projective := slices.DeleteFunc(td.NonKeyAttr, func(s string) bool {
if s == v.SK.Name {
return true
}
return slices.Contains(v.NonProjectiveAttrs, s)
})
if len(projective) > 0 {
lsi += fmt.Sprintf(`:%s`, strings.Join(projective, ","))
}
opts = append(opts, lsi)
}
if wcu > 0 {
opts = append(opts, fmt.Sprintf(`WITH wcu=%d`, wcu))
}
if rcu > 0 {
opts = append(opts, fmt.Sprintf(`WITH rcu=%d`, rcu))
}
if tableClass != "" {
opts = append(opts, fmt.Sprintf(`WITH table-class=%s`, tableClass))
}
for _, o := range opts {
ddlBuilder.WriteString(", ")
ddlBuilder.WriteString(o)
}
if err := m.db.Exec(ddlBuilder.String()).Error; err != nil {
return err
}
return nil
})
if err != nil {
return err
}
}
return nil
}
func (m Migrator) currentTable(stmt *gorm.Statement) string {
txp := m.base.CurrentTable(stmt)
if txp, ok := txp.(clause.Table); ok {
return txp.Name
}
return ""
}
func (m Migrator) DropTable(dst ...interface{}) error {
return ErrDynmgrmAreNotSupported
}
func (m Migrator) HasTable(dst interface{}) bool {
return false
}
// RenameTable See: [gorm.migrator.Migrator.RenameTable]
//
// Deprecated: DynamoDB does not possibly rename an existing table, so this operation is not supported.
//
// [gorm.migrator.Migrator.RenameTable]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.RenameTable
func (m Migrator) RenameTable(oldName, newName interface{}) error {
return ErrDynmgrmAreNotSupported
}
func (m Migrator) GetTables() (tableList []string, err error) {
return
}
func (m Migrator) TableType(dst interface{}) (gorm.TableType, error) {
return nil, ErrDynmgrmAreNotSupported
}
// AddColumn See: [gorm.migrator.Migrator.AddColumn]
//
// Deprecated: DynamoDB are schema less, so this operation is not supported.
//
// [gorm.migrator.Migrator.AlterColumn]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.AddColumn
func (m Migrator) AddColumn(dst interface{}, field string) error {
return ErrDynmgrmAreNotSupported
}
// DropColumn See: [gorm.migrator.Migrator.DropColumn]
//
// Deprecated: DynamoDB are schema less, so this operation is not supported.
//
// [gorm.migrator.Migrator.DropColumn]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.DropColumn
func (m Migrator) DropColumn(dst interface{}, field string) error {
return ErrDynmgrmAreNotSupported
}
// AlterColumn See: [gorm.migrator.Migrator.AlterColumn]
//
// Deprecated: DynamoDB are schema less, so this operation is not supported.
//
// [gorm.migrator.Migrator.AlterColumn]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.AlterColumn
func (m Migrator) AlterColumn(dst interface{}, field string) error {
return ErrDynmgrmAreNotSupported
}
// MigrateColumn See: [gorm.migrator.Migrator.MigrateColumn]
//
// Deprecated: DynamoDB are schema less, so this operation is not supported.
//
// [gorm.migrator.Migrator.MigrateColumn]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.MigrateColumn
func (m Migrator) MigrateColumn(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error {
return ErrDynmgrmAreNotSupported
}
// MigrateColumnUnique See: [gorm.migrator.Migrator.MigrateColumnUnique]
//
// Deprecated: DynamoDB are schema less, so this operation is not supported.
//
// [gorm.migrator.Migrator.MigrateColumnUnique]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.MigrateColumnUnique
func (m Migrator) MigrateColumnUnique(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error {
return ErrDynmgrmAreNotSupported
}
func (m Migrator) HasColumn(dst interface{}, field string) bool {
return false
}
// RenameColumn See: [gorm.migrator.Migrator.RenameColumn]
//
// Deprecated: DynamoDB are schema less, so this operation is not supported.
//
// [gorm.migrator.Migrator.RenameColumn]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.RenameColumn
func (m Migrator) RenameColumn(dst interface{}, oldName, field string) error {
return ErrDynmgrmAreNotSupported
}
func (m Migrator) ColumnTypes(dst interface{}) ([]gorm.ColumnType, error) {
return []gorm.ColumnType{}, ErrDynmgrmAreNotSupported
}
// CreateView See: [gorm.migrator.Migrator.CreateView]
//
// Deprecated: DynamoDB does not provide View feature, so this operation is not supported.
//
// [gorm.migrator.Migrator.CreateView]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.CreateView
func (m Migrator) CreateView(name string, option gorm.ViewOption) error {
return ErrDynmgrmAreNotSupported
}
// DropView See: [gorm.migrator.Migrator.DropView]
//
// Deprecated: DynamoDB does not provide View feature, so this operation is not supported.
//
// [gorm.migrator.Migrator.DropView]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.DropView
func (m Migrator) DropView(name string) error {
return ErrDynmgrmAreNotSupported
}
// CreateConstraint See: [gorm.migrator.Migrator.CreateConstraint]
//
// Deprecated: DynamoDB does not provide constraint feature, so this operation is not supported.
//
// [gorm.migrator.Migrator.CreateConstraint]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.CreateConstraint
func (m Migrator) CreateConstraint(dst interface{}, name string) error {
return ErrDynmgrmAreNotSupported
}
// DropConstraint See: [gorm.migrator.Migrator.DropConstraint]
//
// Deprecated: DynamoDB does not provide constraint feature, so this operation is not supported.
//
// [gorm.migrator.Migrator.DropConstraint]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.DropConstraint
func (m Migrator) DropConstraint(dst interface{}, name string) error {
return ErrDynmgrmAreNotSupported
}
// HasConstraint See: [gorm.migrator.Migrator.HasConstraint]
//
// Deprecated: DynamoDB does not provide constraint feature, so this operation is not supported.
//
// [gorm.migrator.Migrator.HasConstraint]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.HasConstraint
func (m Migrator) HasConstraint(dst interface{}, name string) bool {
return false
}
func (m Migrator) CreateIndex(dst interface{}, name string) error {
return m.base.RunWithValue(dst, func(stmt *gorm.Statement) (err error) {
var (
wcu, rcu int
)
if ws, ok := dst.(CapacityUnitsSpecifier); ok {
wcu = ws.WCU()
rcu = ws.RCU()
}
rv := reflect.ValueOf(dst)
var rt reflect.Type
switch rv.Kind() {
case reflect.Pointer:
rt = reflect.TypeOf(reflect.ValueOf(dst).Elem().Interface())
default:
rt = reflect.TypeOf(dst)
}
td := newDynmgrmTableDefine(rt)
if name != "" {
v, ok := td.GSI[name]
if !ok {
return fmt.Errorf("gsi '%s' is not defined in %T", name, dst)
}
td.GSI = map[string]*dynmgrmSecondaryIndexDefine{name: v}
}
for k, v := range td.GSI {
// `CREATE GSI` are proprietary PartiQL syntax by btnguyen2k/godynamo
// This is why place holder/bind variables are not used.
ddlBuilder := strings.Builder{}
ddlBuilder.WriteString(fmt.Sprintf(`CREATE GSI IF NOT EXISTS %s ON %s `, k, m.currentTable(stmt)))
ddlBuilder.WriteString(fmt.Sprintf(`WITH PK=%s:%s`, v.PK.Name, v.PK.DataType))
if skn := v.SK.Name; skn != "" {
ddlBuilder.WriteString(fmt.Sprintf(`, WITH SK=%s:%s`, skn, v.SK.DataType))
}
opts := make([]string, 0, 3)
if wcu > 0 {
opts = append(opts, fmt.Sprintf(`WITH wcu=%d`, wcu))
}
if rcu > 0 {
opts = append(opts, fmt.Sprintf(`WITH rcu=%d`, rcu))
}
projective := make([]string, 0)
if len(v.NonProjectiveAttrs) == 0 {
projective = append(projective, "*")
} else {
projective = slices.DeleteFunc(append(td.NonKeyAttr, td.PK.Name, td.SK.Name), func(s string) bool {
if s == v.PK.Name || s == v.SK.Name {
return true
}
return slices.Contains(v.NonProjectiveAttrs, s)
})
}
if len(projective) > 0 {
opts = append(opts, fmt.Sprintf(`WITH projection=%s`, strings.Join(projective, ",")))
}
for _, o := range opts {
ddlBuilder.WriteString(", ")
ddlBuilder.WriteString(o)
}
if err := m.db.Exec(ddlBuilder.String()).Error; err != nil {
return err
}
}
return nil
})
}
func (m Migrator) DropIndex(dst interface{}, name string) error {
return ErrDynmgrmAreNotSupported
}
func (m Migrator) HasIndex(dst interface{}, name string) bool {
return false
}
// RenameIndex See: [gorm.migrator.Migrator.RenameIndex]
//
// Deprecated: DynamoDB does not possibly rename an existing index, so this operation is not supported.
//
// [gorm.migrator.Migrator.RenameIndex]: https://pkg.go.dev/gorm.io/gorm/migrator#Migrator.RenameIndex
func (m Migrator) RenameIndex(dst interface{}, oldName, newName string) error {
return ErrDynmgrmAreNotSupported
}
func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error) {
return []gorm.Index{}, ErrDynmgrmAreNotSupported
}