From 83472208a7c3e9cead4fe9ad3bd3c7dcfe83130e Mon Sep 17 00:00:00 2001
From: Shane Maglangit <smaglangit@gitlab.com>
Date: Thu, 20 Feb 2025 04:15:15 +0000
Subject: [PATCH] Validate NOT NULL on project_relation_exports sharding key

Validate the NOT NULL constraint on the project_id
column on the project_relation_exports table

Changelog: other
---
 db/docs/project_relation_export_uploads.yml         |  1 -
 db/docs/project_relation_exports.yml                | 12 ++----------
 ...lation_exports_project_id_not_null_constraint.rb | 13 +++++++++++++
 db/schema_migrations/20250210065256                 |  1 +
 db/structure.sql                                    |  6 ++----
 5 files changed, 18 insertions(+), 15 deletions(-)
 create mode 100644 db/post_migrate/20250210065256_validate_project_relation_exports_project_id_not_null_constraint.rb
 create mode 100644 db/schema_migrations/20250210065256

diff --git a/db/docs/project_relation_export_uploads.yml b/db/docs/project_relation_export_uploads.yml
index a184583a2620..59956d482e61 100644
--- a/db/docs/project_relation_export_uploads.yml
+++ b/db/docs/project_relation_export_uploads.yml
@@ -17,5 +17,4 @@ desired_sharding_key:
         table: project_relation_exports
         sharding_key: project_id
         belongs_to: relation_export
-    awaiting_backfill_on_parent: true
 table_size: small
diff --git a/db/docs/project_relation_exports.yml b/db/docs/project_relation_exports.yml
index aa4277af68ae..b94ceab10097 100644
--- a/db/docs/project_relation_exports.yml
+++ b/db/docs/project_relation_exports.yml
@@ -8,14 +8,6 @@ description: Used to track the generation of relation export files for projects
 introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/90624
 milestone: '15.2'
 gitlab_schema: gitlab_main_cell
-desired_sharding_key:
-  project_id:
-    references: projects
-    backfill_via:
-      parent:
-        foreign_key: project_export_job_id
-        table: project_export_jobs
-        sharding_key: project_id
-        belongs_to: project_export_job
-desired_sharding_key_migration_job_name: BackfillProjectRelationExportsProjectId
 table_size: small
+sharding_key:
+  project_id: projects
diff --git a/db/post_migrate/20250210065256_validate_project_relation_exports_project_id_not_null_constraint.rb b/db/post_migrate/20250210065256_validate_project_relation_exports_project_id_not_null_constraint.rb
new file mode 100644
index 000000000000..dbb34e255b7a
--- /dev/null
+++ b/db/post_migrate/20250210065256_validate_project_relation_exports_project_id_not_null_constraint.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class ValidateProjectRelationExportsProjectIdNotNullConstraint < Gitlab::Database::Migration[2.2]
+  milestone '17.10'
+
+  def up
+    validate_not_null_constraint :project_relation_exports, :project_id
+  end
+
+  def down
+    # no-op
+  end
+end
diff --git a/db/schema_migrations/20250210065256 b/db/schema_migrations/20250210065256
new file mode 100644
index 000000000000..933fe853235f
--- /dev/null
+++ b/db/schema_migrations/20250210065256
@@ -0,0 +1 @@
+9f0cfe2f27bf8bf3e0e8000df91230a893118064fd115077e7722c680a5ae7e2
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 294c8afc0cfd..3dbf7639109b 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -19797,7 +19797,8 @@ CREATE TABLE project_relation_exports (
     project_id bigint,
     CONSTRAINT check_15e644d856 CHECK ((char_length(jid) <= 255)),
     CONSTRAINT check_4b5880b795 CHECK ((char_length(relation) <= 255)),
-    CONSTRAINT check_dbd1cf73d0 CHECK ((char_length(export_error) <= 300))
+    CONSTRAINT check_dbd1cf73d0 CHECK ((char_length(export_error) <= 300)),
+    CONSTRAINT check_f461e3537f CHECK ((project_id IS NOT NULL))
 );
 
 CREATE SEQUENCE project_relation_exports_id_seq
@@ -27251,9 +27252,6 @@ ALTER TABLE sprints
 ALTER TABLE web_hook_logs
     ADD CONSTRAINT check_df72cb58f5 CHECK ((char_length(url_hash) <= 44)) NOT VALID;
 
-ALTER TABLE project_relation_exports
-    ADD CONSTRAINT check_f461e3537f CHECK ((project_id IS NOT NULL)) NOT VALID;
-
 ALTER TABLE merge_request_blocks
     ADD CONSTRAINT check_f8034ca45e CHECK ((project_id IS NOT NULL)) NOT VALID;
 
-- 
GitLab