From 2138863be5c5dd6d27830ced0de03053e5cb455f Mon Sep 17 00:00:00 2001
From: Simon Tomlinson <stomlinson@gitlab.com>
Date: Thu, 3 Mar 2022 15:16:08 -0600
Subject: [PATCH] Fix ActiveRecord::Base accesses in geo files

Geo does not yet support syncing both primary and ci databases, so all
accesses here refer to the primary database.
---
 .rubocop_todo/database/multiple_databases.yml |  4 ----
 ee/lib/gitlab/geo/geo_tasks.rb                |  2 +-
 ee/lib/gitlab/geo/health_check.rb             | 11 ++++++-----
 ee/lib/gitlab/geo/log_cursor/daemon.rb        |  2 +-
 4 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/.rubocop_todo/database/multiple_databases.yml b/.rubocop_todo/database/multiple_databases.yml
index 8364ee2af7fa8..27e28128a9869 100644
--- a/.rubocop_todo/database/multiple_databases.yml
+++ b/.rubocop_todo/database/multiple_databases.yml
@@ -1,10 +1,6 @@
 ---
 Database/MultipleDatabases:
   Exclude:
-  - ee/lib/gitlab/geo/database_tasks.rb
-  - ee/lib/gitlab/geo/geo_tasks.rb
-  - ee/lib/gitlab/geo/health_check.rb
-  - ee/lib/gitlab/geo/log_cursor/daemon.rb
   - ee/spec/services/ee/merge_requests/update_service_spec.rb
   - lib/backup/database.rb
   - lib/backup/manager.rb
diff --git a/ee/lib/gitlab/geo/geo_tasks.rb b/ee/lib/gitlab/geo/geo_tasks.rb
index 7b41cb8b501e8..3e6cd99d87264 100644
--- a/ee/lib/gitlab/geo/geo_tasks.rb
+++ b/ee/lib/gitlab/geo/geo_tasks.rb
@@ -18,7 +18,7 @@ def set_primary_geo_node
       end
 
       def set_secondary_as_primary
-        ActiveRecord::Base.transaction do
+        GeoNode.transaction do
           primary_node = GeoNode.primary_node
           current_node = GeoNode.current_node
 
diff --git a/ee/lib/gitlab/geo/health_check.rb b/ee/lib/gitlab/geo/health_check.rb
index 8fa4159243b4e..0b5cdbab6e9a8 100644
--- a/ee/lib/gitlab/geo/health_check.rb
+++ b/ee/lib/gitlab/geo/health_check.rb
@@ -19,8 +19,9 @@ def perform_checks
       end
 
       def db_replication_lag_seconds
+        # Geo currently only replicates the primary database (not the ci database)
         # Obtain the replication lag in seconds
-        ActiveRecord::Base.connection
+        ApplicationRecord.connection
           .execute(db_replication_lag_seconds_query)
           .first
           .fetch('replication_lag').to_i
@@ -71,7 +72,7 @@ def database_version
         strong_memoize(:database_version) do
           if defined?(ActiveRecord)
             connection = ::Geo::BaseRegistry.connection
-            schema_migrations_table_name = ActiveRecord::Base.schema_migrations_table_name
+            schema_migrations_table_name = ApplicationRecord.schema_migrations_table_name
 
             if connection.table_exists?(schema_migrations_table_name)
               connection.execute("SELECT MAX(version) AS version FROM #{schema_migrations_table_name}")
@@ -107,7 +108,7 @@ def archive_recovery_replication_enabled?
       end
 
       def streaming_replication_enabled?
-        !ActiveRecord::Base.connection
+        !ApplicationRecord.connection
           .execute("SELECT * FROM pg_last_wal_receive_lsn() as result")
           .first['result']
           .nil?
@@ -115,7 +116,7 @@ def streaming_replication_enabled?
 
       def some_replication_active?
         # Is some sort of replication active?
-        !ActiveRecord::Base.connection
+        !ApplicationRecord.connection
           .execute("SELECT * FROM pg_last_xact_replay_timestamp() as result")
           .first['result']
           .nil?
@@ -123,7 +124,7 @@ def some_replication_active?
 
       def streaming_replication_active?
         # This only works for Postgresql 9.6 and greater
-        ActiveRecord::Base.connection
+        ApplicationRecord.connection
           .select_values('SELECT pid FROM pg_stat_wal_receiver').first.to_i > 0
       end
     end
diff --git a/ee/lib/gitlab/geo/log_cursor/daemon.rb b/ee/lib/gitlab/geo/log_cursor/daemon.rb
index c9b07caa94fe9..09e8a94b1abd3 100644
--- a/ee/lib/gitlab/geo/log_cursor/daemon.rb
+++ b/ee/lib/gitlab/geo/log_cursor/daemon.rb
@@ -47,7 +47,7 @@ def find_and_handle_events!
 
           # Wrap this with the connection to make it possible to reconnect if
           # PGbouncer dies: https://github.com/rails/rails/issues/29189
-          ActiveRecord::Base.connection_pool.with_connection do
+          ::Geo::EventLog.connection_pool.with_connection do
             LogCursor::EventLogs.new.fetch_in_batches do |batch, last_id|
               break if exit?
 
-- 
GitLab