From d0a26a639a08076fc273e1bee3a1b3e190e18935 Mon Sep 17 00:00:00 2001
From: Thong Kuah <tkuah@gitlab.com>
Date: Tue, 3 May 2022 15:25:05 +1200
Subject: [PATCH] Switch ApplicationRecord in old background migrations

These places can be switched to use ApplicationRecord as they
exclusively read, and write non-ci tables
---
 .rubocop_todo/database/multiple_databases.yml | 24 -------------------
 .../backfill_integrations_type_new.rb         |  2 +-
 .../backfill_issue_search_data.rb             |  2 +-
 ...fill_member_namespace_for_group_members.rb |  2 +-
 ...ckfill_namespace_id_for_namespace_route.rb |  2 +-
 ...backfill_namespace_id_for_project_route.rb |  8 +++----
 ...ckfill_namespace_traversal_ids_children.rb |  2 +-
 .../backfill_upvotes_count_on_issues.rb       |  2 +-
 .../backfill_user_namespace.rb                |  2 +-
 .../delete_orphaned_deployments.rb            |  2 +-
 ..._policies_linked_to_no_container_images.rb |  2 +-
 .../fix_duplicate_project_name_and_path.rb    |  2 +-
 .../fix_projects_without_project_feature.rb   |  2 +-
 ...fix_projects_without_prometheus_service.rb |  4 ++--
 ...e_shimo_confluence_integration_category.rb |  2 +-
 ...ner_registry_enabled_to_project_feature.rb |  2 +-
 ...ate_container_repository_migration_plan.rb |  2 +-
 ...ulate_topics_non_private_projects_count.rb |  2 +-
 ...ulate_topics_total_projects_count_cache.rb |  2 +-
 .../populate_vulnerability_reads.rb           |  2 +-
 .../backfill_project_namespaces.rb            | 10 ++++----
 .../remove_vulnerability_finding_links.rb     |  2 +-
 .../update_timelogs_null_spent_at.rb          |  2 +-
 .../update_timelogs_project_id.rb             |  2 +-
 ...ere_two_factor_auth_required_from_group.rb |  2 +-
 25 files changed, 32 insertions(+), 56 deletions(-)

diff --git a/.rubocop_todo/database/multiple_databases.yml b/.rubocop_todo/database/multiple_databases.yml
index 060dc93f1170e..40de71b120373 100644
--- a/.rubocop_todo/database/multiple_databases.yml
+++ b/.rubocop_todo/database/multiple_databases.yml
@@ -14,33 +14,9 @@ Database/MultipleDatabases:
     - 'ee/spec/services/ee/merge_requests/update_service_spec.rb'
     - 'lib/backup/database.rb'
     - 'lib/backup/manager.rb'
-    - lib/gitlab/background_migration/backfill_integrations_type_new.rb
-    - lib/gitlab/background_migration/backfill_issue_search_data.rb
-    - lib/gitlab/background_migration/backfill_member_namespace_for_group_members.rb
-    - lib/gitlab/background_migration/backfill_namespace_id_for_namespace_route.rb
-    - lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb
-    - lib/gitlab/background_migration/backfill_namespace_traversal_ids_children.rb
     - lib/gitlab/background_migration/backfill_projects_with_coverage.rb
-    - lib/gitlab/background_migration/backfill_upvotes_count_on_issues.rb
-    - lib/gitlab/background_migration/backfill_user_namespace.rb
     - lib/gitlab/background_migration/copy_ci_builds_columns_to_security_scans.rb
-    - lib/gitlab/background_migration/delete_orphaned_deployments.rb
-    - lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb
-    - lib/gitlab/background_migration/fix_duplicate_project_name_and_path.rb
-    - lib/gitlab/background_migration/fix_projects_without_project_feature.rb
-    - lib/gitlab/background_migration/fix_projects_without_prometheus_service.rb
-    - lib/gitlab/background_migration/migrate_shimo_confluence_integration_category.rb
-    - lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb
     - lib/gitlab/background_migration/nullify_orphan_runner_id_on_ci_builds.rb
