@@ -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