From cc6cb9bd1b9b2ff48cdababaa5d9b6ae6d781e5d Mon Sep 17 00:00:00 2001
From: Manoj M J <mmj@gitlab.com>
Date: Thu, 11 Apr 2024 16:44:37 +0200
Subject: [PATCH] Add relaxed sharding keys for devops_reports

Add relaxed sharding keys for feature category `devops_reports`.

These tables have been identified as
[cell local tables](https://docs.gitlab.com/ee/development/database/multiple_databases.html#guidelines-on-choosing-between-gitlab_main_cell-and-gitlab_main_clusterwide-schema).

All cell local tables require a
[sharding key](https://docs.gitlab.com/ee/development/database/multiple_databases.html#defining-a-sharding-key-for-all-cell-local-tables).

A "relaxed" sharding key has been automatically selected for these tables, referencing either `projects` or `namespaces`, or a combination of both.

The term "relaxed" is used because:

- normally, a sharding key needs to have a NOT NULL constraint on the database.
- But, "relaxed" sharding keys do not have NOT NULL constraints. However, we have verified via database-lab that these
  columns do not contain any NULL values across any rows in the table.

This means that while the NOT NULL constraint is missing, the data itself is clean, so we can always add a NOT NULL constraint after the classification of these tables are completed.

Reviwer, please confirm that each table should indeed be cell local, and that the selected column is an appropriate
sharding key. When you are finished, please request a review from the database maintainer suggested by Danger.

If you have any questions or concerns, reach out to `@manojmj` or `@tigerwnz`.

If you would like to go through similar merged MRs so as to gather an understanding on this topic, you
can use [this](https://gitlab.com/gitlab-org/gitlab/-/merge_requests?scope=all&state=merged&label_name[]=automation%3Agitlab-housekeeper-authored) link.

This change was generated by
[gitlab-housekeeper](https://gitlab.com/gitlab-org/gitlab/-/tree/master/gems/gitlab-housekeeper)
using the Keeps::DetermineRelaxedShardingKey keep.

To provide feedback on your experience with `gitlab-housekeeper` please comment in
<https://gitlab.com/gitlab-org/gitlab/-/issues/442003>.

Changelog: other
---
 db/docs/analytics_devops_adoption_segments.yml | 10 +++++++++-
 spec/lib/gitlab/database/sharding_key_spec.rb  |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/db/docs/analytics_devops_adoption_segments.yml b/db/docs/analytics_devops_adoption_segments.yml
index 42f04f5760803..3bc5952f15f43 100644
--- a/db/docs/analytics_devops_adoption_segments.yml
+++ b/db/docs/analytics_devops_adoption_segments.yml
@@ -7,4 +7,12 @@ feature_categories:
 description: Stores a reference to the Namespace which is enabled for the DevOps report.
 introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/45748
 milestone: '13.6'
-gitlab_schema: gitlab_main
+gitlab_schema: gitlab_main_cell
+allow_cross_joins:
+- gitlab_main_clusterwide
+allow_cross_transactions:
+- gitlab_main_clusterwide
+allow_cross_foreign_keys:
+- gitlab_main_clusterwide
+sharding_key:
+  namespace_id: namespaces
diff --git a/spec/lib/gitlab/database/sharding_key_spec.rb b/spec/lib/gitlab/database/sharding_key_spec.rb
index 1fdbe9bf087cb..1a3c9783cbd27 100644
--- a/spec/lib/gitlab/database/sharding_key_spec.rb
+++ b/spec/lib/gitlab/database/sharding_key_spec.rb
@@ -23,6 +23,7 @@
   let(:allowed_to_be_missing_not_null) do
     [
       *tables_with_alternative_not_null_constraint,
+      'analytics_devops_adoption_segments.namespace_id',
       *['boards.project_id', 'boards.group_id'],
       'ci_pipeline_schedules.project_id',
       'ci_sources_pipelines.project_id',
-- 
GitLab