github.com/quay/claircore@v1.5.28/datastore/postgres/migrations/indexer/01-init.sql (about)

     1  --- Layer
     2  --- an identity table consisting of a content addressable layer hash
     3  CREATE TABLE IF NOT EXISTS layer (
     4  	hash text PRIMARY KEY
     5  );
     6  
     7  --- Manifest
     8  --- an identity table consisting of a content addressable manifest hash
     9  CREATE TABLE IF NOT EXISTS manifest (
    10  	hash text PRIMARY KEY
    11  );
    12  
    13  --- ManifestLayer
    14  --- a many to many link table identifying the layers which comprise a manifest
    15  --- and the layer's ordering within a manifest
    16  CREATE TABLE IF NOT EXISTS manifest_layer (
    17  	manifest_hash text REFERENCES manifest(hash),
    18  	layer_hash text REFERENCES layer(hash),
    19  	i bigint,
    20  	PRIMARY KEY(manifest_hash, layer_hash, i)
    21  );
    22  
    23  --- Scanner
    24  --- a unique versioned scanner which is responsible
    25  --- for finding packages and distributions in a layer
    26  CREATE TABLE IF NOT EXISTS scanner (
    27  	id BIGSERIAL PRIMARY KEY,
    28  	name text NOT NULL,
    29  	version text NOT NULL,
    30  	kind text NOT NULL
    31  );
    32  CREATE UNIQUE INDEX IF NOT EXISTS scanner_unique_idx ON scanner (name, kind, version);
    33  
    34  --- ScannedManifest
    35  --- a relation to identify if a manifest was successfully scanned by a particular
    36  --- scanner
    37  CREATE TABLE IF NOT EXISTS scanned_manifest (
    38  	manifest_hash text REFERENCES manifest(hash),
    39  	scanner_id bigint REFERENCES scanner(id),
    40  	PRIMARY KEY(manifest_hash, scanner_id)
    41  );
    42  
    43  --- ScannedLayer
    44  --- a relation to identify if a layer was successfully scanned by a particular scanner
    45  CREATE TABLE IF NOT EXISTS scanned_layer (
    46  	layer_hash text REFERENCES layer(hash),
    47  	scanner_id bigint REFERENCES scanner(id),
    48  	PRIMARY KEY(layer_hash, scanner_id)
    49  );
    50  
    51  --- ScannerList
    52  --- a relation informing us if a manifest hash has
    53  --- been scanned by a particular scanner
    54  CREATE TABLE IF NOT EXISTS scannerlist (
    55  	id BIGSERIAL PRIMARY KEY,
    56  	manifest_hash text,
    57  	scanner_id bigint REFERENCES scanner(id)
    58  );
    59  CREATE INDEX IF NOT EXISTS scannerlist_manifest_hash_idx ON scannerlist (manifest_hash);
    60  
    61  --- IndexReport
    62  --- the jsonb serialized result of a scan for a particular
    63  --- manifest
    64  CREATE TABLE IF NOT EXISTS indexreport (
    65  	manifest_hash text PRIMARY KEY REFERENCES manifest(hash),
    66  	state text,
    67  	scan_result jsonb
    68  );
    69  
    70  -- Distribution
    71  --- a unique distribution discovered by a scanner
    72  CREATE TABLE IF NOT EXISTS dist (
    73  	id BIGSERIAL PRIMARY KEY,
    74  	name text NOT NULL DEFAULT '',
    75  	did text NOT NULL DEFAULT '', -- os-release id field
    76  	version text NOT NULL DEFAULT '',
    77  	version_code_name text NOT NULL DEFAULT '',
    78  	version_id text NOT NULL DEFAULT '',
    79  	arch text NOT NULL DEFAULT '',
    80  	cpe text NOT NULL DEFAULT '',
    81  	pretty_name text NOT NULL DEFAULT ''
    82  );
    83  CREATE UNIQUE INDEX IF NOT EXISTS dist_unique_idx ON dist (name, did, version, version_code_name, version_id, arch, cpe, pretty_name);
    84  
    85  --- DistributionScanArtifact
    86  --- A relation linking discovered distributions to a layer
    87  CREATE TABLE IF NOT EXISTS dist_scanartifact (
    88  	  dist_id bigint REFERENCES dist(id),
    89  	  scanner_id bigint REFERENCES scanner(id),
    90  	  layer_hash text REFERENCES layer(hash),
    91  	  PRIMARY KEY(dist_id, scanner_id, layer_hash)
    92  );
    93  CREATE INDEX IF NOT EXISTS dist_scanartifact_lookup_idx ON dist_scanartifact(layer_hash);
    94  
    95  --- Package
    96  --- a unique package discovered by a scanner
    97  CREATE TABLE IF NOT EXISTS package (
    98  	id BIGSERIAL PRIMARY KEY,
    99  	name text NOT NULL,
   100  	kind text NOT NULL DEFAULT '',
   101  	version text NOT NULL DEFAULT '',
   102  	norm_kind text,
   103  	norm_version integer[10],
   104  	module text NOT NULL DEFAULT '',
   105  	arch text NOT NULL DEFAULT ''
   106  );
   107  CREATE UNIQUE INDEX IF NOT EXISTS package_unique_idx ON package (name, version, kind, module, arch);
   108  
   109  --- PackageScanArtifact
   110  --- A relation linking discovered packages with the
   111  --- layer hash it was found
   112  CREATE TABLE IF NOT EXISTS package_scanartifact (
   113  	   layer_hash text REFERENCES layer(hash),
   114  	   package_id bigint REFERENCES package(id),
   115  	   source_id bigint REFERENCES package(id),
   116  	   scanner_id bigint REFERENCES scanner(id),
   117  	   package_db text,
   118  	   repository_hint text,
   119  	   PRIMARY KEY(layer_hash, package_id, source_id, scanner_id, package_db, repository_hint)
   120  );
   121  CREATE INDEX IF NOT EXISTS package_scanartifact_lookup_idx ON package_scanartifact(layer_hash);
   122  
   123  --- Repository
   124  --- a unique package repository discovered by a scanner
   125  CREATE TABLE IF NOT EXISTS repo (
   126  	id BIGSERIAL PRIMARY KEY,
   127  	name text NOT NULL,
   128  	key text DEFAULT '',
   129  	uri text DEFAULT '',
   130  	cpe text DEFAULT ''
   131  );
   132  CREATE UNIQUE INDEX IF NOT EXISTS repo_unique_idx ON repo (name, key, uri);
   133  
   134  --- RepositoryScanArtifact
   135  --- A relation linking discovered distributions to a layer
   136  CREATE TABLE IF NOT EXISTS repo_scanartifact (
   137  	repo_id bigint REFERENCES repo(id),
   138  	scanner_id bigint REFERENCES scanner(id),
   139  	layer_hash text REFERENCES layer(hash),
   140  	PRIMARY KEY(repo_id, scanner_id, layer_hash)
   141  );
   142  CREATE INDEX IF NOT EXISTS repo_scanartifact_lookup_idx ON repo_scanartifact(layer_hash);
   143  
   144  --- ManifestIndex
   145  --- A searchable index of a coalesced manifest's content.
   146  --- a package id is required.
   147  --- either a dist_id or a repo_id maybe null, but not both
   148  CREATE TABLE IF NOT EXISTS manifest_index
   149  (
   150  	id            bigserial PRIMARY KEY,
   151  	package_id    bigint NOT NULL REFERENCES package (id),
   152  	dist_id       bigint REFERENCES dist (id),
   153  	repo_id       bigint REFERENCES repo (id),
   154  	manifest_hash text REFERENCES manifest (hash)
   155  );
   156  CREATE INDEX IF NOT EXISTS manifest_index_hash_lookup_idx ON manifest_index (manifest_hash);
   157  CREATE UNIQUE INDEX manifest_index_unique ON manifest_index (package_id, COALESCE(dist_id, 0), COALESCE(repo_id, 0), manifest_hash);