github.com/quay/claircore@v1.5.28/test/postgres/repository_scanartifact.go (about)

     1  package postgres
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/jackc/pgx/v4/pgxpool"
     8  
     9  	"github.com/quay/claircore"
    10  	"github.com/quay/claircore/indexer"
    11  )
    12  
    13  func InsertRepoScanArtifact(ctx context.Context, pool *pgxpool.Pool, layerHash claircore.Digest, repos []*claircore.Repository, scnrs indexer.VersionedScanners) error {
    14  	query := `
    15  WITH
    16  	layer_insert
    17  		AS (
    18  			INSERT
    19  			INTO
    20  				layer (hash)
    21  			VALUES
    22  				($1)
    23  			ON CONFLICT
    24  			DO
    25  				UPDATE SET hash = excluded.hash
    26  			RETURNING
    27  				id AS layer_id
    28  		)
    29  INSERT
    30  INTO
    31  	repo_scanartifact (layer_id, repo_id, scanner_id)
    32  VALUES
    33  	((SELECT layer_id FROM layer_insert), $2, $3);
    34  `
    35  
    36  	insertLayer := `
    37  INSERT INTO layer (hash) VALUES ($1);
    38  `
    39  
    40  	_, err := pool.Exec(ctx, insertLayer, &layerHash)
    41  	if err != nil {
    42  		return fmt.Errorf("failed to insert layer %v", err)
    43  	}
    44  
    45  	n := len(scnrs)
    46  	for i, repo := range repos {
    47  		nn := i % n
    48  		_, err := pool.Exec(ctx, query, &layerHash, &repo.ID, &nn)
    49  		if err != nil {
    50  			return fmt.Errorf("failed to insert repo scan artifact: %v", err)
    51  		}
    52  	}
    53  
    54  	return nil
    55  }