Skip to content

Commit

Permalink
Merge pull request #2038 from DFE-Digital/fix/check-db-connection-pro…
Browse files Browse the repository at this point in the history
…perly

Check DB connection is available by fetching a record
  • Loading branch information
Laura Porter authored Dec 16, 2024
2 parents 1b78098 + 161d237 commit 0a62a35
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
5 changes: 3 additions & 2 deletions app/api/v1/healthcheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class V1::Healthcheck < Grape::API

get :healthcheck do
content_type "text/plain"
body "Healthy" if ActiveRecord::Base.connected?

"Unhealthy"
return body "Healthy" if Ops::DbAvailability.db_available?

body "Unhealthy"
end
end
2 changes: 1 addition & 1 deletion app/controllers/healthcheck_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class HealthcheckController < ApplicationController
skip_before_action :redirect_unauthenticated_user

def check
return render plain: "Healthy" if ActiveRecord::Base.connected?
return render plain: "Healthy" if Ops::DbAvailability.db_available?

render plain: "Unhealthy"
end
Expand Down
6 changes: 6 additions & 0 deletions lib/ops/db_availability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Ops::DbAvailability
def self.db_available?
return true if User.first && ActiveRecord::Base.connected?
false
end
end
4 changes: 2 additions & 2 deletions spec/api/v1/healthcheck_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe V1::Healthcheck do
context "when the db connection is up" do
before { allow(ActiveRecord::Base).to receive(:connected?).and_return(true) }
before { allow(Ops::DbAvailability).to receive(:db_available?).and_return(true) }

it "returns 'Healthy'" do
get "/api/v1/healthcheck"
Expand All @@ -11,7 +11,7 @@
end

context "when the db connection is NOT up" do
before { allow(ActiveRecord::Base).to receive(:connected?).and_return(false) }
before { allow(Ops::DbAvailability).to receive(:db_available?).and_return(false) }

it "returns 'Unhealthy'" do
get "/api/v1/healthcheck"
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/ops/db_availability_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "rails_helper"

RSpec.describe "check DB availability" do
describe "db_available?" do
context "when the DB is available" do
it "returns true if there is a user" do
create(:user)

expect(Ops::DbAvailability.db_available?).to be true
end
end

context "when the DB is not available" do
before do
allow(ActiveRecord::Base).to receive(:connected?).and_return(false)
end

it "returns false if there is a user" do
create(:user)

expect(Ops::DbAvailability.db_available?).to be false
end

it "returns false if there is NO user and the db is NOT up" do
expect(Ops::DbAvailability.db_available?).to be false
end
end
end
end
4 changes: 2 additions & 2 deletions spec/requests/healthcheck_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe HealthcheckController, type: :request do
describe "#healthcheck" do
context "when the database is connected" do
before { allow(ActiveRecord::Base).to receive(:connected?).and_return(true) }
before { allow(Ops::DbAvailability).to receive(:db_available?).and_return(true) }

it "returns status 200 Healthy" do
get healthcheck_path
Expand All @@ -14,7 +14,7 @@
end

context "when the database is NOT connected" do
before { allow(ActiveRecord::Base).to receive(:connected?).and_return(false) }
before { allow(Ops::DbAvailability).to receive(:db_available?).and_return(false) }

it "returns status 200 Unhealthy" do
get healthcheck_path
Expand Down

0 comments on commit 0a62a35

Please sign in to comment.