-
Notifications
You must be signed in to change notification settings - Fork 10
/
insert.go
348 lines (271 loc) · 7.5 KB
/
insert.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
package postgres
import (
"database/sql"
"fmt"
"github.com/pkg/errors"
"github.com/simplesurance/baur/storage"
)
func insertBuild(tx *sql.Tx, appID, vcsID int, b *storage.Build) (int, error) {
const stmt = `
INSERT INTO build
(application_id, vcs_id, start_timestamp, stop_timestamp, total_input_digest)
VALUES($1, $2, $3, $4, $5)
RETURNING id;`
var id int
r := tx.QueryRow(stmt, appID, vcsID, b.StartTimeStamp, b.StopTimeStamp, b.TotalInputDigest)
if err := r.Scan(&id); err != nil {
return -1, err
}
return id, nil
}
func insertBuildOutputs(tx *sql.Tx, buildID int, outputIDs []int) ([]int, error) {
const stmt1 = "INSERT INTO build_output(build_id, output_id) VALUES"
const stmt2 = "RETURNING ID"
var ids []int
var stmtVals string
for i, outputID := range outputIDs {
stmtVals += fmt.Sprintf("(%d, %d)", buildID, outputID)
if i < len(outputIDs)-1 {
stmtVals += ", "
}
}
query := stmt1 + stmtVals + stmt2
rows, err := tx.Query(query)
if err != nil {
return nil, errors.Wrapf(err, "db query %q failed", query)
}
for rows.Next() {
var id int
err := rows.Scan(&id)
if err != nil {
rows.Close()
return nil, errors.Wrapf(err, "parsing result of query %q failed", query)
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "iterating over rows failed")
}
return ids, nil
}
func insertOutputsIfNotExist(tx *sql.Tx, outputs []*storage.Output) ([]int, error) {
const stmt1 = "INSERT INTO output (name, type, digest, size_bytes) VALUES"
const stmt2 = `
ON CONFLICT ON CONSTRAINT output_digest_key
DO UPDATE SET id=output.id RETURNING id
`
var (
argCNT = 1
stmtVals string
queryArgs = make([]interface{}, 0, len(outputs)*4)
ids = make([]int, 0, len(outputs))
)
for i, out := range outputs {
stmtVals += fmt.Sprintf("($%d, $%d, $%d, $%d)", argCNT, argCNT+1, argCNT+2, argCNT+3)
argCNT += 4
queryArgs = append(queryArgs, out.Name, out.Type, out.Digest, out.SizeBytes)
if i < len(outputs)-1 {
stmtVals += ", "
}
}
query := stmt1 + stmtVals + stmt2
rows, err := tx.Query(query, queryArgs...)
if err != nil {
return nil, errors.Wrapf(err, "db query %q failed", query)
}
for rows.Next() {
var id int
err := rows.Scan(&id)
if err != nil {
rows.Close()
return nil, errors.Wrapf(err, "parsing result of query %q failed", query)
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "iterating over rows failed")
}
return ids, nil
}
func insertInputBuilds(tx *sql.Tx, buildID int, inputIDs []int) error {
const stmt1 = `
INSERT into input_build
(build_id, input_id)
VALUES
`
var stmtVals string
argCNT := 1
queryArgs := make([]interface{}, 0, len(inputIDs))
queryArgs = append(queryArgs, buildID)
argCNT++
for i, id := range inputIDs {
stmtVals += fmt.Sprintf("($1, $%d)", argCNT)
argCNT++
queryArgs = append(queryArgs, id)
if i < len(inputIDs)-1 {
stmtVals += ", "
}
}
query := stmt1 + stmtVals
_, err := tx.Exec(query, queryArgs...)
if err != nil {
return errors.Wrapf(err, "db query %q failed", query)
}
return nil
}
func insertInputsIfNotExist(tx *sql.Tx, inputs []*storage.Input) ([]int, error) {
const stmt1 = "INSERT INTO input (uri, digest) VALUES"
const stmt2 = `
ON CONFLICT ON CONSTRAINT input_uniq
DO UPDATE SET id=input.id RETURNING id
`
var (
stmtVals string
argCNT = 1
queryArgs = make([]interface{}, 0, len(inputs)*2)
ids = make([]int, 0, len(inputs))
)
for i, in := range inputs {
stmtVals += fmt.Sprintf("($%d, $%d)", argCNT, argCNT+1)
argCNT += 2
queryArgs = append(queryArgs, in.URI, in.Digest)
if i < len(inputs)-1 {
stmtVals += ", "
}
}
query := stmt1 + stmtVals + stmt2
rows, err := tx.Query(query, queryArgs...)
if err != nil {
return nil, errors.Wrapf(err, "db query %q failed", query)
}
for rows.Next() {
var id int
err := rows.Scan(&id)
if err != nil {
rows.Close()
return nil, errors.Wrapf(err, "db query %q failed", query)
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, errors.Wrap(err, "iterating over rows failed")
}
return ids, nil
}
func insertVCSIfNotExist(tx *sql.Tx, v *storage.VCSState) (int, error) {
const stmt = `
INSERT INTO vcs
(commit, dirty)
VALUES($1, $2)
ON CONFLICT ON CONSTRAINT vcs_uniq
DO UPDATE SET id=vcs.id RETURNING id
`
var id int
err := tx.QueryRow(stmt, v.CommitID, v.IsDirty).Scan(&id)
if err != nil {
return -1, errors.Wrapf(err, "db query %q failed", stmt)
}
return id, nil
}
func insertAppIfNotExist(tx *sql.Tx, app *storage.Application) error {
const stmt = `
INSERT INTO application
(name)
VALUES($1)
ON CONFLICT ON CONSTRAINT application_name_key
DO UPDATE SET id=application.id RETURNING id
`
err := tx.QueryRow(stmt, app.NameLower()).Scan(&app.ID)
if err != nil {
return errors.Wrapf(err, "db query %q failed", stmt)
}
return nil
}
func insertUploads(tx *sql.Tx, buildOutputIDs []int, outputs []*storage.Output) error {
const stmt = `
INSERT into upload
(build_output_id, uri, method, upload_duration_ns)
VALUES
`
var (
stmtVals string
argCNT = 1
queryArgs = make([]interface{}, 0, len(outputs)*4)
)
// TODO: retrieve the ID from the insert and set it in out.Upload
if len(outputs) != len(buildOutputIDs) {
return fmt.Errorf("output (%d) and buildOutputIDs (%d) slices are not of same length",
len(outputs), len(buildOutputIDs))
}
for i, out := range outputs {
stmtVals += fmt.Sprintf("($%d, $%d,$%d, $%d)", argCNT, argCNT+1, argCNT+2, argCNT+3)
argCNT += 4
queryArgs = append(queryArgs, buildOutputIDs[i], out.Upload.URI, out.Upload.Method, out.Upload.UploadDuration)
if i < len(outputs)-1 {
stmtVals += ", "
}
}
query := stmt + stmtVals
_, err := tx.Exec(query, queryArgs...)
if err != nil {
return errors.Wrapf(err, "db query %q failed", query)
}
return err
}
// Save stores a build in the database, the ID field of the passed Build is
// ignored. The database generates a record ID and it will be stored in the
// passed Build.
func (c *Client) Save(b *storage.Build) error {
tx, err := c.Db.Begin()
if err != nil {
return errors.Wrap(err, "starting transaction failed")
}
defer func() {
if err != nil {
_ = tx.Rollback()
return
}
commitErr := tx.Commit()
if commitErr != nil {
err = errors.Wrap(err, "committing transaction failed")
}
}()
err = insertAppIfNotExist(tx, &b.Application)
if err != nil {
return errors.Wrap(err, "storing application record failed")
}
vcsID, err := insertVCSIfNotExist(tx, &b.VCSState)
if err != nil {
return errors.Wrap(err, "storing vcs information failed")
}
buildID, err := insertBuild(tx, b.Application.ID, vcsID, b)
if err != nil {
return errors.Wrap(err, "storing build record failed")
}
outputIDs, err := insertOutputsIfNotExist(tx, b.Outputs)
if err != nil {
return errors.Wrap(err, "storing output records failed")
}
buildOutputIDs, err := insertBuildOutputs(tx, buildID, outputIDs)
if err != nil {
return errors.Wrap(err, "storing buildOutput records failed")
}
err = insertUploads(tx, buildOutputIDs, b.Outputs)
if err != nil {
return errors.Wrap(err, "storing upload record failed")
}
// inputs not specified in the baur app config
if len(b.Inputs) == 0 {
return nil
}
ids, err := insertInputsIfNotExist(tx, b.Inputs)
if err != nil {
return errors.Wrap(err, "storing inputs failed")
}
err = insertInputBuilds(tx, buildID, ids)
if err != nil {
return errors.Wrap(err, "storing input_build failed")
}
b.ID = buildID
return nil
}