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 63
/
migrations.go
1125 lines (1059 loc) · 37.6 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package config
import (
"database/sql"
"fmt"
"time"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytestdlib/storage"
"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", "description_entities"}
)
var LegacyMigrations = []*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")
},
},
// Add state to execution model.
{
ID: "2022-01-11-execution-state",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Table("execution").Migrator().DropColumn(&models.Execution{}, "state")
},
},
// Add internal data to node execution model.
{
ID: "2022-03-29-node-execution-internal-data",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&NodeExecution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Table("node_execution").Migrator().DropColumn(&NodeExecution{}, "internal_data")
},
},
// Add created_at index to the execution model.
{
ID: "2022-04-04-execution-created-at-index",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Execution{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Model(&models.Execution{}).Migrator().DropIndex(&models.Execution{}, "idx_executions_created_at")
},
},
// Create description entities table
{
ID: "2022-09-13-description-entities",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.DescriptionEntity{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("description_entities")
},
},
// Modify the tasks table, if necessary
{
ID: "2020-09-13-task-short_description",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE tasks ADD COLUMN IF NOT EXISTS short_description varchar(4000)").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE tasks DROP COLUMN IF EXISTS short_description").Error
},
},
// Modify the workflows table, if necessary
{
ID: "2020-09-13-workflow-short_description",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE workflows ADD COLUMN IF NOT EXISTS short_description varchar(4000)").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Exec("ALTER TABLE workflows DROP COLUMN IF EXISTS short_description").Error
},
},
// Create signals table.
{
ID: "2022-04-11-signals",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&models.Signal{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("signals")
},
},
// Add the launch_type resource to the execution model
{
ID: "2022-12-09-execution-launch-type",
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{}, "launch_entity")
},
},
}
var NoopMigrations = []*gormigrate.Migration{
/* The following is a series of Postgres specific migrations. They should mirror the state
of the database as of 2023 March. The rollback is a noop for everything because the migration itself should
be a noop.
*/
{
ID: "pg-noop-2023-03-31-noop-project-3",
Migrate: func(tx *gorm.DB) error {
type Project struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
Identifier string `gorm:"primary_key"`
Name string `valid:"length(0|255)"` // Human-readable name, not a unique identifier.
Description string `gorm:"type:varchar(300)"`
Labels []byte
// GORM doesn't save the zero value for ints, so we use a pointer for the State field
State *int32 `gorm:"default:0;index"`
}
return tx.AutoMigrate(&Project{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
// ALTER TABLE "projects" ALTER COLUMN "id" DROP NOT NULL otherwise.
{
ID: "pg-noop-2023-03-31-noop-task-2",
Migrate: func(tx *gorm.DB) error {
type Task struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
Project string `gorm:"primary_key;index:task_project_domain_name_idx;index:task_project_domain_idx" valid:"length(0|255)"`
Domain string `gorm:"primary_key;index:task_project_domain_name_idx;index:task_project_domain_idx" valid:"length(0|255)"`
Name string `gorm:"primary_key;index:task_project_domain_name_idx" valid:"length(0|255)"`
Version string `gorm:"primary_key" valid:"length(0|255)"`
Closure []byte `gorm:"not null"`
// Hash of the compiled task closure
Digest []byte
// Task type (also stored in the closure put promoted as a column for filtering).
Type string `gorm:"" valid:"length(0|255)"`
// ShortDescription for the task.
ShortDescription string
}
return tx.AutoMigrate(&Task{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-workflow",
Migrate: func(tx *gorm.DB) error {
type Workflow struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
Project string `gorm:"primary_key;index:workflow_project_domain_name_idx;index:workflow_project_domain_idx" valid:"length(0|255)"`
Domain string `gorm:"primary_key;index:workflow_project_domain_name_idx;index:workflow_project_domain_idx" valid:"length(0|255)"`
Name string `gorm:"primary_key;index:workflow_project_domain_name_idx" valid:"length(0|255)"`
Version string `gorm:"primary_key"`
TypedInterface []byte
RemoteClosureIdentifier string `gorm:"not null" valid:"length(0|255)"`
// Hash of the compiled workflow closure
Digest []byte
// ShortDescription for the workflow.
ShortDescription string
}
return tx.AutoMigrate(&Workflow{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-launchplan",
Migrate: func(tx *gorm.DB) error {
type LaunchPlanScheduleType string
type LaunchPlan struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
Project string `gorm:"primary_key;index:lp_project_domain_name_idx,lp_project_domain_idx" valid:"length(0|255)"`
Domain string `gorm:"primary_key;index:lp_project_domain_name_idx,lp_project_domain_idx" valid:"length(0|255)"`
Name string `gorm:"primary_key;index:lp_project_domain_name_idx" valid:"length(0|255)"`
Version string `gorm:"primary_key" valid:"length(0|255)"`
Spec []byte `gorm:"not null"`
WorkflowID uint `gorm:"index"`
Closure []byte `gorm:"not null"`
// GORM doesn't save the zero value for ints, so we use a pointer for the State field
State *int32 `gorm:"default:0"`
// Hash of the launch plan
Digest []byte
ScheduleType LaunchPlanScheduleType
}
return tx.AutoMigrate(&LaunchPlan{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-namedentitymetadata",
Migrate: func(tx *gorm.DB) error {
type NamedEntityMetadata struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
ResourceType core.ResourceType `gorm:"primary_key;index:named_entity_metadata_type_project_domain_name_idx" valid:"length(0|255)"`
Project string `gorm:"primary_key;index:named_entity_metadata_type_project_domain_name_idx" valid:"length(0|255)"`
Domain string `gorm:"primary_key;index:named_entity_metadata_type_project_domain_name_idx" valid:"length(0|255)"`
Name string `gorm:"primary_key;index:named_entity_metadata_type_project_domain_name_idx" valid:"length(0|255)"`
Description string `gorm:"type:varchar(300)"`
// GORM doesn't save the zero value for ints, so we use a pointer for the State field
State *int32 `gorm:"default:0"`
}
return tx.AutoMigrate(&NamedEntityMetadata{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-execution",
Migrate: func(tx *gorm.DB) error {
type ExecutionKey struct {
Project string `gorm:"primary_key;column:execution_project" valid:"length(0|255)"`
Domain string `gorm:"primary_key;column:execution_domain" valid:"length(0|255)"`
Name string `gorm:"primary_key;column:execution_name" valid:"length(0|255)"`
}
type Execution struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `gorm:"index"`
ExecutionKey
LaunchPlanID uint `gorm:"index"`
WorkflowID uint `gorm:"index"`
TaskID uint `gorm:"index"`
Phase string `valid:"length(0|255)"`
Closure []byte
Spec []byte `gorm:"not null"`
StartedAt *time.Time
// Corresponds to the CreatedAt field in the Execution closure.
// Prefixed with Execution to avoid clashes with gorm.Model CreatedAt
ExecutionCreatedAt *time.Time `gorm:"index:idx_executions_created_at"`
// Corresponds to the UpdatedAt field in the Execution closure
// Prefixed with Execution to avoid clashes with gorm.Model UpdatedAt
ExecutionUpdatedAt *time.Time
Duration time.Duration
// In the case of an aborted execution this string may be non-empty.
// It should be ignored for any other value of phase other than aborted.
AbortCause string `valid:"length(0|255)"`
// Corresponds to the execution mode used to trigger this execution
Mode int32
// The "parent" execution (if there is one) that is related to this execution.
SourceExecutionID uint
// The parent node execution if this was launched by a node
ParentNodeExecutionID uint
// Cluster where execution was triggered
Cluster string `valid:"length(0|255)"`
// Offloaded location of inputs LiteralMap. These are the inputs evaluated and contain applied defaults.
InputsURI storage.DataReference
// User specified inputs. This map might be incomplete and not include defaults applied
UserInputsURI storage.DataReference
// Execution Error Kind. nullable
ErrorKind *string `gorm:"index"`
// Execution Error Code nullable
ErrorCode *string `valid:"length(0|255)"`
// The user responsible for launching this execution.
// This is also stored in the spec but promoted as a column for filtering.
User string `gorm:"index" valid:"length(0|255)"`
// GORM doesn't save the zero value for ints, so we use a pointer for the State field
State *int32 `gorm:"index;default:0"`
// The resource type of the entity used to launch the execution, one of 'launch_plan' or 'task'
LaunchEntity string
}
return tx.AutoMigrate(&Execution{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-taskexecution",
Migrate: func(tx *gorm.DB) error {
type TaskKey struct {
Project string `gorm:"primary_key"`
Domain string `gorm:"primary_key"`
Name string `gorm:"primary_key"`
Version string `gorm:"primary_key"`
}
type TaskExecutionKey struct {
TaskKey
Project string `gorm:"primary_key;column:execution_project;index:idx_task_executions_exec"`
Domain string `gorm:"primary_key;column:execution_domain;index:idx_task_executions_exec"`
Name string `gorm:"primary_key;column:execution_name;index:idx_task_executions_exec"`
NodeID string `gorm:"primary_key;index:idx_task_executions_exec;index"`
// *IMPORTANT* This is a pointer to an int in order to allow setting an empty ("0") value according to gorm convention.
// Because RetryAttempt is part of the TaskExecution primary key is should *never* be null.
RetryAttempt *uint32 `gorm:"primary_key;AUTO_INCREMENT:FALSE"`
}
type TaskExecution struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
TaskExecutionKey
Phase string `gorm:"type:text"`
PhaseVersion uint32
InputURI string `gorm:"type:text"`
Closure []byte
StartedAt *time.Time
// Corresponds to the CreatedAt field in the TaskExecution closure
// This field is prefixed with TaskExecution because it signifies when
// the execution was createdAt, not to be confused with gorm.Model.CreatedAt
TaskExecutionCreatedAt *time.Time
// Corresponds to the UpdatedAt field in the TaskExecution closure
// This field is prefixed with TaskExecution because it signifies when
// the execution was UpdatedAt, not to be confused with gorm.Model.UpdatedAt
TaskExecutionUpdatedAt *time.Time
Duration time.Duration
// The child node executions (if any) launched by this task execution.
ChildNodeExecution []NodeExecution `gorm:"foreignkey:ParentTaskExecutionID;references:ID"`
}
return tx.AutoMigrate(&TaskExecution{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
// This migration handles the necessary setup to change the type of the `parent_id` column in the node_executions table.
ID: "pg-2023-05-02-fix-parentid-type-phase-1",
Migrate: func(tx *gorm.DB) error {
shouldMigrate, err := shouldApplyFixParentidMigration(tx)
if err != nil {
return err
}
if !shouldMigrate {
return nil
}
// Alter table and add new column
if err := tx.Exec("ALTER TABLE node_executions ADD COLUMN new_parent_id BIGINT;").Error; err != nil {
return err
}
// Create trigger function
triggerFunction := `
CREATE FUNCTION set_new_parent_id() RETURNS TRIGGER AS
$BODY$
BEGIN
NEW.new_parent_id := NEW.parent_id;
RETURN NEW;
END
$BODY$ LANGUAGE PLPGSQL;
`
if err := tx.Exec(triggerFunction).Error; err != nil {
return err
}
// Create trigger
if err := tx.Exec("CREATE TRIGGER set_new_parent_id_trigger BEFORE INSERT OR UPDATE ON node_executions FOR EACH ROW EXECUTE PROCEDURE set_new_parent_id();").Error; err != nil {
return err
}
// Update table
if err := tx.Exec("UPDATE node_executions SET new_parent_id = parent_id WHERE parent_id is not null;").Error; err != nil {
return err
}
// Create new index
if err := tx.Exec("CREATE INDEX idx_node_executions_new_parent_id ON public.node_executions USING btree (new_parent_id);").Error; err != nil {
return err
}
return nil
},
Rollback: func(tx *gorm.DB) error {
// Drop trigger and function
if err := tx.Exec("DROP TRIGGER IF EXISTS set_new_parent_id_trigger ON node_executions;").Error; err != nil {
return err
}
if err := tx.Exec("DROP FUNCTION IF EXISTS set_new_parent_id();").Error; err != nil {
return err
}
// Drop column iff exists
if err := tx.Exec("ALTER TABLE node_executions DROP COLUMN IF EXISTS new_parent_id;").Error; err != nil {
return err
}
// Drop index idx_node_executions_new_parent_id
if err := tx.Exec("DROP INDEX IF EXISTS idx_node_executions_new_parent_id;").Error; err != nil {
return err
}
return nil
},
},
{
// This migration actually changes the type of the `parent_id` column in the node_executions table in a transaction.
ID: "pg-2023-05-02-fix-parentid-type-phase-2",
Migrate: func(tx *gorm.DB) error {
shouldMigrate, err := shouldApplyFixParentidMigration(tx)
if err != nil {
return err
}
if !shouldMigrate {
return nil
}
// Start transaction
tx1 := tx.Begin()
defer func() {
if r := recover(); r != nil {
tx1.Rollback()
}
}()
// Lock table
if err := tx1.Exec("LOCK TABLE node_executions IN EXCLUSIVE MODE;").Error; err != nil {
tx1.Rollback()
return err
}
// DropIndex and create a new one
if err := tx1.Exec("DROP INDEX idx_node_executions_parent_id;").Error; err != nil {
tx1.Rollback()
return err
}
// Drop and rename columns
if err := tx1.Exec("ALTER TABLE node_executions DROP COLUMN parent_id;").Error; err != nil {
tx1.Rollback()
return err
}
// Rename idx_node_executions_new_parent_id to idx_node_executions_parent_id
if err := tx1.Exec("ALTER INDEX idx_node_executions_new_parent_id RENAME TO idx_node_executions_parent_id;").Error; err != nil {
tx1.Rollback()
return err
}
if err := tx1.Exec("ALTER TABLE node_executions RENAME COLUMN new_parent_id TO parent_id;").Error; err != nil {
tx1.Rollback()
return err
}
// Drop trigger and function
if err := tx1.Exec("DROP TRIGGER IF EXISTS set_new_parent_id_trigger ON node_executions;").Error; err != nil {
tx1.Rollback()
return err
}
if err := tx1.Exec("DROP FUNCTION IF EXISTS set_new_parent_id();").Error; err != nil {
tx1.Rollback()
return err
}
// Commit transaction
if err := tx1.Commit().Error; err != nil {
return err
}
return nil
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-nodeexecution",
Migrate: func(tx *gorm.DB) error {
type ExecutionKey struct {
Project string `gorm:"primary_key;column:execution_project"`
Domain string `gorm:"primary_key;column:execution_domain"`
Name string `gorm:"primary_key;column:execution_name"`
}
type NodeExecutionKey struct {
ExecutionKey
NodeID string `gorm:"primary_key;index"`
}
type NodeExecution struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
NodeExecutionKey
// Also stored in the closure, but defined as a separate column because it's useful for filtering and sorting.
Phase string
InputURI string
Closure []byte
StartedAt *time.Time
// Corresponds to the CreatedAt field in the NodeExecution closure
// Prefixed with NodeExecution to avoid clashes with gorm.Model CreatedAt
NodeExecutionCreatedAt *time.Time
// Corresponds to the UpdatedAt field in the NodeExecution closure
// Prefixed with NodeExecution to avoid clashes with gorm.Model UpdatedAt
NodeExecutionUpdatedAt *time.Time
Duration time.Duration
// The task execution (if any) which launched this node execution.
// TO BE DEPRECATED - as we have now introduced ParentID
ParentTaskExecutionID uint `sql:"default:null" gorm:"index"`
// The workflow execution (if any) which this node execution launched
LaunchedExecution models.Execution `gorm:"foreignKey:ParentNodeExecutionID;references:ID"`
// In the case of dynamic workflow nodes, the remote closure is uploaded to the path specified here.
DynamicWorkflowRemoteClosureReference string
// Metadata that is only relevant to the flyteadmin service that is used to parse the model and track additional attributes.
InternalData []byte
NodeExecutionMetadata []byte
// Parent that spawned this node execution - value is empty for executions at level 0
ParentID *uint `sql:"default:null" gorm:"index"`
// List of child node executions - for cases like Dynamic task, sub workflow, etc
ChildNodeExecutions []NodeExecution `gorm:"foreignKey:ParentID;references:ID"`
// Execution Error Kind. nullable, can be one of core.ExecutionError_ErrorKind
ErrorKind *string `gorm:"index"`
// Execution Error Code nullable. string value, but finite set determined by the execution engine and plugins
ErrorCode *string
// If the node is of Type Task, this should always exist for a successful execution, indicating the cache status for the execution
CacheStatus *string
}
return tx.AutoMigrate(&NodeExecution{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-execution-event",
Migrate: func(tx *gorm.DB) error {
type ExecutionKey struct {
Project string `gorm:"primary_key;column:execution_project" valid:"length(0|127)"`
Domain string `gorm:"primary_key;column:execution_domain" valid:"length(0|127)"`
Name string `gorm:"primary_key;column:execution_name" valid:"length(0|127)"`
}
type ExecutionEvent struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
ExecutionKey
RequestID string `valid:"length(0|255)"`
OccurredAt time.Time
Phase string `gorm:"primary_key"`
}
return tx.AutoMigrate(&ExecutionEvent{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-node-execution-event",
Migrate: func(tx *gorm.DB) error {
type ExecutionKey struct {
Project string `gorm:"primary_key;column:execution_project" valid:"length(0|127)"`
Domain string `gorm:"primary_key;column:execution_domain" valid:"length(0|127)"`
Name string `gorm:"primary_key;column:execution_name" valid:"length(0|127)"`
}
type NodeExecutionKey struct {
ExecutionKey
NodeID string `gorm:"primary_key;index" valid:"length(0|180)"`
}
type NodeExecutionEvent struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
NodeExecutionKey
RequestID string
OccurredAt time.Time
Phase string `gorm:"primary_key"`
}
return tx.AutoMigrate(&NodeExecutionEvent{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-description-entity-2",
Migrate: func(tx *gorm.DB) error {
type DescriptionEntityKey struct {
ResourceType core.ResourceType `gorm:"primary_key;index:description_entity_project_domain_name_version_idx" valid:"length(0|255)"`
Project string `gorm:"primary_key;index:description_entity_project_domain_name_version_idx" valid:"length(0|255)"`
Domain string `gorm:"primary_key;index:description_entity_project_domain_name_version_idx" valid:"length(0|255)"`
Name string `gorm:"primary_key;index:description_entity_project_domain_name_version_idx" valid:"length(0|255)"`
Version string `gorm:"primary_key;index:description_entity_project_domain_name_version_idx" valid:"length(0|255)"`
}
// SourceCode Database model to encapsulate a SourceCode.
type SourceCode struct {
Link string `valid:"length(0|255)"`
}
// DescriptionEntity Database model to encapsulate a DescriptionEntity.
type DescriptionEntity struct {
DescriptionEntityKey
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
SourceCode
ShortDescription string
LongDescription []byte
}
return tx.AutoMigrate(&DescriptionEntity{})
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "pg-noop-2023-03-31-noop-signal",
Migrate: func(tx *gorm.DB) error {
type SignalKey struct {
ExecutionKey
SignalID string `gorm:"primary_key;index" valid:"length(0|255)"`
}
type Signal struct {
ID uint `gorm:"index;autoIncrement;not null"`
CreatedAt time.Time `gorm:"type:time"`
UpdatedAt time.Time `gorm:"type:time"`
DeletedAt *time.Time `gorm:"index"`
SignalKey