-
Notifications
You must be signed in to change notification settings - Fork 1
/
migration.go
296 lines (246 loc) · 8.5 KB
/
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
/*
Copyright 2023 Adarga Limited
Licensed under the Apache License, Version 2.0 (the "License"). You may not use
this file except in compliance with the License. You may obtain a copy of the
License at:
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package tigergraph
import (
"context"
_ "embed"
"errors"
"fmt"
"os"
"sort"
"strconv"
"strings"
"time"
)
const (
// MetadataGraphName is the name of the graph the client stores metadata in
MetadataGraphName = "ClientMetadata"
// ExpectedFailurePrefix is the start of the error received when the client has not initialised the metadata
ExpectedFailurePrefix = "Graph name " + MetadataGraphName + " cannot be found."
trackMigrationFailureTemplate = "failed to commit migration version to metadata graph.\n" +
"IMPORTANT: this requires manual intervention. This migration record failed to be set,\n" +
"but the migration was run successfully. The easiest resolution to this is to set the init version\n" +
"to be the migration version printed in this log, as that will ensure the new migration vertex is added\n" +
"and will skip the version.\nI.e. set TIGER_GRAPH_MIGRATION_INIT_VERSION=%s as an env var. Original error: %w"
)
var (
// ErrUnknownInitialisationCheckFailure occurs when an error is returned, but it is not the
// error which signals that initialisation hasn't happened yet
ErrUnknownInitialisationCheckFailure = errors.New("initialisation check failed for an unknown reason")
// ErrTigerGraphSchemaSetUpFailed means that a failure occurred when running migrations
ErrTigerGraphSchemaSetUpFailed = errors.New("failed to set up schema in tiger graph")
// ErrInvalidMigrationNumber means that a supplied migration number was invalid
ErrInvalidMigrationNumber = errors.New("migration number was invalid")
)
// CheckIsInitialised determines if the metadata graph has been initialised
// and ready for use.
func (c *TigerGraphClient) CheckIsInitialised(ctx context.Context) (bool, error) {
meta, err := c.GetGraphMetadata(ctx, MetadataGraphName)
if err != nil {
return false, err
}
if !meta.Error && meta.Results.GraphName == MetadataGraphName {
return true, nil
}
if strings.HasPrefix(meta.Message, ExpectedFailurePrefix) {
return false, nil
}
return false, ErrUnknownInitialisationCheckFailure
}
// InitFileString is the content of the initialisation GSQL as a string
//
//go:embed gsql/metadata_init.gsql
var InitFileString string
// Migrate checks the status of migrations in the metadata graph and uses that
// information along with the specified version to determine which migrations to
// run.
//
// If the metadata graph does not yet exist, it is created and initialised.
func (c *TigerGraphClient) Migrate(
ctx context.Context,
graph string,
version string,
initVersion string,
migrationFileDir string,
dryRun bool,
) error {
isInitialised, err := c.CheckIsInitialised(ctx)
if err != nil {
return err
}
if !isInitialised {
if err = c.RunGSQL(ctx, InitFileString); err != nil {
return err
}
initialVersion := initVersion
if initialVersion != "" {
migrationNumbers, migrationMode, err := getMigrationsBetweenVersions("", initialVersion)
if err != nil {
return fmt.Errorf(
"failed to determine the initial migrations to run: initialVersion: %s, %w",
initialVersion,
err,
)
}
for _, migrationNumber := range migrationNumbers {
if err = c.commitMigrationVersion(ctx, graph, migrationNumber, migrationMode); err != nil {
return fmt.Errorf("failed to commit migration number: migrationNumber: %s, %w", migrationNumber, err)
}
}
}
}
currentMigrationNumber, err := c.GetCurrentMigrationNumber(ctx, graph)
if err != nil {
return fmt.Errorf("failed to get current migration number from TigerGraph: %w", err)
}
desiredMigrationNumber := version
migrationNumbers, migrationMode, err := getMigrationsBetweenVersions(currentMigrationNumber, desiredMigrationNumber)
if err != nil {
return err
}
for _, migrationNumber := range migrationNumbers {
if dryRun {
continue
}
if err = c.tryMigrateStep(ctx, migrationNumber, migrationMode, migrationFileDir); err != nil {
return err
}
if err = c.commitMigrationVersion(ctx, graph, migrationNumber, migrationMode); err != nil {
return fmt.Errorf(trackMigrationFailureTemplate, migrationNumber, err)
}
}
return nil
}
func decrementMigrationNumber(n string) (string, error) {
asInt, err := strconv.ParseInt(n, 10, 32)
if err != nil {
return "", ErrInvalidMigrationNumber
}
return fmt.Sprintf("%03d", asInt-1), nil
}
func getMigrationsBetweenVersions(from string, to string) ([]string, string, error) {
result := make([]string, 0)
if to == "" {
return result, "", ErrInvalidMigrationNumber
}
if from == "" {
from = "-001"
}
fromInt, err := strconv.ParseInt(from, 10, 32)
if err != nil {
return result, "", ErrInvalidMigrationNumber
}
toInt, err := strconv.ParseInt(to, 10, 32)
if err != nil {
return result, "", ErrInvalidMigrationNumber
}
minInt := fromInt
if toInt < minInt {
minInt = toInt
}
maxInt := toInt
if fromInt > maxInt {
maxInt = fromInt
}
for i := minInt + 1; i <= maxInt; i++ {
versionString := fmt.Sprintf("%03d", i)
result = append(result, versionString)
}
mode := "up"
if fromInt > toInt {
mode = "down"
sort.Slice(result, func(i, j int) bool {
return result[i] > result[j]
})
}
return result, mode, nil
}
func (c *TigerGraphClient) tryMigrateStep(ctx context.Context, number string, mode string, migrationFileDir string) error {
files, err := os.ReadDir(migrationFileDir)
if err != nil {
return err
}
expectedSuffix := fmt.Sprintf("%s.gsql", mode)
for _, file := range files {
if strings.HasPrefix(file.Name(), number+"_") && strings.HasSuffix(file.Name(), expectedSuffix) {
fileName := migrationFileDir + "/" + file.Name()
err = c.migrateFile(ctx, fileName)
if err != nil {
return fmt.Errorf("failed to set up TG schema: %s, %w", err, ErrTigerGraphSchemaSetUpFailed)
}
return nil
}
}
return fmt.Errorf(
"failed to run migration, no file with migration number found. number: %s, mode: %s",
number,
mode,
)
}
func (c *TigerGraphClient) migrateFile(ctx context.Context, fileName string) error {
bytes, err := os.ReadFile(fileName)
if err != nil {
return err
}
err = c.RunGSQL(ctx, string(bytes))
if err != nil {
return err
}
return nil
}
// MigrationVertexPayloadValue is an object containing a "value" attribute
type MigrationVertexPayloadValue[T any] struct {
Value T `json:"value"`
}
// MigrationVertexPayload is the shape of a migration vertex being stored in the vertex upsert payload
type MigrationVertexPayload struct {
GraphName MigrationVertexPayloadValue[string] `json:"graph_name"`
MigrationNumber MigrationVertexPayloadValue[string] `json:"migration_number"`
Mode MigrationVertexPayloadValue[string] `json:"mode"`
CreatedAt MigrationVertexPayloadValue[time.Time] `json:"created_at"`
}
// MigrationVerticesPayload is the map to all vertices in the payload
type MigrationVerticesPayload struct {
Migration map[string]MigrationVertexPayload `json:"Migration"`
}
// MigrationUpsertPayload is the whole payload sent to the upsert vertices endpoint for migrations
type MigrationUpsertPayload struct {
Vertices MigrationVerticesPayload `json:"vertices"`
}
func (c *TigerGraphClient) commitMigrationVersion(ctx context.Context, graph string, version string, mode string) error {
createdAt := time.Now()
id := fmt.Sprintf("%s_%s_%s", version, mode, createdAt.Format(time.RFC3339))
payload := MigrationUpsertPayload{
MigrationVerticesPayload{
map[string]MigrationVertexPayload{
id: {
GraphName: MigrationVertexPayloadValue[string]{graph},
MigrationNumber: MigrationVertexPayloadValue[string]{version},
Mode: MigrationVertexPayloadValue[string]{mode},
CreatedAt: MigrationVertexPayloadValue[time.Time]{createdAt},
},
},
},
}
res, err := c.Upsert(ctx, MetadataGraphName, payload)
if err != nil {
return err
}
if res.AcceptedVertices != 1 {
return fmt.Errorf(
"upsert of migration vertex returned an unexpected number of accepted vertices. accepted: %d but expected only 1. error type: %w",
res.AcceptedVertices,
ErrTigerGraphSchemaSetUpFailed,
)
}
return nil
}