-    - lib/gitlab/background_migration/populate_container_repository_migration_plan.rb
-    - lib/gitlab/background_migration/populate_topics_non_private_projects_count.rb
-    - lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb
-    - lib/gitlab/background_migration/populate_vulnerability_reads.rb
-    - lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb
-    - lib/gitlab/background_migration/remove_vulnerability_finding_links.rb
-    - lib/gitlab/background_migration/update_timelogs_null_spent_at.rb
-    - lib/gitlab/background_migration/update_timelogs_project_id.rb
-    - lib/gitlab/background_migration/update_users_where_two_factor_auth_required_from_group.rb
     - 'lib/gitlab/database.rb'
     - 'lib/gitlab/database/load_balancing/load_balancer.rb'
     - 'lib/gitlab/database/migrations/observers/query_log.rb'
diff --git a/lib/gitlab/background_migration/backfill_integrations_type_new.rb b/lib/gitlab/background_migration/backfill_integrations_type_new.rb
index a234cebfce526..6f33472af7da0 100644
--- a/lib/gitlab/background_migration/backfill_integrations_type_new.rb
+++ b/lib/gitlab/background_migration/backfill_integrations_type_new.rb
@@ -22,7 +22,7 @@ def perform(start_id, stop_id, batch_table, batch_column, sub_batch_size, pause_
       private
 
       def connection
-        ActiveRecord::Base.connection
+        ApplicationRecord.connection
       end
 
       def process_sub_batch(sub_batch)
diff --git a/lib/gitlab/background_migration/backfill_issue_search_data.rb b/lib/gitlab/background_migration/backfill_issue_search_data.rb
index ec206cbfd4196..e408fd0cda677 100644
--- a/lib/gitlab/background_migration/backfill_issue_search_data.rb
+++ b/lib/gitlab/background_migration/backfill_issue_search_data.rb
@@ -9,7 +9,7 @@ class BackfillIssueSearchData
       include Gitlab::Database::DynamicModelHelpers
 
       def perform(start_id, stop_id, batch_table, batch_column, sub_batch_size, pause_ms)
-        define_batchable_model(batch_table, connection: ActiveRecord::Base.connection).where(batch_column => start_id..stop_id).each_batch(of: sub_batch_size) do |sub_batch|
+        define_batchable_model(batch_table, connection: ApplicationRecord.connection).where(batch_column => start_id..stop_id).each_batch(of: sub_batch_size) do |sub_batch|
           update_search_data(sub_batch)
 
           sleep(pause_ms * 0.001)
diff --git a/lib/gitlab/background_migration/backfill_member_namespace_for_group_members.rb b/lib/gitlab/background_migration/backfill_member_namespace_for_group_members.rb
index 1ed147d67c7f7..5f3d830c48d51 100644
--- a/lib/gitlab/background_migration/backfill_member_namespace_for_group_members.rb
+++ b/lib/gitlab/background_migration/backfill_member_namespace_for_group_members.rb
@@ -26,7 +26,7 @@ def batch_metrics
       private
 
       def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
-        define_batchable_model(source_table, connection: ActiveRecord::Base.connection)
+        define_batchable_model(source_table, connection: ApplicationRecord.connection)
           .joins('INNER JOIN namespaces ON members.source_id = namespaces.id')
           .where(source_key_column => start_id..stop_id)
           .where(type: 'GroupMember')
diff --git a/lib/gitlab/background_migration/backfill_namespace_id_for_namespace_route.rb b/lib/gitlab/background_migration/backfill_namespace_id_for_namespace_route.rb
index fe3edd3322b2b..0585924cb7bf2 100644
--- a/lib/gitlab/background_migration/backfill_namespace_id_for_namespace_route.rb
+++ b/lib/gitlab/background_migration/backfill_namespace_id_for_namespace_route.rb
@@ -27,7 +27,7 @@ def batch_metrics
       private
 
       def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
-        define_batchable_model(source_table, connection: ActiveRecord::Base.connection)
+        define_batchable_model(source_table, connection: ApplicationRecord.connection)
           .joins('inner join namespaces on routes.source_id = namespaces.id')
           .where(source_key_column => start_id..stop_id)
           .where(namespace_id: nil)
diff --git a/lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb b/lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb
index 1f0d606f001c3..7f75b64e98a6b 100644
--- a/lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb
+++ b/lib/gitlab/background_migration/backfill_namespace_id_for_project_route.rb
@@ -13,7 +13,7 @@ def perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_m
           cleanup_gin_index('routes')
 
           batch_metrics.time_operation(:update_all) do
-            ActiveRecord::Base.connection.execute <<~SQL
+            ApplicationRecord.connection.execute <<~SQL
               WITH route_and_ns(route_id, project_namespace_id) AS #{::Gitlab::Database::AsWithMaterialized.materialized_if_supported} (
                 #{sub_batch.to_sql}
               )
