From c853e60ec17a81743da08b00b967ece41101cdc8 Mon Sep 17 00:00:00 2001
From: Mario Celi <mcelicalderon@gitlab.com>
Date: Tue, 14 May 2024 16:54:03 -0500
Subject: [PATCH] Finalize BackupAndRemoveNotesWithNullNoteableType migration

Background migration finalized and we schedule
the NOT NULL constraint of the notes.noteable_type
column to be run over the weekend

Changelog: other
---
 ...d_remove_notes_with_null_noteable_type.yml |  2 +-
 ...nd_remove_notes_with_null_noteable_type.rb | 22 +++++++++++++++++++
 ...notes_noteable_type_not_null_constraint.rb | 15 +++++++++++++
 db/schema_migrations/20240514210445           |  1 +
 db/schema_migrations/20240514214517           |  1 +
 5 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 db/post_migrate/20240514210445_finalize_backup_and_remove_notes_with_null_noteable_type.rb
 create mode 100644 db/post_migrate/20240514214517_async_validate_notes_noteable_type_not_null_constraint.rb
 create mode 100644 db/schema_migrations/20240514210445
 create mode 100644 db/schema_migrations/20240514214517

diff --git a/db/docs/batched_background_migrations/backup_and_remove_notes_with_null_noteable_type.yml b/db/docs/batched_background_migrations/backup_and_remove_notes_with_null_noteable_type.yml
index b842b1fdaab51..1ce1b2b4fef12 100644
--- a/db/docs/batched_background_migrations/backup_and_remove_notes_with_null_noteable_type.yml
+++ b/db/docs/batched_background_migrations/backup_and_remove_notes_with_null_noteable_type.yml
@@ -6,4 +6,4 @@ introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/146023
 milestone: '16.10'
 queued_migration_version: 20240226143323
 finalize_after: '2024-03-15'
-finalized_by: # version of the migration that finalized this BBM
+finalized_by: 20240514210445
diff --git a/db/post_migrate/20240514210445_finalize_backup_and_remove_notes_with_null_noteable_type.rb b/db/post_migrate/20240514210445_finalize_backup_and_remove_notes_with_null_noteable_type.rb
new file mode 100644
index 0000000000000..7ad46e1208d28
--- /dev/null
+++ b/db/post_migrate/20240514210445_finalize_backup_and_remove_notes_with_null_noteable_type.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class FinalizeBackupAndRemoveNotesWithNullNoteableType < Gitlab::Database::Migration[2.2]
+  MIGRATION = 'BackupAndRemoveNotesWithNullNoteableType'
+
+  disable_ddl_transaction!
+  restrict_gitlab_migration gitlab_schema: :gitlab_main_cell
+  milestone '17.1'
+
+  def up
+    ensure_batched_background_migration_is_finished(
+      job_class_name: MIGRATION,
+      table_name: :notes,
+      column_name: :id,
+      job_arguments: []
+    )
+  end
+
+  def down
+    # no-op
+  end
+end
diff --git a/db/post_migrate/20240514214517_async_validate_notes_noteable_type_not_null_constraint.rb b/db/post_migrate/20240514214517_async_validate_notes_noteable_type_not_null_constraint.rb
new file mode 100644
index 0000000000000..779e80c6d2a87
--- /dev/null
+++ b/db/post_migrate/20240514214517_async_validate_notes_noteable_type_not_null_constraint.rb
@@ -0,0 +1,15 @@
+# frozen_string_literal: true
+
+class AsyncValidateNotesNoteableTypeNotNullConstraint < Gitlab::Database::Migration[2.2]
+  milestone '17.1'
+
+  CONSTRAINT_NAME = 'check_1244cbd7d0'
+
+  def up
+    prepare_async_check_constraint_validation :notes, name: CONSTRAINT_NAME
+  end
+
+  def down
+    unprepare_async_check_constraint_validation :notes, name: CONSTRAINT_NAME
+  end
+end
diff --git a/db/schema_migrations/20240514210445 b/db/schema_migrations/20240514210445
new file mode 100644
index 0000000000000..f76ef5499f6c1
--- /dev/null
+++ b/db/schema_migrations/20240514210445
@@ -0,0 +1 @@
+89358eb2216e9ab4a763faf903028d840017349bbd1b0a9e2dbe64b31ab7df0f
\ No newline at end of file
diff --git a/db/schema_migrations/20240514214517 b/db/schema_migrations/20240514214517
new file mode 100644
index 0000000000000..48fe39e43e262
--- /dev/null
+++ b/db/schema_migrations/20240514214517
@@ -0,0 +1 @@
+92736183b5831da66a0b5689ad026520e147c2be21dd2abc82fb1f4c8e7b12dd
\ No newline at end of file
-- 
GitLab