forked from vmware-archive/atc
/
165_add_groups_and_remove_config_from_pipeline.go
96 lines (80 loc) · 1.59 KB
/
165_add_groups_and_remove_config_from_pipeline.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
package migrations
import (
"database/sql"
"encoding/json"
"github.com/concourse/atc"
"github.com/concourse/atc/db/migration"
)
func AddGroupsAndRemoveConfigFromPipeline(strategy EncryptionStrategy) migration.Migrator {
return func(tx migration.LimitedTx) error {
_, err := tx.Exec(`
ALTER TABLE pipelines
ADD COLUMN groups json;
`)
if err != nil {
return err
}
rows, err := tx.Query(`
SELECT id, config, nonce
FROM pipelines
`)
if err != nil {
return err
}
defer rows.Close()
pipelineGroups := map[int][]byte{}
for rows.Next() {
var (
pipelineID int
pipelineConfig []byte
nonce sql.NullString
)
err := rows.Scan(&pipelineID, &pipelineConfig, &nonce)
if err != nil {
return err
}
var noncense *string
if nonce.Valid {
noncense = &nonce.String
}
decryptedConfig, err := strategy.Decrypt(string(pipelineConfig), noncense)
if err != nil {
return err
}
var config atc.Config
err = json.Unmarshal(decryptedConfig, &config)
if err != nil {
return err
}
groups, err := json.Marshal(config.Groups)
if err != nil {
return err
}
pipelineGroups[pipelineID] = groups
}
for id, groups := range pipelineGroups {
_, err := tx.Exec(`
UPDATE pipelines
SET groups = $1
WHERE id = $2`, groups, id)
if err != nil {
return err
}
}
_, err = tx.Exec(`
ALTER TABLE pipelines
DROP COLUMN config
`)
if err != nil {
return err
}
_, err = tx.Exec(`
ALTER TABLE pipelines
DROP COLUMN nonce
`)
if err != nil {
return err
}
return nil
}
}