-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexes.go
151 lines (145 loc) · 4.79 KB
/
indexes.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
package migrate
import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
type SecondaryIndex struct {
Name string `json:"IndexName" yaml:"IndexName"`
Keys Keys `json:"KeySchema" yaml:"KeySchema"`
Projection *Projection `json:"Projection" yaml:"Projection"`
Throughput *ProvisionedThroughput `json:"ProvisionedThroughput,omitempty" yaml:"ProvisionedThroughput,omitempty"`
}
func (s SecondaryIndex) CreateGlobal() types.GlobalSecondaryIndex {
g := types.GlobalSecondaryIndex{
IndexName: aws.String(s.Name),
}
g.KeySchema = s.Keys.Elements()
if s.Projection != nil {
g.Projection = s.Projection.Element()
}
if s.Throughput != nil {
g.ProvisionedThroughput = s.Throughput.Element()
}
return g
}
func (s SecondaryIndex) CreateLocal() types.LocalSecondaryIndex {
local := types.LocalSecondaryIndex{
IndexName: aws.String(s.Name),
}
local.KeySchema = s.Keys.Elements()
if s.Projection != nil {
local.Projection = func() *types.Projection {
if s.Projection != nil {
return s.Projection.Element()
}
return nil
}()
}
return local
}
type SecondaryIndexes []SecondaryIndex
func (indexes SecondaryIndexes) GlobalIndexes() []types.GlobalSecondaryIndex {
array := make([]types.GlobalSecondaryIndex, 0, len(indexes))
for _, v := range indexes {
array = append(array, v.CreateGlobal())
}
return array
}
func (indexes SecondaryIndexes) LocalIndexes() []types.LocalSecondaryIndex {
array := make([]types.LocalSecondaryIndex, 0, len(indexes))
for _, v := range indexes {
array = append(array, v.CreateLocal())
}
return array
}
func (indexes SecondaryIndexes) UpdateGlobals(desc types.TableDescription) []types.GlobalSecondaryIndexUpdate {
updates := make([]types.GlobalSecondaryIndexUpdate, 0, 2)
if desc.GlobalSecondaryIndexes != nil && len(desc.GlobalSecondaryIndexes) > 0 { // 既存のGlobalSecondaryIndexが存在する場合
if len(indexes) > 0 { // 更新
org := make(map[string]types.GlobalSecondaryIndexDescription)
for _, v := range desc.GlobalSecondaryIndexes {
org[*v.IndexName] = v
}
for _, newIndex := range indexes {
var updateProvision *types.ProvisionedThroughput
for _, orgIndex := range desc.GlobalSecondaryIndexes {
if *orgIndex.IndexName == newIndex.Name {
newReadCapacity, newWriteCapacity := int64(0), int64(0)
if newIndex.Throughput != nil {
newReadCapacity = newIndex.Throughput.Read
newWriteCapacity = newIndex.Throughput.Write
}
if *orgIndex.ProvisionedThroughput.ReadCapacityUnits != newReadCapacity {
updateProvision = newIndex.Throughput.Element()
break
} else if *orgIndex.ProvisionedThroughput.WriteCapacityUnits != newWriteCapacity {
updateProvision = newIndex.Throughput.Element()
break
}
}
}
if updateProvision != nil { // 更新
delete(org, newIndex.Name)
updates = append(updates, types.GlobalSecondaryIndexUpdate{
Update: &types.UpdateGlobalSecondaryIndexAction{
IndexName: aws.String(newIndex.Name),
ProvisionedThroughput: updateProvision,
},
})
updateProvision = nil
} else { // 追加
var throughput *types.ProvisionedThroughput
if newIndex.Throughput != nil {
throughput = newIndex.Throughput.Element()
}
updates = append(updates, types.GlobalSecondaryIndexUpdate{
Create: &types.CreateGlobalSecondaryIndexAction{
IndexName: aws.String(newIndex.Name),
KeySchema: newIndex.Keys.Elements(),
Projection: newIndex.Projection.Element(),
ProvisionedThroughput: throughput,
},
})
}
for k := range org { // 削除
updates = append(updates, types.GlobalSecondaryIndexUpdate{
Delete: &types.DeleteGlobalSecondaryIndexAction{IndexName: aws.String(k)},
})
}
}
} else { // GlobalIndex全削除する
for _, v := range desc.GlobalSecondaryIndexes {
updates = append(updates, types.GlobalSecondaryIndexUpdate{
Delete: &types.DeleteGlobalSecondaryIndexAction{IndexName: v.IndexName},
})
}
}
} else { // GlobalIndexを新規に追加
if len(indexes) > 0 {
for _, v := range indexes {
updates = append(updates, types.GlobalSecondaryIndexUpdate{
Create: &types.CreateGlobalSecondaryIndexAction{
IndexName: aws.String(v.Name),
KeySchema: v.Keys.Elements(),
Projection: func() *types.Projection {
if v.Projection != nil {
return v.Projection.Element()
}
return nil
}(),
ProvisionedThroughput: func() *types.ProvisionedThroughput {
if v.Throughput != nil {
return v.Throughput.Element()
}
return nil
}(),
},
})
}
}
}
if len(updates) > 0 {
return updates
}
return nil
}