From c6dd34067d58961037b2fb4692e47d322d7afdb8 Mon Sep 17 00:00:00 2001
From: Manoj M J <mmj@gitlab.com>
Date: Tue, 6 Feb 2024 10:47:02 +0100
Subject: [PATCH] Add desired sharding keys for value_stream_management

Add desired sharding keys for feature category `value_stream_management`.

These tables have been identified as a [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)
or a [desired sharding key](https://docs.gitlab.com/ee/development/database/multiple_databases.html#defining-a-desired_sharding_key-for-automatically-backfilling-a-sharding_key)

A desired sharding key has been automatically selected for these tables.
These keys were chosen as the desired sharding keys because the
table has a :belongs_to relationship to a table that itself has a `NOT NULL` sharding key.

Additionally, `gitlab_schema` has been set to `gitlab_main_cell` for any tables didn't use this schema already.
For these tables we have also added `allow_cross_joins`, `allow_cross_transactions` and
`allow_cross_foreign_keys`. These will silence any existing violations, allowing the pipeline to pass without
requiring further changes. In the future, we'll remove these `allow_...` statements and fix any violations as
they arise. You can read more about this in the [documentation for multiple databases](https://docs.gitlab.com/ee/development/database/multiple_databases.html)

We have assigned a random backend engineer from ~"group::optimize" to review these changes.

Please confirm that:

- each of these tables can be classified as cell local
- the selected desired sharding key is appropriate
- the backfill configuration for the desired sharding key is correct

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 @tigerwnz, @DylanGriffith or @manojmj.

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)

Changelog: other
---
 ...cs_cycle_analytics_value_stream_settings.yml | 17 ++++++++++++++++-
 db/docs/issue_assignment_events.yml             | 17 ++++++++++++++++-
 db/docs/issue_metrics.yml                       | 17 ++++++++++++++++-
 db/docs/merge_request_assignment_events.yml     | 17 ++++++++++++++++-
 4 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/db/docs/analytics_cycle_analytics_value_stream_settings.yml b/db/docs/analytics_cycle_analytics_value_stream_settings.yml
index b3947f9bf63a7..94708f963a1fa 100644
--- a/db/docs/analytics_cycle_analytics_value_stream_settings.yml
+++ b/db/docs/analytics_cycle_analytics_value_stream_settings.yml
@@ -7,4 +7,19 @@ feature_categories:
 description: Stores settings for each value stream.
 introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/132650
 milestone: '16.5'
-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
+desired_sharding_key:
+  namespace_id:
+    references: namespaces
+    backfill_via:
+      parent:
+        foreign_key: value_stream_id
+        table: analytics_cycle_analytics_group_value_streams
+        sharding_key: group_id
+        belongs_to: value_stream
diff --git a/db/docs/issue_assignment_events.yml b/db/docs/issue_assignment_events.yml
index 0ba9f9ca21ff1..750e45b3580a4 100644
--- a/db/docs/issue_assignment_events.yml
+++ b/db/docs/issue_assignment_events.yml
@@ -7,4 +7,19 @@ feature_categories:
 description: Tracks the assignment and unassignment events for issues
 introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/117379
 milestone: '15.11'
-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
+desired_sharding_key:
+  namespace_id:
+    references: namespaces
+    backfill_via:
+      parent:
+        foreign_key: issue_id
+        table: issues
+        sharding_key: namespace_id
+        belongs_to: issue
diff --git a/db/docs/issue_metrics.yml b/db/docs/issue_metrics.yml
index d886aede7d0d0..f99f143a1e113 100644
--- a/db/docs/issue_metrics.yml
+++ b/db/docs/issue_metrics.yml
@@ -7,4 +7,19 @@ feature_categories:
 description: Store various metrics for issues.
 introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/commit/516c838a1846d049814765afa85c28a3c14a5b9f
 milestone: '8.12'
-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
+desired_sharding_key:
+  namespace_id:
+    references: namespaces
+    backfill_via:
+      parent:
+        foreign_key: issue_id
+        table: issues
+        sharding_key: namespace_id
+        belongs_to: issue
diff --git a/db/docs/merge_request_assignment_events.yml b/db/docs/merge_request_assignment_events.yml
index 49eeefcbcf0b4..2122a4d64639f 100644
--- a/db/docs/merge_request_assignment_events.yml
+++ b/db/docs/merge_request_assignment_events.yml
@@ -7,4 +7,19 @@ feature_categories:
 description: Tracks the assignment and unassignment events for merge requests
 introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/117379
 milestone: '15.11'
-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
+desired_sharding_key:
+  project_id:
+    references: projects
+    backfill_via:
+      parent:
+        foreign_key: merge_request_id
+        table: merge_requests
+        sharding_key: target_project_id
+        belongs_to: merge_request
-- 
GitLab