From 2b91a2470a2cebec39ffa5c218cfb38b2f66d546 Mon Sep 17 00:00:00 2001
From: Krasimir Angelov <kangelov@gitlab.com>
Date: Wed, 25 Aug 2021 12:54:43 +1200
Subject: [PATCH] Update list of forbidden tables when creating indexes

Add taggings, ci_builds_metadata, and events to the list.

https://gitlab.com/gitlab-org/gitlab/-/issues/339280
---
 ...ract_project_topics_into_separate_table.rb |  2 +-
 .../cop/migration/prevent_index_creation.rb   |  2 +-
 .../migration/prevent_index_creation_spec.rb  | 35 +++++++++++--------
 3 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/db/post_migrate/20210730104800_schedule_extract_project_topics_into_separate_table.rb b/db/post_migrate/20210730104800_schedule_extract_project_topics_into_separate_table.rb
index bb498f89015b3..3102561a12915 100644
--- a/db/post_migrate/20210730104800_schedule_extract_project_topics_into_separate_table.rb
+++ b/db/post_migrate/20210730104800_schedule_extract_project_topics_into_separate_table.rb
@@ -20,7 +20,7 @@ class Tagging < ActiveRecord::Base
 
   def up
     # this index is used in 20210730104800_schedule_extract_project_topics_into_separate_table
-    add_concurrent_index :taggings, :id, where: INDEX_CONDITION, name: INDEX_NAME
+    add_concurrent_index :taggings, :id, where: INDEX_CONDITION, name: INDEX_NAME # rubocop:disable Migration/PreventIndexCreation
 
     queue_background_migration_jobs_by_range_at_intervals(
       Tagging.where(taggable_type: 'Project'),
diff --git a/rubocop/cop/migration/prevent_index_creation.rb b/rubocop/cop/migration/prevent_index_creation.rb
index c90f911d24e55..1486607acbe8c 100644
--- a/rubocop/cop/migration/prevent_index_creation.rb
+++ b/rubocop/cop/migration/prevent_index_creation.rb
@@ -8,7 +8,7 @@ module Migration
       class PreventIndexCreation < RuboCop::Cop::Cop
         include MigrationHelpers
 
-        FORBIDDEN_TABLES = %i[ci_builds].freeze
+        FORBIDDEN_TABLES = %i[ci_builds taggings ci_builds_metadata events].freeze
 
         MSG = "Adding new index to #{FORBIDDEN_TABLES.join(", ")} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886"
 
diff --git a/spec/rubocop/cop/migration/prevent_index_creation_spec.rb b/spec/rubocop/cop/migration/prevent_index_creation_spec.rb
index a3965f54bbd5f..aa1779a8cd563 100644
--- a/spec/rubocop/cop/migration/prevent_index_creation_spec.rb
+++ b/spec/rubocop/cop/migration/prevent_index_creation_spec.rb
@@ -6,28 +6,35 @@
 RSpec.describe RuboCop::Cop::Migration::PreventIndexCreation do
   subject(:cop) { described_class.new }
 
+  let(:forbidden_tables) { %w(ci_builds taggings ci_builds_metadata events) }
+  let(:forbidden_tables_list) { forbidden_tables.join(', ') }
+
   context 'when in migration' do
     before do
       allow(cop).to receive(:in_migration?).and_return(true)
     end
 
     context 'when adding an index to a forbidden table' do
-      it 'registers an offense when add_index is used' do
-        expect_offense(<<~RUBY)
-          def change
-            add_index :ci_builds, :protected
-            ^^^^^^^^^ Adding new index to ci_builds is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
-          end
-        RUBY
+      it "registers an offense when add_index is used", :aggregate_failures do
+        forbidden_tables.each do |table|
+          expect_offense(<<~RUBY)
+            def change
+              add_index :#{table}, :protected
+              ^^^^^^^^^ Adding new index to #{forbidden_tables_list} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
+            end
+          RUBY
+        end
       end
 
-      it 'registers an offense when add_concurrent_index is used' do
-        expect_offense(<<~RUBY)
-          def change
-            add_concurrent_index :ci_builds, :protected
-            ^^^^^^^^^^^^^^^^^^^^ Adding new index to ci_builds is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
-          end
-        RUBY
+      it "registers an offense when add_concurrent_index is used", :aggregate_failures do
+        forbidden_tables.each do |table|
+          expect_offense(<<~RUBY)
+            def change
+              add_concurrent_index :#{table}, :protected
+              ^^^^^^^^^^^^^^^^^^^^ Adding new index to #{forbidden_tables_list} is forbidden, see https://gitlab.com/gitlab-org/gitlab/-/issues/332886
+            end
+          RUBY
+        end
       end
     end
 
-- 
GitLab