From 672c854e755ff6ca98895a4294929f176c86961c Mon Sep 17 00:00:00 2001
From: Arturo Herrero <arturo.herrero@gmail.com>
Date: Thu, 25 Mar 2021 11:26:41 +0000
Subject: [PATCH] Add foreign key from web_hooks to groups

This foreign key constraint prevents creating inconsistent records.
---
 .../202423-foreign-key-webhooks-groups.yml    |  5 +++++
 ...dd_not_valid_foreign_key_to_group_hooks.rb | 19 +++++++++++++++++++
 db/schema_migrations/20210325092215           |  1 +
 db/structure.sql                              |  3 +++
 spec/db/schema_spec.rb                        |  1 -
 5 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/unreleased/202423-foreign-key-webhooks-groups.yml
 create mode 100644 db/migrate/20210325092215_add_not_valid_foreign_key_to_group_hooks.rb
 create mode 100644 db/schema_migrations/20210325092215

diff --git a/changelogs/unreleased/202423-foreign-key-webhooks-groups.yml b/changelogs/unreleased/202423-foreign-key-webhooks-groups.yml
new file mode 100644
index 0000000000000..bb1f0de8eb4f3
--- /dev/null
+++ b/changelogs/unreleased/202423-foreign-key-webhooks-groups.yml
@@ -0,0 +1,5 @@
+---
+title: Add foreign key from web_hooks to groups
+merge_request: 57735
+author:
+type: other
diff --git a/db/migrate/20210325092215_add_not_valid_foreign_key_to_group_hooks.rb b/db/migrate/20210325092215_add_not_valid_foreign_key_to_group_hooks.rb
new file mode 100644
index 0000000000000..2389f90d498fe
--- /dev/null
+++ b/db/migrate/20210325092215_add_not_valid_foreign_key_to_group_hooks.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class AddNotValidForeignKeyToGroupHooks < ActiveRecord::Migration[6.0]
+  include Gitlab::Database::MigrationHelpers
+
+  DOWNTIME = false
+
+  def up
+    with_lock_retries do
+      add_foreign_key :web_hooks, :namespaces, column: :group_id, on_delete: :cascade, validate: false
+    end
+  end
+
+  def down
+    with_lock_retries do
+      remove_foreign_key_if_exists :web_hooks, column: :group_id
+    end
+  end
+end
diff --git a/db/schema_migrations/20210325092215 b/db/schema_migrations/20210325092215
new file mode 100644
index 0000000000000..0693c47994b48
--- /dev/null
+++ b/db/schema_migrations/20210325092215
@@ -0,0 +1 @@
+ea819fd401c5566986fd495ed3b8aa0d296d6c9e3fedf2a10f34cb7fbaeedb20
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 96e1a788f3151..836122f609726 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -26571,6 +26571,9 @@ ALTER TABLE ONLY requirements_management_test_reports
 ALTER TABLE ONLY pool_repositories
     ADD CONSTRAINT fk_rails_d2711daad4 FOREIGN KEY (source_project_id) REFERENCES projects(id) ON DELETE SET NULL;
 
+ALTER TABLE ONLY web_hooks
+    ADD CONSTRAINT fk_rails_d35697648e FOREIGN KEY (group_id) REFERENCES namespaces(id) ON DELETE CASCADE NOT VALID;
+
 ALTER TABLE ONLY group_group_links
     ADD CONSTRAINT fk_rails_d3a0488427 FOREIGN KEY (shared_group_id) REFERENCES namespaces(id) ON DELETE CASCADE;
 
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb
index d292ba60a1211..0d8407dd7facc 100644
--- a/spec/db/schema_spec.rb
+++ b/spec/db/schema_spec.rb
@@ -87,7 +87,6 @@
     users_star_projects: %w[user_id],
     vulnerability_identifiers: %w[external_id],
     vulnerability_scanners: %w[external_id],
-    web_hooks: %w[group_id],
     web_hook_logs_part_0c5294f417: %w[web_hook_id]
   }.with_indifferent_access.freeze
 
-- 
GitLab