@@ -218,6 +218,12 @@ class CVEDB:
218
218
VALUES (?, ?)
219
219
""" ,
220
220
}
221
+ METRICS = [
222
+ (UNKNOWN_METRIC_ID , "UNKNOWN" ),
223
+ (EPSS_METRIC_ID , "EPSS" ),
224
+ (CVSS_2_METRIC_ID , "CVSS-2" ),
225
+ (CVSS_3_METRIC_ID , "CVSS-3" ),
226
+ ]
221
227
222
228
def __init__ (
223
229
self ,
@@ -368,7 +374,6 @@ def latest_schema(
368
374
369
375
# getting schema from command
370
376
lines = table_schema .split ("(" )[1 ].split ("," )
371
-
372
377
table_schema = [x .split ("\n " )[1 ].strip ().split (" " )[0 ] for x in lines ]
373
378
table_schema .pop ()
374
379
@@ -378,13 +383,16 @@ def latest_schema(
378
383
if table_schema == current_schema :
379
384
schema_latest = True
380
385
381
- # Check for metrics table schema
386
+ # Check for metrics table data integrity
382
387
if table_name == "metrics" :
383
- result = cursor .execute (
384
- "SELECT * FROM metrics WHERE metrics_id=?" , (UNKNOWN_METRIC_ID ,)
385
- )
386
- if not result .fetchone ():
387
- schema_latest = False
388
+ for metric_id , metric_name in self .METRICS :
389
+ result = cursor .execute (
390
+ "SELECT * FROM metrics WHERE metrics_id=? AND metrics_name=?" ,
391
+ (metric_id , metric_name ),
392
+ )
393
+ if not result .fetchone ():
394
+ schema_latest = False
395
+ break # Early exit if any metric is missing
388
396
389
397
return schema_latest
390
398
@@ -640,16 +648,9 @@ def populate_affected(self, affected_data, cursor, data_source):
640
648
def populate_metrics (self ):
641
649
"""Adding data to metric table."""
642
650
cursor = self .db_open_and_get_cursor ()
643
- # Insert a row without specifying cve_metrics_id
644
651
insert_metrics = self .INSERT_QUERIES ["insert_metrics" ]
645
- data = [
646
- (UNKNOWN_METRIC_ID , "UNKNOWN" ),
647
- (EPSS_METRIC_ID , "EPSS" ),
648
- (CVSS_2_METRIC_ID , "CVSS-2" ),
649
- (CVSS_3_METRIC_ID , "CVSS-3" ),
650
- ]
651
- # Execute the insert query for each row
652
- for row in data :
652
+ # Use the METRICS constant to populate the table
653
+ for row in self .METRICS :
653
654
cursor .execute (insert_metrics , row )
654
655
self .connection .commit ()
655
656
self .db_close ()
0 commit comments