@@ -37,15 +37,15 @@ def batch_metrics
 
       def cleanup_gin_index(table_name)
         sql = "select indexname::text from pg_indexes where tablename = '#{table_name}' and indexdef ilike '%gin%'"
-        index_names = ActiveRecord::Base.connection.select_values(sql)
+        index_names = ApplicationRecord.connection.select_values(sql)
 
         index_names.each do |index_name|
-          ActiveRecord::Base.connection.execute("select gin_clean_pending_list('#{index_name}')")
+          ApplicationRecord.connection.execute("select gin_clean_pending_list('#{index_name}')")
         end
       end
 
       def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
-        define_batchable_model(source_table, connection: ActiveRecord::Base.connection)
+        define_batchable_model(source_table, connection: ApplicationRecord.connection)
           .joins('INNER JOIN projects ON routes.source_id = projects.id')
           .where(source_key_column => start_id..stop_id)
           .where(namespace_id: nil)
diff --git a/lib/gitlab/background_migration/backfill_namespace_traversal_ids_children.rb b/lib/gitlab/background_migration/backfill_namespace_traversal_ids_children.rb
index 79e7a2f2279f0..587de1bcb5a68 100644
--- a/lib/gitlab/background_migration/backfill_namespace_traversal_ids_children.rb
+++ b/lib/gitlab/background_migration/backfill_namespace_traversal_ids_children.rb
@@ -29,7 +29,7 @@ def perform(start_id, end_id, sub_batch_size)
             WHERE namespaces.id = calculated_ids.id
               AND namespaces.traversal_ids = '{}'
           SQL
-          ActiveRecord::Base.connection.execute(update_sql)
+          ApplicationRecord.connection.execute(update_sql)
 
           sleep PAUSE_SECONDS
         end
diff --git a/lib/gitlab/background_migration/backfill_upvotes_count_on_issues.rb b/lib/gitlab/background_migration/backfill_upvotes_count_on_issues.rb
index 170af90805a3e..3bf6bf993ddb9 100644
--- a/lib/gitlab/background_migration/backfill_upvotes_count_on_issues.rb
+++ b/lib/gitlab/background_migration/backfill_upvotes_count_on_issues.rb
@@ -16,7 +16,7 @@ def perform(start_id, stop_id)
       private
 
       def execute(sql)
-        @connection ||= ::ActiveRecord::Base.connection
+        @connection ||= ApplicationRecord.connection
         @connection.execute(sql)
       end
 
diff --git a/lib/gitlab/background_migration/backfill_user_namespace.rb b/lib/gitlab/background_migration/backfill_user_namespace.rb
index ab569e236fbe1..df6b1f083c349 100644
--- a/lib/gitlab/background_migration/backfill_user_namespace.rb
+++ b/lib/gitlab/background_migration/backfill_user_namespace.rb
@@ -25,7 +25,7 @@ def batch_metrics
       private
 
       def connection
-        ActiveRecord::Base.connection
+        ApplicationRecord.connection
       end
 
       def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
diff --git a/lib/gitlab/background_migration/delete_orphaned_deployments.rb b/lib/gitlab/background_migration/delete_orphaned_deployments.rb
index 5d41a46c8cddb..4a3a12ab53d14 100644
--- a/lib/gitlab/background_migration/delete_orphaned_deployments.rb
+++ b/lib/gitlab/background_migration/delete_orphaned_deployments.rb
@@ -15,7 +15,7 @@ def perform(start_id, end_id)
       end
 
       def orphaned_deployments
-        define_batchable_model('deployments', connection: ActiveRecord::Base.connection)
+        define_batchable_model('deployments', connection: ApplicationRecord.connection)
           .where('NOT EXISTS (SELECT 1 FROM environments WHERE deployments.environment_id = environments.id)')
       end
 
diff --git a/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb b/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb
index 9a88eb8ea0674..dad5da875ab21 100644
--- a/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb
+++ b/lib/gitlab/background_migration/disable_expiration_policies_linked_to_no_container_images.rb
@@ -32,7 +32,7 @@ def perform(from_id, to_id)
       private
 
       def execute(sql)
-        ActiveRecord::Base
+        ApplicationRecord
           .connection
           .execute(sql)
       end
