From b4c0b2f9f71029b35ca7abd5c88fe1929f4e77c3 Mon Sep 17 00:00:00 2001
From: Ahmad Farhat <ahmad.af.farhat@gmail.com>
Date: Mon, 19 Jun 2023 13:41:05 -0400
Subject: [PATCH] Fix health check destroying db connections (#5249)

---
 app/controllers/health_checks_controller.rb | 5 +----
 lib/tasks/configuration.rake                | 4 +---
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/app/controllers/health_checks_controller.rb b/app/controllers/health_checks_controller.rb
index 13823ead..5283f338 100644
--- a/app/controllers/health_checks_controller.rb
+++ b/app/controllers/health_checks_controller.rb
@@ -37,10 +37,7 @@ class HealthChecksController < ApplicationController
   private
 
   def check_database
-    ActiveRecord::Base.establish_connection # Establishes connection
-    ActiveRecord::Base.connection # Calls connection object
-
-    raise 'Unable to connect to Database' unless ActiveRecord::Base.connected?
+    raise 'Unable to connect to Database' unless ActiveRecord::Base.connection.active?
     raise 'Unable to connect to Database - pending migrations' unless ActiveRecord::Migration.check_pending!.nil?
   rescue StandardError => e
     raise "Unable to connect to Database - #{e}"
diff --git a/lib/tasks/configuration.rake b/lib/tasks/configuration.rake
index 83fab74d..09af5a61 100644
--- a/lib/tasks/configuration.rake
+++ b/lib/tasks/configuration.rake
@@ -32,9 +32,7 @@ namespace :configuration do
 
     info 'Checking connection to Postgres Database:'
     begin
-      ActiveRecord::Base.establish_connection # Establishes connection
-      ActiveRecord::Base.connection # Calls connection object
-      failed('Unable to connect to Database') unless ActiveRecord::Base.connected?
+      failed('Unable to connect to Database') unless ActiveRecord::Base.connection.active?
     rescue StandardError => e
       failed("Unable to connect to Database - #{e}")
     end
-- 
GitLab