From c25a7ca68d3ad1ec4bf8e7278cccfa210f7e174b Mon Sep 17 00:00:00 2001 From: "stonezdj(Daojun Zhang)" Date: Tue, 25 Jul 2023 19:41:35 +0800 Subject: [PATCH] Skip to run migrate script when data available (#18976) --- .../postgresql/0120_2.9.0_schema.up.sql | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/make/migrations/postgresql/0120_2.9.0_schema.up.sql b/make/migrations/postgresql/0120_2.9.0_schema.up.sql index a44d26fdf..19c4a5c3d 100644 --- a/make/migrations/postgresql/0120_2.9.0_schema.up.sql +++ b/make/migrations/postgresql/0120_2.9.0_schema.up.sql @@ -7,9 +7,14 @@ AND vendor_id IN (SELECT id FROM scanner_registration) AND vendor_type = 'IMAGE_SCAN'; /* extract score from vendor attribute */ -UPDATE vulnerability_record -SET cvss_score_v3 = (vendor_attributes->'CVSS'->'nvd'->>'V3Score')::double precision -WHERE jsonb_path_exists(vendor_attributes::jsonb, '$.CVSS.nvd.V3Score'); +DO $$ +BEGIN + IF NOT EXISTS (SELECT 1 FROM vulnerability_record WHERE cvss_score_v3 IS NOT NULL LIMIT 1) THEN + UPDATE vulnerability_record + SET cvss_score_v3 = (vendor_attributes->'CVSS'->'nvd'->>'V3Score')::double precision + WHERE jsonb_path_exists(vendor_attributes::jsonb, '$.CVSS.nvd.V3Score'); + END IF; +END $$; CREATE INDEX IF NOT EXISTS idx_vulnerability_record_cvss_score_v3 ON vulnerability_record (cvss_score_v3); CREATE INDEX IF NOT EXISTS idx_vulnerability_registration_uuid ON vulnerability_record (registration_uuid); @@ -40,6 +45,18 @@ $$ unknown_count BIGINT; fixable_count BIGINT; BEGIN + IF EXISTS (SELECT 1 + FROM scan_report + WHERE critical_cnt IS NOT NULL + AND high_cnt IS NOT NULL + AND medium_cnt IS NOT NULL + AND low_cnt IS NOT NULL + AND unknown_cnt IS NOT NULL + AND fixable_cnt IS NOT NULL + LIMIT 1) THEN + RETURN; + END IF; + FOR report IN SELECT uuid FROM scan_report LOOP critical_count := 0;