From b346455a37aa1dfa90e6b25be81a7c900e9e6844 Mon Sep 17 00:00:00 2001
From: Rutger Wessels <rwessels@gitlab.com>
Date: Fri, 22 Dec 2023 15:43:25 +0100
Subject: [PATCH] Replace BeforeAll adapter by TestProf supported adapter

This code was merged into TestProf gem so we can now rely
on the gem
---
 ...e_end_string_concatenation_indentation.yml |  1 -
 .../style/inline_disable_annotation.yml       |  1 -
 .rubocop_todo/style/redundant_self.yml        |  1 -
 spec/support/before_all_adapter.rb            | 33 -------------------
 spec/support/db_cleaner.rb                    |  2 +-
 .../database/multiple_databases_helpers.rb    |  2 +-
 6 files changed, 2 insertions(+), 38 deletions(-)
 delete mode 100644 spec/support/before_all_adapter.rb

diff --git a/.rubocop_todo/layout/line_end_string_concatenation_indentation.yml b/.rubocop_todo/layout/line_end_string_concatenation_indentation.yml
index 7bc8d09558528..8c05d745fbb7a 100644
--- a/.rubocop_todo/layout/line_end_string_concatenation_indentation.yml
+++ b/.rubocop_todo/layout/line_end_string_concatenation_indentation.yml
@@ -293,7 +293,6 @@ Layout/LineEndStringConcatenationIndentation:
     - 'spec/services/snippets/create_service_spec.rb'
     - 'spec/services/users/email_verification/validate_token_service_spec.rb'
     - 'spec/services/work_items/parent_links/create_service_spec.rb'
-    - 'spec/support/before_all_adapter.rb'
     - 'spec/support/helpers/database/multiple_databases_helpers.rb'
     - 'spec/support/helpers/redis_without_keys.rb'
     - 'spec/support/matchers/have_gitlab_http_status.rb'
diff --git a/.rubocop_todo/style/inline_disable_annotation.yml b/.rubocop_todo/style/inline_disable_annotation.yml
index db42f855d1a34..9dbb54954f844 100644
--- a/.rubocop_todo/style/inline_disable_annotation.yml
+++ b/.rubocop_todo/style/inline_disable_annotation.yml
@@ -3153,7 +3153,6 @@ Style/InlineDisableAnnotation:
     - 'spec/sidekiq/cron/job_gem_dependency_spec.rb'
     - 'spec/sidekiq_cluster/sidekiq_cluster_spec.rb'
     - 'spec/spec_helper.rb'
-    - 'spec/support/before_all_adapter.rb'
     - 'spec/support/capybara.rb'
     - 'spec/support/database/click_house/hooks.rb'
     - 'spec/support/db_cleaner.rb'
diff --git a/.rubocop_todo/style/redundant_self.yml b/.rubocop_todo/style/redundant_self.yml
index 701ce4db7df7a..8c23290babeb8 100644
--- a/.rubocop_todo/style/redundant_self.yml
+++ b/.rubocop_todo/style/redundant_self.yml
@@ -384,4 +384,3 @@ Style/RedundantSelf:
     - 'spec/lib/gitlab/background_task_spec.rb'
     - 'spec/lib/gitlab/database/load_balancing_spec.rb'
     - 'spec/models/integration_spec.rb'
-    - 'spec/support/before_all_adapter.rb'
diff --git a/spec/support/before_all_adapter.rb b/spec/support/before_all_adapter.rb
deleted file mode 100644
index 35846fcecb8b4..0000000000000
--- a/spec/support/before_all_adapter.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-# frozen_string_literal: true
-
-module TestProfBeforeAllAdapter
-  module MultipleDatabaseAdapter
-    def self.all_connection_classes
-      @all_connection_classes ||= [ActiveRecord::Base] + ActiveRecord::Base.descendants.select(&:connection_class?) # rubocop: disable Database/MultipleDatabases
-    end
-
-    def self.begin_transaction
-      self.all_connection_classes.each do |connection_class|
-        connection_class.connection.begin_transaction(joinable: false)
-      end
-    end
-
-    def self.rollback_transaction
-      self.all_connection_classes.each do |connection_class|
-        if connection_class.connection.open_transactions.zero?
-          warn "!!! before_all transaction has been already rollbacked and " \
-                "could work incorrectly"
-          next
-        end
-
-        connection_class.connection.rollback_transaction
-      end
-    end
-  end
-
-  def self.default_adapter
-    MultipleDatabaseAdapter
-  end
-end
-
-TestProf::BeforeAll.adapter = ::TestProfBeforeAllAdapter.default_adapter
diff --git a/spec/support/db_cleaner.rb b/spec/support/db_cleaner.rb
index 0a1d68a744cc4..b6539d6215b6a 100644
--- a/spec/support/db_cleaner.rb
+++ b/spec/support/db_cleaner.rb
@@ -2,7 +2,7 @@
 
 module DbCleaner
   def all_connection_classes
-    ::TestProfBeforeAllAdapter::MultipleDatabaseAdapter.all_connection_classes
+    ::TestProf::BeforeAll::Adapters::ActiveRecord.all_connections.map(&:connection_class).uniq
   end
 
   def delete_from_all_tables!(except: [])
diff --git a/spec/support/helpers/database/multiple_databases_helpers.rb b/spec/support/helpers/database/multiple_databases_helpers.rb
index bccd6979af140..9df0685e46adf 100644
--- a/spec/support/helpers/database/multiple_databases_helpers.rb
+++ b/spec/support/helpers/database/multiple_databases_helpers.rb
@@ -85,7 +85,7 @@ def ensure_schema_and_empty_tables
     # The usage of this method switches temporarily used `connection_handler`
     # allowing full manipulation of ActiveRecord::Base connections without
     # having side effects like:
-    # - misaligned transactions since this is managed by `BeforeAllAdapter`
+    # - misaligned transactions since this is managed by `TestProf::BeforeAll::Adapters::ActiveRecord`
     # - removal of primary connections
     #
     # The execution within a block ensures safe cleanup of all allocated resources.
-- 
GitLab