-
Notifications
You must be signed in to change notification settings - Fork 79
/
distribution_scanartifact.go
62 lines (54 loc) · 1.3 KB
/
distribution_scanartifact.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
package postgres
import (
"context"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/quay/claircore"
"github.com/quay/claircore/indexer"
)
// InsertDistScanArtifacts will create DistributionScanArtifacts linking the
// layer hash, dist, and scanner artifacts.
//
// If multiple scanners are provided they will be linked in i % n fashion where
// "i" is the current index of the dists slice and "n" is the length of the
// scnrs slice.
func InsertDistScanArtifacts(ctx context.Context, pool *pgxpool.Pool, layerHash claircore.Digest, dists []*claircore.Distribution, scnrs indexer.VersionedScanners) error {
query := `
WITH
layer_insert
AS (
INSERT
INTO
layer (hash)
VALUES
($1)
ON CONFLICT
(hash)
DO
UPDATE SET hash = $1
RETURNING
id AS layer_id
)
INSERT
INTO
dist_scanartifact (layer_id, dist_id, scanner_id)
VALUES
((SELECT layer_id FROM layer_insert), $2, $3);
`
insertLayer := `
INSERT INTO layer (hash) VALUES ($1);
`
_, err := pool.Exec(ctx, insertLayer, &layerHash)
if err != nil {
return fmt.Errorf("failed to insert layer %v", err)
}
n := len(scnrs)
for i, dist := range dists {
nn := i % n
_, err := pool.Exec(ctx, query, &layerHash, &dist.ID, &nn)
if err != nil {
return fmt.Errorf("failed to insert scan artifact %v", err)
}
}
return nil
}