From cd56249b906ca67ee31abe8e1f1726c877b8b4b1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Zaj=C4=85c?= <mzajac@gitlab.com>
Date: Fri, 25 Oct 2024 14:50:21 +0000
Subject: [PATCH] Add `auto_resolved` column to `vulnerabilities` table

Changelog: added
---
 ...53_add_auto_resolved_to_vulnerabilities.rb |  9 +++++
 ...dd_auto_resolved_to_vulnerability_reads.rb |  9 +++++
 ...te_vulnerability_reads_trigger_function.rb | 40 +++++++++++++++++++
 db/schema_migrations/20241021141853           |  1 +
 db/schema_migrations/20241021143317           |  1 +
 db/schema_migrations/20241023134305           |  1 +
 db/structure.sql                              |  5 ++-
 7 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 db/migrate/20241021141853_add_auto_resolved_to_vulnerabilities.rb
 create mode 100644 db/migrate/20241021143317_add_auto_resolved_to_vulnerability_reads.rb
 create mode 100644 db/migrate/20241023134305_modify_update_vulnerability_reads_trigger_function.rb
 create mode 100644 db/schema_migrations/20241021141853
 create mode 100644 db/schema_migrations/20241021143317
 create mode 100644 db/schema_migrations/20241023134305

diff --git a/db/migrate/20241021141853_add_auto_resolved_to_vulnerabilities.rb b/db/migrate/20241021141853_add_auto_resolved_to_vulnerabilities.rb
new file mode 100644
index 0000000000000..e94c532cff61e
--- /dev/null
+++ b/db/migrate/20241021141853_add_auto_resolved_to_vulnerabilities.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+class AddAutoResolvedToVulnerabilities < Gitlab::Database::Migration[2.2]
+  milestone '17.6'
+
+  def change
+    add_column :vulnerabilities, :auto_resolved, :boolean, null: false, default: false, if_not_exists: true
+  end
+end
diff --git a/db/migrate/20241021143317_add_auto_resolved_to_vulnerability_reads.rb b/db/migrate/20241021143317_add_auto_resolved_to_vulnerability_reads.rb
new file mode 100644
index 0000000000000..0b716793c1fc1
--- /dev/null
+++ b/db/migrate/20241021143317_add_auto_resolved_to_vulnerability_reads.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+class AddAutoResolvedToVulnerabilityReads < Gitlab::Database::Migration[2.2]
+  milestone '17.6'
+
+  def change
+    add_column :vulnerability_reads, :auto_resolved, :boolean, null: false, default: false, if_not_exists: true
+  end
+end
diff --git a/db/migrate/20241023134305_modify_update_vulnerability_reads_trigger_function.rb b/db/migrate/20241023134305_modify_update_vulnerability_reads_trigger_function.rb
new file mode 100644
index 0000000000000..a01d585e29b81
--- /dev/null
+++ b/db/migrate/20241023134305_modify_update_vulnerability_reads_trigger_function.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+class ModifyUpdateVulnerabilityReadsTriggerFunction < Gitlab::Database::Migration[2.2]
+  include Gitlab::Database::SchemaHelpers
+
+  milestone '17.6'
+
+  FUNCTION_NAME = 'update_vulnerability_reads_from_vulnerability'
+
+  def up
+    create_trigger_function(FUNCTION_NAME, replace: true) do
+      <<~SQL
+        UPDATE
+          vulnerability_reads
+        SET
+          severity = NEW.severity,
+          state = NEW.state,
+          resolved_on_default_branch = NEW.resolved_on_default_branch,
+          auto_resolved = NEW.auto_resolved
+        WHERE vulnerability_id = NEW.id;
+        RETURN NULL;
+      SQL
+    end
+  end
+
+  def down
+    create_trigger_function(FUNCTION_NAME, replace: true) do
+      <<~SQL
+        UPDATE
+          vulnerability_reads
+        SET
+          severity = NEW.severity,
+          state = NEW.state,
+          resolved_on_default_branch = NEW.resolved_on_default_branch
+        WHERE vulnerability_id = NEW.id;
+        RETURN NULL;
+      SQL
+    end
+  end
+end
diff --git a/db/schema_migrations/20241021141853 b/db/schema_migrations/20241021141853
new file mode 100644
index 0000000000000..8a95230c78b46
--- /dev/null
+++ b/db/schema_migrations/20241021141853
@@ -0,0 +1 @@
+b5f1aa937a3284dc007b3d1464141cb91820127420d2f75e4bd83bd7fdd2cbc2
\ No newline at end of file
diff --git a/db/schema_migrations/20241021143317 b/db/schema_migrations/20241021143317
new file mode 100644
index 0000000000000..3f95e736ce7fc
--- /dev/null
+++ b/db/schema_migrations/20241021143317
@@ -0,0 +1 @@
+3af42e06ea9f989afd4b5dfe30bbc7b4a0c07df6abf4ea6c58546383e8687a7d
\ No newline at end of file
diff --git a/db/schema_migrations/20241023134305 b/db/schema_migrations/20241023134305
new file mode 100644
index 0000000000000..e9dd580b2c98c
--- /dev/null
+++ b/db/schema_migrations/20241023134305
@@ -0,0 +1 @@
+67b6b8af3c27a99440470303d1d8c85bd59ec6d59ae0ec68a3e49bcecc1d9d11
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index cdbcf563f2650..b5c466dc1b6de 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -2880,7 +2880,8 @@ UPDATE
 SET
   severity = NEW.severity,
   state = NEW.state,
-  resolved_on_default_branch = NEW.resolved_on_default_branch
+  resolved_on_default_branch = NEW.resolved_on_default_branch,
+  auto_resolved = NEW.auto_resolved
 WHERE vulnerability_id = NEW.id;
 RETURN NULL;
 
@@ -20372,6 +20373,7 @@ CREATE TABLE vulnerabilities (
     detected_at timestamp with time zone DEFAULT now(),
     finding_id bigint,
     cvss jsonb DEFAULT '[]'::jsonb,
+    auto_resolved boolean DEFAULT false NOT NULL,
     CONSTRAINT check_4d8a873f1f CHECK ((finding_id IS NOT NULL))
 );
 
@@ -20809,6 +20811,7 @@ CREATE TABLE vulnerability_reads (
     archived boolean DEFAULT false NOT NULL,
     identifier_names text[] DEFAULT '{}'::text[] NOT NULL,
     has_vulnerability_resolution boolean DEFAULT false,
+    auto_resolved boolean DEFAULT false NOT NULL,
     CONSTRAINT check_380451bdbe CHECK ((char_length(location_image) <= 2048)),
     CONSTRAINT check_4b1a1bf5ea CHECK ((has_merge_request IS NOT NULL)),
     CONSTRAINT check_a105eb825a CHECK ((char_length(cluster_agent_id) <= 10)),
-- 
GitLab