/
0045_add_uuid_to_pipeline_task_runs.go
56 lines (49 loc) · 1.78 KB
/
0045_add_uuid_to_pipeline_task_runs.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
package migrations
import (
"gorm.io/gorm"
)
const up45 = `
-- Truncate tables to ease bigint -> UUID migration
TRUNCATE TABLE pipeline_runs, pipeline_task_runs, flux_monitor_round_stats_v2;
-- Migrate pipeline_task_runs to UUID
ALTER TABLE pipeline_task_runs DROP CONSTRAINT pipeline_task_runs_pkey;
ALTER TABLE pipeline_task_runs DROP COLUMN id;
ALTER TABLE pipeline_task_runs ADD COLUMN id uuid PRIMARY KEY;
-- Add state & inputs to pipeline_runs
ALTER TABLE pipeline_runs ADD COLUMN inputs jsonb;
CREATE TYPE pipeline_runs_state AS ENUM (
'running',
'suspended',
'errored',
'completed'
);
ALTER TABLE pipeline_runs ADD COLUMN state pipeline_runs_state NOT NULL DEFAULT 'completed';
ALTER TABLE pipeline_runs DROP CONSTRAINT pipeline_runs_check;
ALTER TABLE pipeline_runs ADD CONSTRAINT pipeline_runs_check CHECK (
((state IN ('completed', 'errored')) AND (finished_at IS NOT NULL) AND (num_nulls(outputs, errors) = 0))
OR
((state IN ('running', 'suspended')) AND num_nulls(finished_at, outputs, errors) = 3)
);
`
const down45 = `
ALTER TABLE pipeline_runs DROP CONSTRAINT pipeline_runs_check;
ALTER TABLE pipeline_runs ADD CONSTRAINT pipeline_runs_check CHECK (
(((outputs IS NULL) AND (errors IS NULL) AND (finished_at IS NULL))
OR ((outputs IS NOT NULL) AND (errors IS NOT NULL) AND (finished_at IS NOT NULL)))
)
DROP CONSTRAINT IF EXISTS pipeline_task_runs_run_id_key;
ALTER TABLE pipeline_task_runs DROP COLUMN run_id;
ALTER TABLE pipeline_runs DROP COLUMN inputs;
DROP TYPE pipeline_runs_state;
`
func init() {
Migrations = append(Migrations, &Migration{
ID: "0045_add_uuid_to_pipeline_task_runs",
Migrate: func(db *gorm.DB) error {
return db.Exec(up45).Error
},
Rollback: func(db *gorm.DB) error {
return db.Exec(down45).Error
},
})
}