/
tables.go
175 lines (125 loc) · 3.57 KB
/
tables.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
package dynamodb
import (
"context"
"fmt"
"log/slog"
"time"
"github.com/aws/aws-sdk-go/aws"
aws_dynamodb "github.com/aws/aws-sdk-go/service/dynamodb"
)
// CreateTablesOptions defines options for the CreateTables method
type CreateTablesOptions struct {
// A hash map containing table names and their dynamodb.CreateTableInput defintions
Tables map[string]*aws_dynamodb.CreateTableInput
// If true and the table already exists, delete and recreate the table
Refresh bool
}
// Create one or more tables associated with the dynamodb.DynamoDB instance.
func CreateTables(client *aws_dynamodb.DynamoDB, opts *CreateTablesOptions) error {
for table_name, def := range opts.Tables {
// To do: Do this concurrently because of the delay waiting for table deletion to complete
has_table, err := HasTable(client, table_name)
if err != nil {
return fmt.Errorf("Failed to determined whether table exists, %w", err)
}
if has_table {
if !opts.Refresh {
continue
}
req := &aws_dynamodb.DeleteTableInput{
TableName: aws.String(table_name),
}
_, err := client.DeleteTable(req)
if err != nil {
return fmt.Errorf("Failed to delete table '%s', %w", table_name, err)
}
// Now wait for the deletion to complete...
slog.Debug("Table deleted, now waiting for completion", "table_name", table_name)
ctx := context.Background()
ticker_ctx, ticker_cancel := context.WithTimeout(ctx, 30*time.Second)
defer ticker_cancel()
ticker := time.NewTicker(5 * time.Second)
done_ch := make(chan bool)
ready_ch := make(chan bool)
err_ch := make(chan error)
go func() {
for {
select {
case <-ticker_ctx.Done():
return
case <-done_ch:
return
case <-ticker.C:
has_table, err := HasTable(client, table_name)
if err != nil {
slog.Error("Failed to determine if table exists", "table name", table_name, "error", err)
} else {
slog.Debug("Has table check", "table name", table_name, "has_table", has_table)
}
if !has_table {
ready_ch <- true
}
}
}
}()
table_ready := false
for {
select {
case <-ticker_ctx.Done():
return fmt.Errorf("Ticker to delete table timed out")
case err := <-err_ch:
return fmt.Errorf("Failed to delete table, %w", err)
case <-ready_ch:
table_ready = true
}
if table_ready {
break
go func() {
ticker.Stop()
done_ch <- true
}()
}
}
}
def.TableName = aws.String(table_name)
_, err = client.CreateTable(def)
if err != nil {
return fmt.Errorf("Failed to create table '%s', %w", table_name, err)
}
}
return nil
}
// Return a boolean value indication whether or not the dynamodb.DynamoDB instances contains a table matching table_name.
func HasTable(client *aws_dynamodb.DynamoDB, table_name string) (bool, error) {
tables, err := ListTables(client)
if err != nil {
return false, err
}
has_table := false
for _, name := range tables {
if name == table_name {
has_table = true
break
}
}
return has_table, nil
}
// Return the list of table names associated with the dynamodb.DynamoDB instance.
func ListTables(client *aws_dynamodb.DynamoDB) ([]string, error) {
tables := make([]string, 0)
input := &aws_dynamodb.ListTablesInput{}
for {
rsp, err := client.ListTables(input)
if err != nil {
return nil, err
}
for _, n := range rsp.TableNames {
tables = append(tables, *n)
}
input.ExclusiveStartTableName = rsp.LastEvaluatedTableName
if rsp.LastEvaluatedTableName == nil {
break
}
}
return tables, nil
}