Skip to content

Commit 8968679

Browse files
committed
fixes migration
1 parent b2053b4 commit 8968679

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

database/migrations/20260415090935_refactor-vulndb-tables.up.sql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ ALTER TABLE public.affected_components DROP COLUMN IF EXISTS qualifiers;
1010
ALTER TABLE public.dependency_vulns DROP CONSTRAINT IF EXISTS fk_dependency_vulns_cve;
1111

1212
-- Truncate all vulndb tables; CASCADE cleans up internal FKs automatically
13-
TRUNCATE public.cves, public.affected_components, public.malicious_packages, public.malicious_affected_components CASCADE;
13+
TRUNCATE public.cves, public.affected_components, public.malicious_packages, public.malicious_affected_components, public.exploits, public.weaknesses, public.cve_affected_component, public.cve_relationships CASCADE;
1414

1515
-- Drop primary keys so we can redefine the column types (look up actual constraint names to handle renames)
1616
DO $$ DECLARE r record;
1717
BEGIN
1818
FOR r IN SELECT constraint_name FROM information_schema.table_constraints
1919
WHERE table_schema = 'public' AND table_name = 'affected_components' AND constraint_type = 'PRIMARY KEY'
20-
LOOP EXECUTE 'ALTER TABLE public.affected_components DROP CONSTRAINT ' || quote_ident(r.constraint_name); END LOOP;
20+
LOOP EXECUTE 'ALTER TABLE public.affected_components DROP CONSTRAINT ' || quote_ident(r.constraint_name) || ' CASCADE'; END LOOP;
2121
FOR r IN SELECT constraint_name FROM information_schema.table_constraints
2222
WHERE table_schema = 'public' AND table_name = 'cve_affected_component' AND constraint_type = 'PRIMARY KEY'
23-
LOOP EXECUTE 'ALTER TABLE public.cve_affected_component DROP CONSTRAINT ' || quote_ident(r.constraint_name); END LOOP;
23+
LOOP EXECUTE 'ALTER TABLE public.cve_affected_component DROP CONSTRAINT ' || quote_ident(r.constraint_name) || ' CASCADE'; END LOOP;
2424
FOR r IN SELECT constraint_name FROM information_schema.table_constraints
2525
WHERE table_schema = 'public' AND table_name = 'cves' AND constraint_type = 'PRIMARY KEY'
26-
LOOP EXECUTE 'ALTER TABLE public.cves DROP CONSTRAINT ' || quote_ident(r.constraint_name); END LOOP;
26+
LOOP EXECUTE 'ALTER TABLE public.cves DROP CONSTRAINT ' || quote_ident(r.constraint_name) || ' CASCADE'; END LOOP;
2727
END $$;
2828

2929
-- Rebuild affected_components with bigint id
@@ -48,10 +48,9 @@ ALTER TABLE public.cves ADD CONSTRAINT cves_cve_unique UNIQUE (cve);
4848
-- Re-add foreign key constraints
4949
ALTER TABLE public.cve_affected_component ADD CONSTRAINT fk_cve_affected_component_affected_component FOREIGN KEY (affected_component_id) REFERENCES public.affected_components(id) ON DELETE CASCADE;
5050
ALTER TABLE public.cve_affected_component ADD CONSTRAINT fk_cve_affected_component_cve FOREIGN KEY (cve_id) REFERENCES public.cves(id) ON DELETE CASCADE;
51-
ALTER TABLE public.dependency_vulns ADD CONSTRAINT fk_dependency_vulns_cve FOREIGN KEY (cve_id) REFERENCES public.cves(cve) ON DELETE CASCADE;
5251
ALTER TABLE public.exploits ADD CONSTRAINT fk_cves_exploits FOREIGN KEY (cve_id) REFERENCES public.cves(cve) ON DELETE CASCADE;
5352
ALTER TABLE public.weaknesses ADD CONSTRAINT fk_cves_weaknesses FOREIGN KEY (cve_id) REFERENCES public.cves(cve) ON DELETE CASCADE;
54-
ALTER TABLE public.vex_rules ADD CONSTRAINT fk_vex_rules_cve FOREIGN KEY (cve_id) REFERENCES public.cves(cve) ON DELETE CASCADE;
53+
-- fk_vex_rules_cve is added after the full vulndb import in vulndb_service.go to avoid orphan violations
5554
ALTER TABLE public.cve_relationships ADD CONSTRAINT fk_cve_relationships_source FOREIGN KEY (source_cve) REFERENCES public.cves(cve) ON DELETE CASCADE;
5655