diff --git a/lib/gitlab/background_migration/fix_duplicate_project_name_and_path.rb b/lib/gitlab/background_migration/fix_duplicate_project_name_and_path.rb
index defd9ea832bc0..3772430d0b7f3 100644
--- a/lib/gitlab/background_migration/fix_duplicate_project_name_and_path.rb
+++ b/lib/gitlab/background_migration/fix_duplicate_project_name_and_path.rb
@@ -21,7 +21,7 @@ def perform(start_id, end_id)
         backfill_project_namespaces_service.cleanup_gin_index('projects')
 
         project_ids.each_slice(SUB_BATCH_SIZE) do |ids|
-          ActiveRecord::Base.connection.execute(update_projects_name_and_path_sql(ids))
+          ApplicationRecord.connection.execute(update_projects_name_and_path_sql(ids))
         end
 
         backfill_project_namespaces_service.backfill_project_namespaces
diff --git a/lib/gitlab/background_migration/fix_projects_without_project_feature.rb b/lib/gitlab/background_migration/fix_projects_without_project_feature.rb
index 83c01afa432b4..c21f9c1d50fe8 100644
--- a/lib/gitlab/background_migration/fix_projects_without_project_feature.rb
+++ b/lib/gitlab/background_migration/fix_projects_without_project_feature.rb
@@ -14,7 +14,7 @@ def perform(from_id, to_id)
       private
 
       def create_missing!(from_id, to_id)
-        result = ActiveRecord::Base.connection.select_one(sql(from_id, to_id))
+        result = ApplicationRecord.connection.select_one(sql(from_id, to_id))
         return 0 unless result
 
         result['number_of_created_records']
diff --git a/lib/gitlab/background_migration/fix_projects_without_prometheus_service.rb b/lib/gitlab/background_migration/fix_projects_without_prometheus_service.rb
index b8e4562b3bfe0..496ec0bd0a169 100644
--- a/lib/gitlab/background_migration/fix_projects_without_prometheus_service.rb
+++ b/lib/gitlab/background_migration/fix_projects_without_prometheus_service.rb
@@ -120,14 +120,14 @@ def process_batch(from_id, to_id)
       end
 
       def create_missing(from_id, to_id)
-        result = ActiveRecord::Base.connection.select_one(create_sql(from_id, to_id))
+        result = ApplicationRecord.connection.select_one(create_sql(from_id, to_id))
         return unless result
 
         logger.info(message: "#{self.class}: created missing services for #{result['number_of_created_records']} projects in id=#{from_id}...#{to_id}")
       end
 
       def update_inconsistent(from_id, to_id)
-        result = ActiveRecord::Base.connection.select_one(update_sql(from_id, to_id))
+        result = ApplicationRecord.connection.select_one(update_sql(from_id, to_id))
         return unless result
 
         logger.info(message: "#{self.class}: updated inconsistent services for #{result['number_of_updated_records']} projects in id=#{from_id}...#{to_id}")
diff --git a/lib/gitlab/background_migration/migrate_shimo_confluence_integration_category.rb b/lib/gitlab/background_migration/migrate_shimo_confluence_integration_category.rb
index ec4631d1e34c4..d7d24960a41e5 100644
--- a/lib/gitlab/background_migration/migrate_shimo_confluence_integration_category.rb
+++ b/lib/gitlab/background_migration/migrate_shimo_confluence_integration_category.rb
@@ -7,7 +7,7 @@ class MigrateShimoConfluenceIntegrationCategory
       include Gitlab::Database::DynamicModelHelpers
 
       def perform(start_id, end_id)
-        define_batchable_model('integrations', connection: ::ActiveRecord::Base.connection)
+        define_batchable_model('integrations', connection: ApplicationRecord.connection)
           .where(id: start_id..end_id, type_new: %w[Integrations::Confluence Integrations::Shimo])
           .update_all(category: 'third_party_wiki')
 
diff --git a/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb b/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb
index c01545e5dcab3..06422ed282f63 100644
--- a/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb
+++ b/lib/gitlab/background_migration/move_container_registry_enabled_to_project_feature.rb
@@ -22,7 +22,7 @@ def perform(from_id, to_id)
       private
 
       def process_batch(from_id, to_id)
-        ActiveRecord::Base.connection.execute(update_sql(from_id, to_id))
+        ApplicationRecord.connection.execute(update_sql(from_id, to_id))
 
         logger.info(message: "#{self.class}: Copied container_registry_enabled values for projects with IDs between #{from_id}..#{to_id}")
       end
