-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check DB connection is available by fetching a record
In a previous commit we attempted to check the DB is available by using `AxctiveRecord::Base.connection?`. However this will return false unless you have already opened a connection by (for example) fetching a record. Change the healthcheck endpoints to check the db is connected by both fetching a record and then checking for DB availability. Put this into a helper module so we can reuse it in both checks.
- Loading branch information
Laura Porter
committed
Dec 16, 2024
1 parent
1b78098
commit 161d237
Showing
6 changed files
with
43 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters