/
migrate.go
133 lines (113 loc) · 4.32 KB
/
migrate.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
package migration1565291711
import (
"github.com/vordev/VOR/core/store/dbutil"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
// Migrate optimizes the JobRuns table to reduce the cost of IDs
func Migrate(tx *gorm.DB) error {
if dbutil.IsPostgres(tx) {
if err := tx.Exec(`
ALTER TABLE job_specs ADD COLUMN "id_uuid" uuid;
UPDATE job_specs
SET
"id_uuid" = CAST("id" as uuid);
`).Error; err != nil {
return errors.Wrap(err, "failed to add id_uuid on job_runs")
}
if err := tx.Exec(`
ALTER TABLE job_runs ADD COLUMN "job_spec_id_uuid" uuid;
UPDATE job_runs
SET
"job_spec_id_uuid" = CAST("job_spec_id" as uuid);
`).Error; err != nil {
return errors.Wrap(err, "failed to add job_spec_id_uuid on job_runs")
}
if err := tx.Exec(`
ALTER TABLE task_specs ADD COLUMN "job_spec_id_uuid" uuid;
UPDATE task_specs
SET
"job_spec_id_uuid" = CAST("job_spec_id" as uuid);
`).Error; err != nil {
return errors.Wrap(err, "failed to add job_spec_id_uuid on task_specs")
}
if err := tx.Exec(`
ALTER TABLE service_agreements ADD COLUMN "job_spec_id_uuid" uuid;
UPDATE service_agreements
SET
"job_spec_id_uuid" = CAST("job_spec_id" as uuid);
`).Error; err != nil {
return errors.Wrap(err, "failed to add job_spec_id_uuid on service_agreements")
}
if err := tx.Exec(`
ALTER TABLE job_specs DROP CONSTRAINT "job_specs_pkey" CASCADE;
ALTER TABLE job_specs DROP COLUMN "id";
ALTER TABLE job_specs RENAME COLUMN "id_uuid" TO "id";
ALTER TABLE job_specs ADD CONSTRAINT "job_spec_pkey" PRIMARY KEY ("id");
`).Error; err != nil {
return errors.Wrap(err, "failed to rename id on job_specs")
}
if err := tx.Exec(`
ALTER TABLE job_runs DROP COLUMN "job_spec_id";
ALTER TABLE job_runs RENAME COLUMN "job_spec_id_uuid" TO "job_spec_id";
ALTER TABLE job_runs ADD CONSTRAINT "job_runs_job_spec_id_fkey" FOREIGN KEY ("job_spec_id") REFERENCES job_specs ("id") ON DELETE CASCADE;
`).Error; err != nil {
return errors.Wrap(err, "failed to update job_spec_id id on job_runs")
}
if err := tx.Exec(`
ALTER TABLE task_specs DROP COLUMN "job_spec_id";
ALTER TABLE task_specs RENAME COLUMN "job_spec_id_uuid" TO "job_spec_id";
ALTER TABLE task_specs ADD CONSTRAINT "task_specs_job_spec_id_fkey" FOREIGN KEY ("job_spec_id") REFERENCES job_specs ("id") ON DELETE CASCADE;
`).Error; err != nil {
return errors.Wrap(err, "failed to update job_spec_id id on task_specs")
}
if err := tx.Exec(`
ALTER TABLE service_agreements DROP COLUMN "job_spec_id";
ALTER TABLE service_agreements RENAME COLUMN "job_spec_id_uuid" TO "job_spec_id";
ALTER TABLE service_agreements ADD CONSTRAINT "service_agreements_job_spec_id_fkey" FOREIGN KEY ("job_spec_id") REFERENCES job_specs ("id") ON DELETE CASCADE;
`).Error; err != nil {
return errors.Wrap(err, "failed to update job_spec_id id on service_agreements")
}
if err := tx.Exec(`
ALTER TABLE job_runs ADD COLUMN "id_uuid" uuid;
UPDATE job_runs
SET
"id_uuid" = CAST("id" as uuid);
ALTER TABLE job_runs DROP CONSTRAINT "job_runs_pkey" CASCADE;
`).Error; err != nil {
return errors.Wrap(err, "failed to add id_uuid on job_runs")
}
if err := tx.Exec(`
ALTER TABLE task_runs ADD COLUMN "job_run_id_uuid" uuid;
UPDATE task_runs
SET
"job_run_id_uuid" = CAST("job_run_id" as uuid);
`).Error; err != nil {
return errors.Wrap(err, "failed to add job_run_id_uuid on task_runs")
}
if err := tx.Exec(`
ALTER TABLE job_runs DROP COLUMN "id";
ALTER TABLE job_runs RENAME COLUMN "id_uuid" TO "id";
ALTER TABLE job_runs ADD CONSTRAINT "job_run_pkey" PRIMARY KEY ("id");
`).Error; err != nil {
return errors.Wrap(err, "failed to rename id on job_runs")
}
if err := tx.Exec(`
ALTER TABLE task_runs DROP COLUMN "job_run_id";
ALTER TABLE task_runs RENAME COLUMN "job_run_id_uuid" TO "job_run_id";
ALTER TABLE task_runs ADD CONSTRAINT "task_runs_job_run_id_fkey" FOREIGN KEY ("job_run_id") REFERENCES job_runs ("id") ON DELETE CASCADE;
`).Error; err != nil {
return errors.Wrap(err, "failed to update job_run_id id on task_runs")
}
if err := tx.Exec(`
ALTER TABLE task_runs ADD COLUMN "id_uuid" uuid;
UPDATE task_runs SET "id_uuid" = CAST("id" as uuid);
ALTER TABLE task_runs DROP COLUMN "id";
ALTER TABLE task_runs RENAME COLUMN "id_uuid" TO "id";
ALTER TABLE task_runs ADD CONSTRAINT "task_run_pkey" PRIMARY KEY ("id");
`).Error; err != nil {
return errors.Wrap(err, "failed to update id on task_runs")
}
}
return nil
}