forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
team_db_containers.go
172 lines (137 loc) · 3.85 KB
/
team_db_containers.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
package db
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"strings"
)
const teamContainerJoins = containerJoins + "\nLEFT JOIN teams t ON c.team_id = t.id"
func (db *teamDB) FindContainersByDescriptors(id Container) ([]SavedContainer, error) {
err := db.deleteExpiredContainers()
if err != nil {
return nil, err
}
var whereCriteria []string
var params []interface{}
if id.ResourceName != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("r.name = $%d", len(params)+1))
params = append(params, id.ResourceName)
}
if id.StepName != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("c.step_name = $%d", len(params)+1))
params = append(params, id.StepName)
}
if id.JobName != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("j.name = $%d", len(params)+1))
params = append(params, id.JobName)
}
if id.PipelineName != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("p.name = $%d", len(params)+1))
params = append(params, id.PipelineName)
}
if id.BuildID != 0 {
whereCriteria = append(whereCriteria, fmt.Sprintf("build_id = $%d", len(params)+1))
params = append(params, id.BuildID)
}
if id.Type != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("type = $%d", len(params)+1))
params = append(params, id.Type.String())
}
if id.WorkerName != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("worker_name = $%d", len(params)+1))
params = append(params, id.WorkerName)
}
if id.CheckType != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("check_type = $%d", len(params)+1))
params = append(params, id.CheckType)
}
if id.BuildName != "" {
whereCriteria = append(whereCriteria, fmt.Sprintf("b.name = $%d", len(params)+1))
params = append(params, id.BuildName)
}
var checkSourceBlob []byte
if id.CheckSource != nil {
checkSourceBlob, err = json.Marshal(id.CheckSource)
if err != nil {
return nil, err
}
whereCriteria = append(whereCriteria, fmt.Sprintf("check_source = $%d", len(params)+1))
params = append(params, checkSourceBlob)
}
if len(id.Attempts) > 0 {
attemptsBlob, err := json.Marshal(id.Attempts)
if err != nil {
return nil, err
}
whereCriteria = append(whereCriteria, fmt.Sprintf("attempts = $%d", len(params)+1))
params = append(params, attemptsBlob)
}
var rows *sql.Rows
team, found, err := db.GetTeam()
if err != nil {
return nil, err
}
if !found {
return nil, errors.New("team-not-found")
}
selectQuery := fmt.Sprintf(`
SELECT `+containerColumns+`
FROM containers c `+teamContainerJoins+`
WHERE c.team_id = %d
`, team.ID)
if len(whereCriteria) > 0 {
selectQuery += fmt.Sprintf(" AND %s", strings.Join(whereCriteria, " AND "))
}
rows, err = db.conn.Query(selectQuery, params...)
if err != nil {
return nil, err
}
defer rows.Close()
infos := []SavedContainer{}
for rows.Next() {
info, err := scanContainer(rows)
if err != nil {
return nil, err
}
infos = append(infos, info)
}
return infos, nil
}
func (db *teamDB) GetContainer(handle string) (SavedContainer, bool, error) {
err := db.deleteExpiredContainers()
if err != nil {
return SavedContainer{}, false, err
}
team, found, err := db.GetTeam()
if err != nil {
return SavedContainer{}, false, err
}
if !found {
return SavedContainer{}, false, errors.New("team-not-found")
}
container, err := scanContainer(db.conn.QueryRow(fmt.Sprintf(`
SELECT `+containerColumns+`
FROM containers c `+teamContainerJoins+`
WHERE c.handle = $1
AND c.team_id = %d
`, team.ID), handle))
if err != nil {
if err == sql.ErrNoRows {
return SavedContainer{}, false, nil
}
return SavedContainer{}, false, err
}
return container, true, nil
}
func (db *teamDB) deleteExpiredContainers() error {
_, err := db.conn.Exec(`
DELETE FROM containers
WHERE expires_at IS NOT NULL
AND expires_at < NOW()
`)
if err != nil {
return err
}
return nil
}