Skip to content

Commit 7172a7e

Browse files
committed
migrations: SQL formatting
This change adds a `generate` directive that runs an SQL formatter via `go run`. It also runs the formatter over the existing SQL. See-also: https://issues.redhat.com/browse/CLAIRDEV-85 Change-Id: I7ae14f596154b6f9bec66800c388bfe5ce732ca4 Signed-off-by: Hank Donnay <hdonnay@redhat.com>
1 parent 0e3e83c commit 7172a7e

22 files changed

Lines changed: 659 additions & 395 deletions
Lines changed: 101 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,183 @@
11
--- Layer
22
--- an identity table consisting of a content addressable layer hash
3-
CREATE TABLE IF NOT EXISTS layer (
4-
hash text PRIMARY KEY
5-
);
3+
CREATE TABLE IF NOT EXISTS layer (hash text PRIMARY KEY);
64

75
--- Manifest
86
--- an identity table consisting of a content addressable manifest hash
9-
CREATE TABLE IF NOT EXISTS manifest (
10-
hash text PRIMARY KEY
11-
);
7+
CREATE TABLE IF NOT EXISTS manifest (hash text PRIMARY KEY);
128

139
--- ManifestLayer
1410
--- a many to many link table identifying the layers which comprise a manifest
1511
--- and the layer's ordering within a manifest
1612
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)
13+
manifest_hash text REFERENCES manifest (hash),
14+
layer_hash text REFERENCES layer (hash),
15+
i bigint,
16+
PRIMARY KEY (manifest_hash, layer_hash, i)
2117
);
2218

2319
--- Scanner
2420
--- a unique versioned scanner which is responsible
2521
--- for finding packages and distributions in a layer
2622
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
23+
id BIGSERIAL PRIMARY KEY,
24+
name text NOT NULL,
25+
version text NOT NULL,
26+
kind text NOT NULL
3127
);
28+
3229
CREATE UNIQUE INDEX IF NOT EXISTS scanner_unique_idx ON scanner (name, kind, version);
3330

3431
--- ScannedManifest
3532
--- a relation to identify if a manifest was successfully scanned by a particular
3633
--- scanner
3734
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)
35+
manifest_hash text REFERENCES manifest (hash),
36+
scanner_id bigint REFERENCES scanner (id),
37+
PRIMARY KEY (manifest_hash, scanner_id)
4138
);
4239

4340
--- ScannedLayer
4441
--- a relation to identify if a layer was successfully scanned by a particular scanner
4542
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)
43+
layer_hash text REFERENCES layer (hash),
44+
scanner_id bigint REFERENCES scanner (id),
45+
PRIMARY KEY (layer_hash, scanner_id)
4946
);
5047

5148
--- ScannerList
5249
--- a relation informing us if a manifest hash has
5350
--- been scanned by a particular scanner
5451
CREATE TABLE IF NOT EXISTS scannerlist (
55-
id BIGSERIAL PRIMARY KEY,
56-
manifest_hash text,
57-
scanner_id bigint REFERENCES scanner(id)
52+
id BIGSERIAL PRIMARY KEY,
53+
manifest_hash text,
54+
scanner_id bigint REFERENCES scanner (id)
5855
);
56+
5957
CREATE INDEX IF NOT EXISTS scannerlist_manifest_hash_idx ON scannerlist (manifest_hash);
6058

6159
--- IndexReport
6260
--- the jsonb serialized result of a scan for a particular
6361
--- manifest
6462
CREATE TABLE IF NOT EXISTS indexreport (
65-
manifest_hash text PRIMARY KEY REFERENCES manifest(hash),
66-
state text,
67-
scan_result jsonb
63+
manifest_hash text PRIMARY KEY REFERENCES manifest (hash),
64+
state text,
65+
scan_result jsonb
6866
);
6967

7068
-- Distribution
7169
--- a unique distribution discovered by a scanner
7270
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 ''
71+
id BIGSERIAL PRIMARY KEY,
72+
name text NOT NULL DEFAULT '',
73+
did text NOT NULL DEFAULT '', -- os-release id field
74+
version text NOT NULL DEFAULT '',
75+
version_code_name text NOT NULL DEFAULT '',
76+
version_id text NOT NULL DEFAULT '',
77+
arch text NOT NULL DEFAULT '',
78+
cpe text NOT NULL DEFAULT '',
79+
pretty_name text NOT NULL DEFAULT ''
80+
);
81+
82+
CREATE UNIQUE INDEX IF NOT EXISTS dist_unique_idx ON dist (
83+
name,
84+
did,
85+
version,
86+
version_code_name,
87+
version_id,
88+
arch,
89+
cpe,
90+
pretty_name
8291
);
83-
CREATE UNIQUE INDEX IF NOT EXISTS dist_unique_idx ON dist (name, did, version, version_code_name, version_id, arch, cpe, pretty_name);
8492

