Skip to content

Commit 2aba1f5

Browse files
committed
Handle PG::ConnectionBad from PG#server_version similarly to version 0
1 parent 23956c7 commit 2aba1f5

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def get_database_version # :nodoc:
636636
with_raw_connection do |conn|
637637
version = conn.server_version
638638
if version == 0
639-
raise ActiveRecord::ConnectionFailed, "Could not determine PostgreSQL version"
639+
raise ActiveRecord::ConnectionNotEstablished, "Could not determine PostgreSQL version"
640640
end
641641
version
642642
end

activerecord/test/cases/adapters/postgresql/postgresql_adapter_test.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ def test_bad_connection_to_postgres_database
9595
end
9696
end
9797

98-
def test_reconnect_after_bad_connection_on_check_version
98+
def test_reconnect_after_bad_connection_on_check_version_with_0_return
9999
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
100100
connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(db_config.configuration_hash.merge(connection_retries: 0))
101101
connection.connect!
102102

103103
# mimic a connection that hasn't checked and cached the server version yet i.e. without a raw_connection
104104
connection.pool.instance_variable_set(:@server_version, nil)
105105
connection.raw_connection.stub(:server_version, 0) do
106-
error = assert_raises ActiveRecord::ConnectionFailed do
106+
error = assert_raises ActiveRecord::ConnectionNotEstablished do
107107
connection.reconnect!
108108
end
109109
assert_equal "Could not determine PostgreSQL version", error.message
@@ -115,6 +115,27 @@ def test_reconnect_after_bad_connection_on_check_version
115115
end
116116
end
117117

118+
def test_reconnect_after_bad_connection_on_check_version_with_native_exception
119+
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary")
120+
connection = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(db_config.configuration_hash.merge(connection_retries: 0))
121+
connection.connect!
122+
123+
# mimic a connection that hasn't checked and cached the server version yet i.e. without a raw_connection
124+
connection.pool.instance_variable_set(:@server_version, nil)
125+
# https://github.com/ged/ruby-pg/commit/a565e153d4d05955342ad24d4845378eee956935
126+
connection.raw_connection.stub(:server_version, -> { raise PG::ConnectionBad, "PQserverVersion() can't get server version" }) do
127+
error = assert_raises ActiveRecord::ConnectionNotEstablished do
128+
connection.reconnect!
129+
end
130+
assert_equal "PQserverVersion() can't get server version", error.message
131+
end
132+
133+
# can reconnect after a bad connection
134+
assert_nothing_raised do
135+
connection.reconnect!
136+
end
137+
end
138+
118139
def test_database_exists_returns_false_when_the_database_does_not_exist
119140
config = { database: "non_extant_database", adapter: "postgresql" }
120141
assert_not ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.database_exists?(config),

0 commit comments

Comments
 (0)