From 2231abbc1f92d8f6d4342ddf651e543f532dcc65 Mon Sep 17 00:00:00 2001 From: "Sincheol (David) Kim" <dkim@gitlab.com> Date: Fri, 16 Sep 2022 03:57:54 +0000 Subject: [PATCH] Cleanup attention request related system notes Changelog: changed --- ...e_metadata_on_attention_request_actions.rb | 17 +++++++++++ ..._attention_request_related_system_notes.rb | 30 +++++++++++++++++++ db/schema_migrations/20220913030552 | 1 + db/schema_migrations/20220913030624 | 1 + db/structure.sql | 2 ++ ...ntion_request_related_system_notes_spec.rb | 26 ++++++++++++++++ 6 files changed, 77 insertions(+) create mode 100644 db/post_migrate/20220913030552_add_tmp_index_system_note_metadata_on_attention_request_actions.rb create mode 100644 db/post_migrate/20220913030624_cleanup_attention_request_related_system_notes.rb create mode 100644 db/schema_migrations/20220913030552 create mode 100644 db/schema_migrations/20220913030624 create mode 100644 spec/migrations/20220913030624_cleanup_attention_request_related_system_notes_spec.rb diff --git a/db/post_migrate/20220913030552_add_tmp_index_system_note_metadata_on_attention_request_actions.rb b/db/post_migrate/20220913030552_add_tmp_index_system_note_metadata_on_attention_request_actions.rb new file mode 100644 index 000000000000..3418dabc0e92 --- /dev/null +++ b/db/post_migrate/20220913030552_add_tmp_index_system_note_metadata_on_attention_request_actions.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class AddTmpIndexSystemNoteMetadataOnAttentionRequestActions < Gitlab::Database::Migration[2.0] + INDEX_NAME = "tmp_index_system_note_metadata_on_attention_request_actions" + + disable_ddl_transaction! + + def up + add_concurrent_index :system_note_metadata, [:id], + where: "action IN ('attention_requested', 'attention_request_removed')", + name: INDEX_NAME + end + + def down + remove_concurrent_index_by_name :system_note_metadata, INDEX_NAME + end +end diff --git a/db/post_migrate/20220913030624_cleanup_attention_request_related_system_notes.rb b/db/post_migrate/20220913030624_cleanup_attention_request_related_system_notes.rb new file mode 100644 index 000000000000..b7d6908696b1 --- /dev/null +++ b/db/post_migrate/20220913030624_cleanup_attention_request_related_system_notes.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class CleanupAttentionRequestRelatedSystemNotes < Gitlab::Database::Migration[2.0] + disable_ddl_transaction! + restrict_gitlab_migration gitlab_schema: :gitlab_main + + BATCH_SIZE = 100 + + class SystemNoteMetadata < MigrationRecord + include EachBatch + + self.table_name = 'system_note_metadata' + end + + class Note < MigrationRecord + self.table_name = 'notes' + end + + def up + SystemNoteMetadata + .where(action: %w[attention_requested attention_request_removed]) + .each_batch(of: BATCH_SIZE) do |batch| + Note.where(id: batch.pluck(:note_id)).delete_all + end + end + + def down + # no op + end +end diff --git a/db/schema_migrations/20220913030552 b/db/schema_migrations/20220913030552 new file mode 100644 index 000000000000..6d6a68788dc2 --- /dev/null +++ b/db/schema_migrations/20220913030552 @@ -0,0 +1 @@ +39538feebc6f7f4e1822148567ed369eee1a7ed7ee718f7e913e2b585cc0e808 \ No newline at end of file diff --git a/db/schema_migrations/20220913030624 b/db/schema_migrations/20220913030624 new file mode 100644 index 000000000000..8a0641b4f8c9 --- /dev/null +++ b/db/schema_migrations/20220913030624 @@ -0,0 +1 @@ +baac0b236b7e91f9aacd03f3cf1ce84974f6c389529143e9b2813d9b70224e53 \ No newline at end of file diff --git a/db/structure.sql b/db/structure.sql index 3274c47b9c90..6f43f9156a54 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -30861,6 +30861,8 @@ CREATE INDEX tmp_index_on_vulnerabilities_non_dismissed ON vulnerabilities USING CREATE INDEX tmp_index_project_statistics_cont_registry_size ON project_statistics USING btree (project_id) WHERE (container_registry_size = 0); +CREATE INDEX tmp_index_system_note_metadata_on_attention_request_actions ON system_note_metadata USING btree (id) WHERE ((action)::text = ANY ((ARRAY['attention_requested'::character varying, 'attention_request_removed'::character varying])::text[])); + CREATE INDEX tmp_index_system_note_metadata_on_id_where_task ON system_note_metadata USING btree (id, action) WHERE ((action)::text = 'task'::text); CREATE INDEX tmp_index_user_callouts_on_attention_request_feature_names ON user_callouts USING btree (id) WHERE (feature_name = ANY (ARRAY[47, 48])); diff --git a/spec/migrations/20220913030624_cleanup_attention_request_related_system_notes_spec.rb b/spec/migrations/20220913030624_cleanup_attention_request_related_system_notes_spec.rb new file mode 100644 index 000000000000..7338a6ab9aed --- /dev/null +++ b/spec/migrations/20220913030624_cleanup_attention_request_related_system_notes_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_migration! + +RSpec.describe CleanupAttentionRequestRelatedSystemNotes, :migration do + let(:notes) { table(:notes) } + let(:system_note_metadata) { table(:system_note_metadata) } + + it 'removes all notes with attention request related system_note_metadata' do + notes.create!(id: 1, note: 'Attention request note', noteable_type: 'MergeRequest') + notes.create!(id: 2, note: 'Attention request remove note', noteable_type: 'MergeRequest') + notes.create!(id: 3, note: 'MergeRequest note', noteable_type: 'MergeRequest') + notes.create!(id: 4, note: 'Commit note', noteable_type: 'Commit') + system_note_metadata.create!(id: 11, action: 'attention_requested', note_id: 1) + system_note_metadata.create!(id: 22, action: 'attention_request_removed', note_id: 2) + system_note_metadata.create!(id: 33, action: 'merged', note_id: 3) + + expect { migrate! }.to change(notes, :count).by(-2) + + expect(system_note_metadata.where(action: %w[attention_requested attention_request_removed]).size).to eq(0) + expect(notes.where(noteable_type: 'MergeRequest').size).to eq(1) + expect(notes.where(noteable_type: 'Commit').size).to eq(1) + expect(system_note_metadata.where(action: 'merged').size).to eq(1) + end +end -- GitLab