8593
--- DistributionScanArtifact
8694
--- A relation linking discovered distributions to a layer
8795
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)
96+
dist_id bigint REFERENCES dist (id),
97+
scanner_id bigint REFERENCES scanner (id),
98+
layer_hash text REFERENCES layer (hash),
99+
PRIMARY KEY (dist_id, scanner_id, layer_hash)
92100
);
93-
CREATE INDEX IF NOT EXISTS dist_scanartifact_lookup_idx ON dist_scanartifact(layer_hash);
101+
102+
CREATE INDEX IF NOT EXISTS dist_scanartifact_lookup_idx ON dist_scanartifact (layer_hash);
94103

95104
--- Package
96105
--- a unique package discovered by a scanner
97106
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 ''
107+
id BIGSERIAL PRIMARY KEY,
108+
name text NOT NULL,
109+
kind text NOT NULL DEFAULT '',
110+
version text NOT NULL DEFAULT '',
111+
norm_kind text,
112+
norm_version integer[10],
113+
module text NOT NULL DEFAULT '',
114+
arch text NOT NULL DEFAULT ''
106115
);
116+
107117
CREATE UNIQUE INDEX IF NOT EXISTS package_unique_idx ON package (name, version, kind, module, arch);
108118

109119
--- PackageScanArtifact
110120
--- A relation linking discovered packages with the
111121
--- layer hash it was found
112122
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)
123+
layer_hash text REFERENCES layer (hash),
124+
package_id bigint REFERENCES package (id),
125+
source_id bigint REFERENCES package (id),
126+
scanner_id bigint REFERENCES scanner (id),
127+
package_db text,
128+
repository_hint text,
129+
PRIMARY KEY (
130+
layer_hash,
131+
package_id,
132+
source_id,
133+
scanner_id,
134+
package_db,
135+
repository_hint
136+
)
120137
);
121-
CREATE INDEX IF NOT EXISTS package_scanartifact_lookup_idx ON package_scanartifact(layer_hash);
138+
139+
CREATE INDEX IF NOT EXISTS package_scanartifact_lookup_idx ON package_scanartifact (layer_hash);
122140

123141
--- Repository
124142
--- a unique package repository discovered by a scanner
125143
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 ''
144+
id BIGSERIAL PRIMARY KEY,
145+
name text NOT NULL,
146+
key text DEFAULT '',
147+
uri text DEFAULT '',
148+
cpe text DEFAULT ''
131149
);
150+
132151
CREATE UNIQUE INDEX IF NOT EXISTS repo_unique_idx ON repo (name, key, uri);
133152

134153
--- RepositoryScanArtifact
135154
--- A relation linking discovered distributions to a layer
136155
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)
156+
repo_id bigint REFERENCES repo (id),
157+
scanner_id bigint REFERENCES scanner (id),
158+
layer_hash text REFERENCES layer (hash),
159+
PRIMARY KEY (repo_id, scanner_id, layer_hash)
141160
);
142-
CREATE INDEX IF NOT EXISTS repo_scanartifact_lookup_idx ON repo_scanartifact(layer_hash);
161+
162+
CREATE INDEX IF NOT EXISTS repo_scanartifact_lookup_idx ON repo_scanartifact (layer_hash);
143163

144164
--- ManifestIndex
145165
--- A searchable index of a coalesced manifest's content.
146166
--- a package id is required.
147167
--- 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)
168+
CREATE TABLE IF NOT EXISTS manifest_index (
169+
id bigserial PRIMARY KEY,
170+
package_id bigint NOT NULL REFERENCES package (id),
171+
dist_id bigint REFERENCES dist (id),
172+
repo_id bigint REFERENCES repo (id),
173+
manifest_hash text REFERENCES manifest (hash)
155174
);
175+
156176
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);
177+
178+
CREATE UNIQUE INDEX manifest_index_unique ON manifest_index (
179+
package_id,
180+
COALESCE(dist_id, 0),
181+
COALESCE(repo_id, 0),
182+
manifest_hash
183+
);

0 commit comments

Comments
 (0)