github.com/quay/claircore@v1.5.28/test/postgres/package_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  // InsertPackageScanArtifacts will create ScanArtifacts linking the layer hash,
    14  // packages, and scanner artifacts.
    15  //
    16  // If multiple scanners are provided they will be linked in i % n fashion where
    17  // "i" is the current index of the Packages array and "n" is the length of the
    18  // scanners array.
    19  func InsertPackageScanArtifacts(ctx context.Context, pool *pgxpool.Pool, layerHash claircore.Digest, pkgs []*claircore.Package, scnrs indexer.VersionedScanners) error {
    20  	query := `
    21  INSERT
    22  INTO
    23  	package_scanartifact
    24  		(
    25  			layer_id,
    26  			package_id,
    27  			source_id,
    28  			scanner_id,
    29  			package_db,
    30  			repository_hint,
    31  			filepath
    32  		)
    33  VALUES
    34  	(
    35  		(SELECT id FROM layer WHERE hash = $1),
    36  		$2,
    37  		$3,
    38  		$4,
    39  		$5,
    40  		$6,
    41  		$7
    42  	);
    43  `
    44  	insertLayer := `
    45  INSERT INTO layer (hash) VALUES ($1);
    46  `
    47  
    48  	_, err := pool.Exec(ctx, insertLayer, &layerHash)
    49  	if err != nil {
    50  		return fmt.Errorf("failed to insert layer %v", err)
    51  	}
    52  
    53  	n := len(scnrs)
    54  	for i, pkg := range pkgs {
    55  		nn := i % n
    56  		_, err := pool.Exec(ctx, query, &layerHash, &pkg.ID, &pkg.Source.ID, &nn, &pkg.PackageDB, &pkg.RepositoryHint, &pkg.Filepath)
    57  		if err != nil {
    58  			return fmt.Errorf("failed to insert scan artifact %v", err)
    59  		}
    60  	}
    61  
    62  	return nil
    63  }