From 5502fd4a450e526958ed9fa572dbe42983a58204 Mon Sep 17 00:00:00 2001
From: Heinrich Lee Yu <heinrich@gitlab.com>
Date: Tue, 18 Jul 2023 17:59:15 +0800
Subject: [PATCH] Add namespace_id to notes

This is going to be used for partitioning the table. Also prepares the
async index on the column so we can add the foreign key constraint to
this column.

Changelog: other
---
 .../20230718094246_add_namespace_id_to_notes.rb    | 13 +++++++++++++
 ...30718094501_prepare_notes_namespace_id_index.rb | 14 ++++++++++++++
 db/schema_migrations/20230718094246                |  1 +
 db/schema_migrations/20230718094501                |  1 +
 db/structure.sql                                   |  3 ++-
 lib/gitlab/import_export/project/import_export.yml |  2 ++
 spec/db/schema_spec.rb                             |  5 +++--
 7 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100644 db/migrate/20230718094246_add_namespace_id_to_notes.rb
 create mode 100644 db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb
 create mode 100644 db/schema_migrations/20230718094246
 create mode 100644 db/schema_migrations/20230718094501

diff --git a/db/migrate/20230718094246_add_namespace_id_to_notes.rb b/db/migrate/20230718094246_add_namespace_id_to_notes.rb
new file mode 100644
index 0000000000000..f2f3ef401a4f4
--- /dev/null
+++ b/db/migrate/20230718094246_add_namespace_id_to_notes.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class AddNamespaceIdToNotes < Gitlab::Database::Migration[2.1]
+  enable_lock_retries!
+
+  def up
+    add_column :notes, :namespace_id, :bigint
+  end
+
+  def down
+    remove_column :notes, :namespace_id
+  end
+end
diff --git a/db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb b/db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb
new file mode 100644
index 0000000000000..41f57814c4122
--- /dev/null
+++ b/db/post_migrate/20230718094501_prepare_notes_namespace_id_index.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class PrepareNotesNamespaceIdIndex < Gitlab::Database::Migration[2.1]
+  INDEX_NAME = 'index_notes_on_namespace_id'
+
+  # TODO: Index to be created synchronously as part of https://gitlab.com/gitlab-org/gitlab/-/issues/416127
+  def up
+    prepare_async_index :notes, :namespace_id, name: INDEX_NAME
+  end
+
+  def down
+    unprepare_async_index :notes, :namespace_id, name: INDEX_NAME
+  end
+end
diff --git a/db/schema_migrations/20230718094246 b/db/schema_migrations/20230718094246
new file mode 100644
index 0000000000000..4d42af51f7d86
--- /dev/null
+++ b/db/schema_migrations/20230718094246
@@ -0,0 +1 @@
+8cb2cd90109dcc4f29ab34fa4caca617fe7bf42493e3a6712b66978480f06940
\ No newline at end of file
diff --git a/db/schema_migrations/20230718094501 b/db/schema_migrations/20230718094501
new file mode 100644
index 0000000000000..3db259aa8ba60
--- /dev/null
+++ b/db/schema_migrations/20230718094501
@@ -0,0 +1 @@
+636bd7305c03f94cd161dc21deda05c289946b31a3c04805c08273f9a7067729
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index fc0b145ad3823..c783814942255 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -19080,7 +19080,8 @@ CREATE TABLE notes (
     confidential boolean,
     last_edited_at timestamp with time zone,
     internal boolean DEFAULT false NOT NULL,
-    id bigint NOT NULL
+    id bigint NOT NULL,
+    namespace_id bigint
 );
 
 CREATE SEQUENCE notes_id_seq
diff --git a/lib/gitlab/import_export/project/import_export.yml b/lib/gitlab/import_export/project/import_export.yml
index 5986c5de4415b..850c89c1fb1e3 100644
--- a/lib/gitlab/import_export/project/import_export.yml
+++ b/lib/gitlab/import_export/project/import_export.yml
@@ -984,9 +984,11 @@ excluded_attributes:
   notes:
     - :noteable_id
     - :review_id
+    - :namespace_id
   commit_notes:
     - :noteable_id
     - :review_id
+    - :namespace_id
   label_links:
     - :label_id
     - :target_id
diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb
index c22292cb82c6c..bb50c7cf69d18 100644
--- a/spec/db/schema_spec.rb
+++ b/spec/db/schema_spec.rb
@@ -14,7 +14,8 @@
     # but in Search::NamespaceIndexAssignment model, only `search_index_id` is used as foreign key and indexed
     search_namespace_index_assignments: [%w[search_index_id index_type]],
     slack_integrations_scopes: [%w[slack_api_scope_id]],
-    namespaces: %w[organization_id] # this index is added in an async manner, hence it needs to be ignored in the first phase.
+    namespaces: %w[organization_id], # this index is added in an async manner, hence it needs to be ignored in the first phase.
+    notes: %w[namespace_id] # this index is added in an async manner, hence it needs to be ignored in the first phase.
   }.with_indifferent_access.freeze
 
   TABLE_PARTITIONS = %w[ci_builds_metadata].freeze
@@ -82,7 +83,7 @@
     merge_requests_compliance_violations: %w[target_project_id],
     merge_request_diff_commits: %w[commit_author_id committer_id],
     namespaces: %w[owner_id parent_id],
-    notes: %w[author_id commit_id noteable_id updated_by_id resolved_by_id confirmed_by_id discussion_id],
+    notes: %w[author_id commit_id noteable_id updated_by_id resolved_by_id confirmed_by_id discussion_id namespace_id],
     notification_settings: %w[source_id],
     oauth_access_grants: %w[resource_owner_id application_id],
     oauth_access_tokens: %w[resource_owner_id application_id],
-- 
GitLab