Skip to content

DEV: Patch ActiveRecord's postgresql adapter .new_client #33820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions config/initializers/000-pg-connection-debug.rb

This file was deleted.

39 changes: 39 additions & 0 deletions config/initializers/000-pg_connection_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
def self.new_client(conn_params)
start = Time.now()
PG.connect(**conn_params)
rescue ::PG::Error => error
if conn_params && conn_params[:dbname] == "postgres"
raise ActiveRecord::ConnectionNotEstablished, error.message
# === PATCH START ===
# PostgreSQL's error message for a missing database looks something like this `FATAL: database "<databasename>" does not exist`.
# This patch updates the following conditional to be stricter and not match on other error messages that may contain the database name.
# For example, `<database>-some-host" (<some IP>), port 5432 failed: timeout expired after 11.30376464s` should not be considered
# a missing database error.
#
# Ideally, we want to include more of the missing database error message but we are unsure if the error message
# will change based on a user's locale and whether the locale is even selectable. The workaround now is already much better
# than the existing conditional.
elsif conn_params && conn_params[:dbname] &&
error.message.include?("\"#{conn_params[:dbname]}\"")
raise ActiveRecord::NoDatabaseError.db_error(conn_params[:dbname])
# === PATCH END ===
elsif conn_params && conn_params[:user] && error.message.include?(conn_params[:user])
# === PATCH START ===
error_message =
ActiveRecord::DatabaseConnectionError.username_error(conn_params[:user]).message
raise ActiveRecord::DatabaseConnectionError.new("#{error_message}\n#{error.message}")
# === PATCH END ===
elsif conn_params && conn_params[:host] && error.message.include?(conn_params[:host])
# === PATCH START ===
error_message =
ActiveRecord::DatabaseConnectionError.hostname_error(conn_params[:host]).message
raise ActiveRecord::DatabaseConnectionError.new("#{error_message}\n#{error.message}")
# === PATCH END ===
else
raise ActiveRecord::ConnectionNotEstablished, error.message
end
end
end
43 changes: 43 additions & 0 deletions spec/initializers/000_pg_connection_patch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

RSpec.describe "Patching ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#new_client" do
it "raises ActiveRecord::NoDatabaseError for a missing database" do
conn_params = { dbname: "non_existent_db" }

expect do
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new_client(conn_params)
end.to raise_error(ActiveRecord::NoDatabaseError, /non_existent_db/)
end

it "raises ActiveRecord::DatabaseConnectionError when error message contains the `host` connection params" do
conn_params = { host: "some-host", user: "test_user" }

PG.expects(:connect).raises(
::PG::Error.new(
"connection to server at \"some-host.name\" (::1), port 6432 failed: timeout expired after 11.30376464s",
),
)

expect do
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new_client(conn_params)
end.to raise_error(
ActiveRecord::DatabaseConnectionError,
/connection to server at "some-host.name"/,
)
end

it "raises ActiveRecord::DatabaseConnectionError when error message contains the `user` connection params" do
conn_params = { user: "test_user" }

PG.expects(:connect).raises(
::PG::Error.new("FATAL: password authentication failed for user \"test_user\""),
)

expect do
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new_client(conn_params)
end.to raise_error(
ActiveRecord::DatabaseConnectionError,
/password authentication failed for user "test_user"/,
)
end
end
Loading