From c9726c40167e52e9b3fd0a1748b8281877883584 Mon Sep 17 00:00:00 2001
From: Kerri Miller <kerrizor@kerrizor.com>
Date: Wed, 30 Aug 2023 13:18:30 +0000
Subject: [PATCH] Add patch_id_sha column to approvals

Add `patch_id_sha` column to eventually record the `patch_id`
of the most-current `merge_request_diff` when the approval is
created. We associate to `patch_id` rather than setting up an
ActiveRecord association to a `MergeRequestDiff` since the
approval is given against a _diff_, and the same diff (from a
functional perspective) can be included in multiple `MergeRequestDiff`
records (for instance, when an MR is rebased, the diff itself doesn't
functionally change, but a new `MergeRequestDiff` record is created.)
Adding this data to `approvals` records will help us detect when an
approval is still valid, even if new `MergeRequestDiffs` are created.

Related to https://gitlab.com/gitlab-org/gitlab/-/issues/422818

Prior related work: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/126920

Changelog: added
---
 app/models/approval.rb                              |  3 +++
 .../20230823174108_add_patch_id_sha_on_approvals.rb | 13 +++++++++++++
 db/schema_migrations/20230823174108                 |  1 +
 db/structure.sql                                    |  3 ++-
 4 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 db/migrate/20230823174108_add_patch_id_sha_on_approvals.rb
 create mode 100644 db/schema_migrations/20230823174108

diff --git a/app/models/approval.rb b/app/models/approval.rb
index 9ded44fe42556..ecc15077c8dfb 100644
--- a/app/models/approval.rb
+++ b/app/models/approval.rb
@@ -3,10 +3,13 @@
 class Approval < ApplicationRecord
   include CreatedAtFilterable
   include Importable
+  include ShaAttribute
 
   belongs_to :user
   belongs_to :merge_request
 
+  sha_attribute :patch_id_sha
+
   validates :merge_request_id, presence: true, unless: :importing?
   validates :user_id, presence: true, uniqueness: { scope: [:merge_request_id] }
 
diff --git a/db/migrate/20230823174108_add_patch_id_sha_on_approvals.rb b/db/migrate/20230823174108_add_patch_id_sha_on_approvals.rb
new file mode 100644
index 0000000000000..ef68b66d2e829
--- /dev/null
+++ b/db/migrate/20230823174108_add_patch_id_sha_on_approvals.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class AddPatchIdShaOnApprovals < Gitlab::Database::Migration[2.1]
+  enable_lock_retries!
+
+  def up
+    add_column :approvals, :patch_id_sha, :binary
+  end
+
+  def down
+    remove_column :approvals, :patch_id_sha
+  end
+end
diff --git a/db/schema_migrations/20230823174108 b/db/schema_migrations/20230823174108
new file mode 100644
index 0000000000000..32d7292f449fc
--- /dev/null
+++ b/db/schema_migrations/20230823174108
@@ -0,0 +1 @@
+ad75672df6de231df6d2702554e3416b0ba7c4c07cc4ace3d83595038f36b4e6
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 1c2e81950c448..ec8c20dfcd6ce 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -12200,7 +12200,8 @@ CREATE TABLE approvals (
     merge_request_id integer NOT NULL,
     user_id integer NOT NULL,
     created_at timestamp without time zone,
-    updated_at timestamp without time zone
+    updated_at timestamp without time zone,
+    patch_id_sha bytea
 );
 
 CREATE SEQUENCE approvals_id_seq
-- 
GitLab