This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
migrations.go
374 lines (362 loc) · 10.8 KB
/
migrations.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
package config
import (
"database/sql"
"fmt"
"github.com/flyteorg/flyteadmin/pkg/repositories/models"
schedulerModels "github.com/flyteorg/flyteadmin/scheduler/repositories/models"
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)
// TODO: add a way to get these list of tables directly from the gorm loaded models
var (
tables = []string{"execution_events", "executions", "launch_plans", "named_entity_metadata",
"node_execution_events", "node_executions", "projects", "resources", "schedulable_entities",
"schedule_entities_snapshots", "task_executions", "tasks", "workflows"}
)
var Migrations = []*gormigrate.Migration{
// Create projects table.
{
ID: "2019-05-22-projects",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Project{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("projects")
},
},
// Create Task
{
ID: "2018-05-23-tasks",
Migrate: func(tx *gorm.DB) error {
// The gormigrate library recommends that we copy the actual struct into here for record-keeping but after
// some internal discussion we've decided that that's not necessary. Just a history of what we've touched
// when should be sufficient.
return tx.AutoMigrate(&models.Task{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("tasks")
},
},
// Create Workflow
{
ID: "2018-05-23-workflows",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Workflow{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("workflows")
},
},
// Create Launch Plan table
{
ID: "2019-05-23-lp",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.LaunchPlan{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("launch_plans")
},
},
// Create executions table
{
ID: "2019-05-23-executions",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("executions")
},
},
// Create executions events table
{
ID: "2019-01-29-executions-events",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.ExecutionEvent{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("executions_events")
},
},
// Create node executions table
{
ID: "2019-04-17-node-executions",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&NodeExecution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("node_executions")
},
},
// Create node executions events table
{
ID: "2019-01-29-node-executions-events",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.NodeExecutionEvent{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("node_executions_events")
},
},
// Create task executions table
{
ID: "2019-03-16-task-executions",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&TaskExecution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("task_executions")
},
},
// Update node executions with null parent values
{
ID: "2019-04-17-node-executions-backfill",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("update node_executions set parent_task_execution_id = NULL where parent_task_execution_id = 0").Error
},
},
// Update executions table to add cluster
{
ID: "2019-09-27-executions",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE executions DROP COLUMN IF EXISTS cluster").Error
},
},
// Update projects table to add description column
{
ID: "2019-10-09-project-description",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Project{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE projects DROP COLUMN IF EXISTS description").Error
},
},
// Add offloaded URIs to table
{
ID: "2019-10-15-offload-inputs",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE executions DROP COLUMN IF EXISTS InputsURI, DROP COLUMN IF EXISTS UserInputsURI").Error
},
},
// Create named_entity_metadata table.
{
ID: "2019-11-05-named-entity-metadata",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.NamedEntityMetadata{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("named_entity_metadata")
},
},
// Add ProjectAttributes with custom resource attributes.
{
ID: "2020-01-10-resource",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Resource{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("resources")
},
},
// Add Type to Task model.
{
ID: "2020-03-17-task-type",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Task{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE tasks DROP COLUMN IF EXISTS type").Error
},
},
// Add state to name entity model
{
ID: "2020-04-03-named-entity-state",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.NamedEntityMetadata{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Table("named_entity_metadata").Migrator().DropColumn(&models.NamedEntityMetadata{}, "state")
},
},
// Set default state value for workflow model
{
ID: "2020-04-03-named-entity-state-default",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("UPDATE named_entity_metadata SET state = 0").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("UPDATE named_entity_metadata set state = NULL").Error
},
},
// Modify the workflows table, if necessary
{
ID: "2020-04-03-workflow-state",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE workflows DROP COLUMN IF EXISTS state").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE workflows ADD COLUMN IF NOT EXISTS state integer;").Error
},
},
// Modify the executions & node_execution table, if necessary
{
ID: "2020-04-29-executions",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{}, &models.NodeExecution{})
},
Rollback: func(tx *gorm.DB) error {
if err := tx.Model(&models.Execution{}).Migrator().DropColumn(&models.Execution{}, "error_code"); err != nil {
return err
}
if err := tx.Model(&models.Execution{}).Migrator().DropColumn(&models.Execution{}, "error_kind"); err != nil {
return err
}
if err := tx.Model(&models.NodeExecution{}).Migrator().DropColumn(&models.NodeExecution{}, "error_code"); err != nil {
return err
}
if err := tx.Model(&models.NodeExecution{}).Migrator().DropColumn(&models.NodeExecution{}, "error_kind"); err != nil {
return err
}
return nil
},
},
// Add TaskID to Execution model.
{
ID: "2020-04-14-task-type",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE executions DROP COLUMN IF EXISTS task_id").Error
},
},
// NodeExecutions table has CacheStatus for Task nodes
{
ID: "2020-07-27-cachestatus",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.NodeExecution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Model(&models.NodeExecution{}).Migrator().DropColumn(&models.NodeExecution{}, "cache_status")
},
},
{
ID: "2020-07-31-node-execution",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.NodeExecution{})
},
Rollback: func(tx *gorm.DB) error {
if err := tx.Model(&models.NodeExecution{}).Migrator().DropColumn(&models.NodeExecution{}, "parent_id"); err != nil {
return err
}
return tx.Model(&models.NodeExecution{}).Migrator().DropColumn(&models.NodeExecution{}, "node_execution_metadata")
},
},
{
ID: "2020-08-17-labels-addition",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Project{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Model(&models.Project{}).Migrator().DropColumn(&models.Project{}, "labels")
},
},
{
ID: "2020-09-01-task-exec-idx",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&TaskExecution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Model(&TaskExecution{}).Migrator().DropIndex(&TaskExecution{}, "idx_task_executions_exec")
},
},
{
ID: "2020-11-03-project-state-addition",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Project{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Model(&models.Project{}).Migrator().DropColumn(&models.Project{}, "state")
},
},
{
ID: "2020-11-03-project-state-default",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("UPDATE projects set state = 0").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("UPDATE projects set state = NULL").Error
},
},
{
ID: "2021-01-22-execution-user",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Model(&models.Execution{}).Migrator().DropColumn(&models.Execution{}, "user")
},
},
{
ID: "2021-04-19-node-execution_dynamic-workflow",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.NodeExecution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Model(&models.NodeExecution{}).Migrator().DropColumn(&models.NodeExecution{}, "dynamic_workflow_remote_closure_reference")
},
},
{
ID: "2021-07-22-schedulable_entities",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&schedulerModels.SchedulableEntity{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable(&schedulerModels.SchedulableEntity{}, "schedulable_entities")
},
},
{
ID: "2021-08-05-schedulable_entities_snapshot",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&schedulerModels.ScheduleEntitiesSnapshot{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable(&schedulerModels.ScheduleEntitiesSnapshot{}, "schedulable_entities_snapshot")
},
},
// For any new table, Please use the following pattern due to a bug
// in the postgres gorm layer https://github.com/go-gorm/postgres/issues/65
{
ID: "2022-01-11-id-to-bigint",
Migrate: func(tx *gorm.DB) error {
db, err := tx.DB()
if err != nil {
return err
}
return alterTableColumnType(db, "id", "bigint")
},
Rollback: func(tx *gorm.DB) error {
db, err := tx.DB()
if err != nil {
return err
}
return alterTableColumnType(db, "id", "int")
},
},
}
func alterTableColumnType(db *sql.DB, columnName, columnType string) error {
var err error
for _, table := range tables {
if _, err = db.Exec(fmt.Sprintf(`ALTER TABLE IF EXISTS %s ALTER COLUMN "%s" TYPE %s`, table, columnName,
columnType)); err != nil {
return err
}
}
return nil
}