Skip to content

Commit

Permalink
fix: store package_name_id for fixed csaf_product
Browse files Browse the repository at this point in the history
RHINENG-10310
  • Loading branch information
psegedy authored and jdobes committed Jun 6, 2024
1 parent fe4a4e0 commit bd09fe2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
8 changes: 8 additions & 0 deletions database/upgrade_scripts/022-csaf-fixed-package-name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE csaf_product DROP CONSTRAINT pkg_id;

WITH cte AS (
SELECT cp.id as product_id, p.name_id FROM csaf_product cp JOIN package p ON cp.package_id = p.id
)
UPDATE csaf_product cp SET package_name_id = cte.name_id FROM cte WHERE cp.id = cte.product_id;

ALTER TABLE csaf_product ALTER COLUMN package_name_id SET NOT NULL;
9 changes: 3 additions & 6 deletions database/vmaas_db_postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CREATE TABLE IF NOT EXISTS db_version (
)TABLESPACE pg_default;

-- Increment this when editing this file
INSERT INTO db_version (name, version) VALUES ('schema_version', 21);
INSERT INTO db_version (name, version) VALUES ('schema_version', 22);

-- -----------------------------------------------------
-- evr type
Expand Down Expand Up @@ -1312,7 +1312,7 @@ ON CONFLICT DO NOTHING;
CREATE TABLE IF NOT EXISTS csaf_product (
id SERIAL,
cpe_id INT NOT NULL,
package_name_id INT NULL,
package_name_id INT NOT NULL,
package_id INT NULL,
module_stream TEXT NULL CHECK (NOT empty(module_stream)),
PRIMARY KEY (id),
Expand All @@ -1324,10 +1324,7 @@ CREATE TABLE IF NOT EXISTS csaf_product (
REFERENCES package_name (id),
CONSTRAINT package_id
FOREIGN KEY (package_id)
REFERENCES package (id),
CONSTRAINT pkg_id CHECK(
(package_name_id IS NOT NULL AND package_id IS NULL) OR
(package_name_id IS NULL AND package_id IS NOT NULL))
REFERENCES package (id)
)TABLESPACE pg_default;

CREATE UNIQUE INDEX ON csaf_product(cpe_id, package_name_id) WHERE package_name_id IS NOT NULL AND package_id IS NULL AND module_stream IS NULL;
Expand Down
17 changes: 6 additions & 11 deletions vmaas/reposcan/database/csaf_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def _load_product_attr_ids(self, products: model.CsafProducts) -> None:
name_id = self.package_store.package_name_map[name]
evr_id = self.package_store.evr_map[(epoch, ver, rel)]
arch_id = self.package_store.arch_map[arch]
product.package_name_id = name_id
product.package_id = self._get_product_attr_id(
"package", self.package_store.package_map, value=(name_id, evr_id, arch_id)
)
Expand Down Expand Up @@ -153,35 +154,31 @@ def _split_product_data(self, products: model.CsafProducts) -> dict[str, dict[st

fields_unfixed = [cpe_field, package_name_field]
fields_unfixed_module = [cpe_field, package_name_field, module_field]
fields_fixed = [cpe_field, package_field]
fields_fixed_module = [cpe_field, package_field, module_field]
fields_fixed = [cpe_field, package_name_field, package_field]
fields_fixed_module = [cpe_field, package_name_field, package_field, module_field]

unfixed_module = {
"products": [],
"fields": sql.SQL(", ").join(fields_unfixed_module),
"module_null": not_null,
"package_name_null": not_null,
"package_null": null,
}
unfixed_no_module = {
"products": [],
"fields": sql.SQL(", ").join(fields_unfixed),
"module_null": null,
"package_name_null": not_null,
"package_null": null,
}
fixed_module = {
"products": [],
"fields": sql.SQL(", ").join(fields_fixed_module),
"module_null": not_null,
"package_name_null": null,
"package_null": not_null,
}
fixed_no_module = {
"products": [],
"fields": sql.SQL(", ").join(fields_fixed),
"module_null": null,
"package_name_null": null,
"package_null": not_null,
}

Expand All @@ -199,16 +196,16 @@ def _split_product_data(self, products: model.CsafProducts) -> dict[str, dict[st
if any(not isinstance(row[i], (int, str, type(None))) for i in range(4)):
raise TypeError(f"column of product row <{row}> is not of type (int, str, None)")

if row[1]: # unfixed
if row[1] and not row[2]: # unfixed
if row[3]: # has module
res["unfixed_module"]["products"].append((row[0], row[1], row[3])) # type: ignore[attr-defined]
else:
res["unfixed_no_module"]["products"].append((row[0], row[1])) # type: ignore[attr-defined]
elif row[2]: # fixed
if row[3]: # has module
res["fixed_module"]["products"].append((row[0], row[2], row[3])) # type: ignore[attr-defined]
res["fixed_module"]["products"].append((row[0], row[1], row[2], row[3])) # type: ignore[attr-defined]
else:
res["fixed_no_module"]["products"].append((row[0], row[2])) # type: ignore[attr-defined]
res["fixed_no_module"]["products"].append((row[0], row[1], row[2])) # type: ignore[attr-defined]
return res

def _update_product_ids(self, products: model.CsafProducts) -> None:
Expand All @@ -222,7 +219,6 @@ def _update_product_ids(self, products: model.CsafProducts) -> None:
FROM csaf_product
WHERE ({fields}) in %s
AND module_stream IS {module_null}
AND package_name_id IS {package_name_null}
AND package_id IS {package_null}
"""
)
Expand All @@ -237,7 +233,6 @@ def _update_product_ids(self, products: model.CsafProducts) -> None:
formatted_query = query.format(
fields=val["fields"], # type: ignore[arg-type]
module_null=val["module_null"], # type: ignore[arg-type]
package_name_null=val["package_name_null"], # type: ignore[arg-type]
package_null=val["package_null"], # type: ignore[arg-type]
)
cur.execute(formatted_query, (product_tuples,))
Expand Down
4 changes: 2 additions & 2 deletions vmaas/reposcan/database/test/test_csaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def products(self, csaf_store: CsafStore) -> t.Generator[None, None, None]:
)
products = (
(1000, 1000, 1000, None, None),
(1001, 1001, None, 1001, None),
(1001, 1001, 1001, 1001, None),
(1002, 1002, 1002, None, "module:1"),
(1003, 1003, None, 1003, "module:1"),
(1003, 1003, 1003, 1003, "module:1"),
)
cur = csaf_store.conn.cursor()
execute_values(cur, "INSERT INTO csaf_file(id, name, updated) VALUES %s RETURNING id", ((1, "file1", None),))
Expand Down

0 comments on commit bd09fe2

Please sign in to comment.