5756
-- Drop unnecessary indexes; we add more optimized ones at the end

vulndb/osv_service.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (s osvService) applyOSVEntries(ctx context.Context, tx pgx.Tx, osvVulns []O
127127
return nil
128128
}
129129

130-
rows, err := buildVulnDBRows(ctx, s.affectedCmpRepository, osvVulns)
130+
rows, err := buildVulnDBRows(ctx, tx, osvVulns)
131131
if err != nil {
132132
return fmt.Errorf("could not build rows from osv objects: %w", err)
133133
}
@@ -195,7 +195,7 @@ func (s osvService) fetchAndImportOSV(ctx context.Context, tx pgx.Tx, importStar
195195
return -v1.ModifiedTimestamp.Compare(v2.ModifiedTimestamp)
196196
})
197197

198-
rows, err := buildVulnDBRows(ctx, s.affectedCmpRepository, allOSVVulns)
198+
rows, err := buildVulnDBRows(ctx, tx, allOSVVulns)
199199
if err != nil {
200200
return nil, nil, fmt.Errorf("could not build vulndb rows: %w", err)
201201
}
@@ -335,14 +335,24 @@ func (s osvService) zipWorkerFunction(zipWorkWaitGroup *sync.WaitGroup, zipJobs
335335
}
336336

337337
// build all the vuln database rows from the OSV objects
338-
func buildVulnDBRows(ctx context.Context, affectedCmpRepository shared.AffectedComponentRepository, allEntries []OSVEntry) (vulndbRows, error) {
338+
func buildVulnDBRows(ctx context.Context, tx pgx.Tx, allEntries []OSVEntry) (vulndbRows, error) {
339339
// get the current state of the affected components to avoid creating duplicate entries
340340
currentCVEAffectedComponents := make([]cveAffectedComponentRow, 0, len(allEntries)*55)
341-
err := affectedCmpRepository.GetDB(ctx, nil).Raw(`SELECT * FROM cve_affected_component;`).Find(&currentCVEAffectedComponents).Error
341+
rows, err := tx.Query(ctx, `SELECT affected_component_id, cve_id FROM cve_affected_component`)
342342
if err != nil {
343343
return vulndbRows{}, fmt.Errorf("could not get current state of affected components: %w", err)
344344
}
345345

346+
// convert the rows to a slice of cveAffectedComponentRow
347+
for rows.Next() {
348+
var row cveAffectedComponentRow
349+
if err := rows.Scan(&row.AffectedComponentID, &row.CveID); err != nil {
350+
rows.Close()
351+
return vulndbRows{}, fmt.Errorf("could not scan cve_affected_component row: %w", err)
352+
}
353+
currentCVEAffectedComponents = append(currentCVEAffectedComponents, row)
354+
}
355+
346356
// build a map of the current state for faster lookups of the existing state
347357
// used for deduplicating rows in memory rather than on insert
348358
isAffectedComponentPresent := make(map[int64]struct{}, len(currentCVEAffectedComponents))
@@ -399,7 +409,7 @@ func buildVulnDBRows(ctx context.Context, affectedCmpRepository shared.AffectedC
399409
}
400410
}
401411
}
402-
slog.Info("finished building rows", "building time", time.Since(buildingTime))
412+
slog.Info("finished building rows", "buildingTime", time.Since(buildingTime))
403413
return vulndbRows{CVEs: cves, CVERelationships: cveRelationships, AffectedComponents: affectedComponents, CVEAffectedComponents: cveAffectedComponents}, nil
404414
}
405415

0 commit comments

Comments
 (0)