diff --git a/lib/gitlab/background_migration/populate_container_repository_migration_plan.rb b/lib/gitlab/background_migration/populate_container_repository_migration_plan.rb
index 9e102ea151701..a9611e9814c4e 100644
--- a/lib/gitlab/background_migration/populate_container_repository_migration_plan.rb
+++ b/lib/gitlab/background_migration/populate_container_repository_migration_plan.rb
@@ -33,7 +33,7 @@ def perform(start_id, end_id)
       private
 
       def connection
-        @connection ||= ::ActiveRecord::Base.connection
+        @connection ||= ApplicationRecord.connection
       end
 
       def execute(sql)
diff --git a/lib/gitlab/background_migration/populate_topics_non_private_projects_count.rb b/lib/gitlab/background_migration/populate_topics_non_private_projects_count.rb
index 769ca4be7f350..1f2b55004e450 100644
--- a/lib/gitlab/background_migration/populate_topics_non_private_projects_count.rb
+++ b/lib/gitlab/background_migration/populate_topics_non_private_projects_count.rb
@@ -15,7 +15,7 @@ class Topic < ActiveRecord::Base
 
       def perform(start_id, stop_id)
         Topic.where(id: start_id..stop_id).each_batch(of: SUB_BATCH_SIZE) do |batch|
