DEV(cache_critical_dns): improve postgres_healthcheck

The `PG::Connection#ping` method is only reliable for checking if the
given host is accepting connections, and not if the authentication
details are valid.

This extends the healthcheck to confirm that the auth details are
able to both create a connection and execute queries against the
database.

We expect the empty query to return an empty result set, so we can
assert on that. If a failure occurs for any reason, the healthcheck will
return false.
This commit is contained in:
Michael Fitz-Payne 2022-05-23 13:41:15 +10:00 committed by Michael Fitz-Payne
parent 6c8f491dc3
commit 0553788d3b
1 changed files with 4 additions and 2 deletions

View File

@ -158,16 +158,18 @@ ensure
end
def postgres_healthcheck(host:, user:, password:, dbname:)
response = PG::Connection.ping(
client = PG::Connection.new(
host: host,
user: user,
password: password,
dbname: dbname,
connect_timeout: 2, # minimum
)
response == PG::Constants::PQPING_OK
client.exec(';').none?
rescue
false
ensure
client.close if client
end
HEALTH_CHECKS = {