-          ActiveRecord::Base.connection.execute(<<~SQL)
+          ApplicationRecord.connection.execute(<<~SQL)
             WITH batched_relation AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} (#{batch.select(:id).limit(SUB_BATCH_SIZE).to_sql})
             UPDATE topics
             SET non_private_projects_count = (
diff --git a/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb b/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb
index 1d96872d44540..2495cb51364d5 100644
--- a/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb
+++ b/lib/gitlab/background_migration/populate_topics_total_projects_count_cache.rb
@@ -15,7 +15,7 @@ class Topic < ActiveRecord::Base
 
       def perform(start_id, stop_id)
         Topic.where(id: start_id..stop_id).each_batch(of: SUB_BATCH_SIZE) do |batch|
-          ActiveRecord::Base.connection.execute(<<~SQL)
+          ApplicationRecord.connection.execute(<<~SQL)
             WITH batched_relation AS #{Gitlab::Database::AsWithMaterialized.materialized_if_supported} (#{batch.select(:id).limit(SUB_BATCH_SIZE).to_sql})
             UPDATE topics
             SET total_projects_count = (SELECT COUNT(*) FROM project_topics WHERE topic_id = batched_relation.id)
diff --git a/lib/gitlab/background_migration/populate_vulnerability_reads.rb b/lib/gitlab/background_migration/populate_vulnerability_reads.rb
index 7b6d4c1ff819f..5e6475a3d1a53 100644
--- a/lib/gitlab/background_migration/populate_vulnerability_reads.rb
+++ b/lib/gitlab/background_migration/populate_vulnerability_reads.rb
@@ -26,7 +26,7 @@ def vulnerability_model
       end
 
       def connection
-        ActiveRecord::Base.connection
+        ApplicationRecord.connection
       end
 
       def insert_query(start_id, end_id)
diff --git a/lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb b/lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb
index bd7d7d0216212..91a0db08d5ba7 100644
--- a/lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb
+++ b/lib/gitlab/background_migration/project_namespaces/backfill_project_namespaces.rb
@@ -55,10 +55,10 @@ def backfill_project_namespaces
         end
 
         def cleanup_gin_index(table_name)
-          index_names = ActiveRecord::Base.connection.select_values("select indexname::text from pg_indexes where tablename = '#{table_name}' and indexdef ilike '%gin%'")
+          index_names = ApplicationRecord.connection.select_values("select indexname::text from pg_indexes where tablename = '#{table_name}' and indexdef ilike '%gin%'")
 
           index_names.each do |index_name|
-            ActiveRecord::Base.connection.execute("select gin_clean_pending_list('#{index_name}')")
+            ApplicationRecord.connection.execute("select gin_clean_pending_list('#{index_name}')")
           end
         end
 
@@ -77,7 +77,7 @@ def batch_insert_namespaces(project_ids)
           projects = IsolatedModels::Project.where(id: project_ids)
             .select("projects.id, projects.name, projects.path, projects.namespace_id, projects.visibility_level, shared_runners_enabled, '#{PROJECT_NAMESPACE_STI_NAME}', now(), now()")
 
-          ActiveRecord::Base.connection.execute <<~SQL
+          ApplicationRecord.connection.execute <<~SQL
             INSERT INTO namespaces (tmp_project_id, name, path, parent_id, visibility_level, shared_runners_enabled, type, created_at, updated_at)
             #{projects.to_sql}
             ON CONFLICT DO NOTHING;
@@ -89,7 +89,7 @@ def batch_update_projects(project_ids)
                        .joins("INNER JOIN namespaces ON projects.id = namespaces.tmp_project_id")
                        .select("namespaces.id, namespaces.tmp_project_id")
 
-          ActiveRecord::Base.connection.execute <<~SQL
+          ApplicationRecord.connection.execute <<~SQL
             WITH cte(project_namespace_id, project_id) AS #{::Gitlab::Database::AsWithMaterialized.materialized_if_supported} (
               #{projects.to_sql}
             )
@@ -105,7 +105,7 @@ def batch_update_project_namespaces_traversal_ids(project_ids)
                          .joins("INNER JOIN namespaces n2 ON namespaces.parent_id = n2.id")
                          .select("namespaces.id as project_namespace_id, n2.traversal_ids")
 
-          ActiveRecord::Base.connection.execute <<~SQL
+          ApplicationRecord.connection.execute <<~SQL
             UPDATE namespaces
             SET traversal_ids = array_append(project_namespaces.traversal_ids, project_namespaces.project_namespace_id)
             FROM (#{namespaces.to_sql}) as project_namespaces(project_namespace_id, traversal_ids)
diff --git a/lib/gitlab/background_migration/remove_vulnerability_finding_links.rb b/lib/gitlab/background_migration/remove_vulnerability_finding_links.rb
index 323f109449b5b..4acef9029f9ac 100644
--- a/lib/gitlab/background_migration/remove_vulnerability_finding_links.rb
+++ b/lib/gitlab/background_migration/remove_vulnerability_finding_links.rb
@@ -10,7 +10,7 @@ class RemoveVulnerabilityFindingLinks
       include Gitlab::Database::DynamicModelHelpers
 
       def perform(start_id, stop_id)
-        define_batchable_model('vulnerability_finding_links', connection: ActiveRecord::Base.connection)
+        define_batchable_model('vulnerability_finding_links', connection: ApplicationRecord.connection)
           .where(id: start_id..stop_id)
           .delete_all
       end
diff --git a/lib/gitlab/background_migration/update_timelogs_null_spent_at.rb b/lib/gitlab/background_migration/update_timelogs_null_spent_at.rb
index f54bb8256d0a5..38932e52bb0c4 100644
--- a/lib/gitlab/background_migration/update_timelogs_null_spent_at.rb
+++ b/lib/gitlab/background_migration/update_timelogs_null_spent_at.rb
@@ -28,7 +28,7 @@ def update_timelogs(batch_start, batch_stop)
       end
 
       def connection
-        @connection ||= ::ActiveRecord::Base.connection
+        @connection ||= ApplicationRecord.connection
       end
 
       def execute(sql)
diff --git a/lib/gitlab/background_migration/update_timelogs_project_id.rb b/lib/gitlab/background_migration/update_timelogs_project_id.rb
index 24c9967b88e05..69bb5cf6e6d1b 100644
--- a/lib/gitlab/background_migration/update_timelogs_project_id.rb
+++ b/lib/gitlab/background_migration/update_timelogs_project_id.rb
@@ -36,7 +36,7 @@ def update_merge_request_timelogs(batch_start, batch_stop)
       end
 
       def execute(sql)
-        @connection ||= ::ActiveRecord::Base.connection
+        @connection ||= ApplicationRecord.connection
         @connection.execute(sql)
       end
     end
diff --git a/lib/gitlab/background_migration/update_users_where_two_factor_auth_required_from_group.rb b/lib/gitlab/background_migration/update_users_where_two_factor_auth_required_from_group.rb
index f5ba9e6333376..10db9f5064a4b 100644
--- a/lib/gitlab/background_migration/update_users_where_two_factor_auth_required_from_group.rb
+++ b/lib/gitlab/background_migration/update_users_where_two_factor_auth_required_from_group.rb
@@ -5,7 +5,7 @@ module Gitlab
   module BackgroundMigration
     class UpdateUsersWhereTwoFactorAuthRequiredFromGroup # rubocop:disable Metrics/ClassLength
       def perform(start_id, stop_id)
-        ActiveRecord::Base.connection.execute <<~SQL
+        ApplicationRecord.connection.execute <<~SQL
           UPDATE
             users
           SET
-- 
GitLab