File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
lib/active_record/connection_adapters
test/cases/adapters/postgresql Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -634,7 +634,11 @@ def use_insert_returning?
634
634
# Returns the version of the connected PostgreSQL server.
635
635
def get_database_version # :nodoc:
636
636
with_raw_connection do |conn |
637
- conn . server_version
637
+ version = conn . server_version
638
+ if version == 0
639
+ raise ActiveRecord ::ConnectionFailed , "Could not determine PostgreSQL version"
640
+ end
641
+ version
638
642
end
639
643
end
640
644
alias :postgresql_version :database_version
Original file line number Diff line number Diff line change @@ -95,6 +95,26 @@ def test_bad_connection_to_postgres_database
95
95
end
96
96
end
97
97
98
+ def test_reconnect_after_bad_connection_on_check_version
99
+ db_config = ActiveRecord ::Base . configurations . configs_for ( env_name : "arunit" , name : "primary" )
100
+ connection = ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter . new ( db_config . configuration_hash . merge ( connection_retries : 0 ) )
101
+ connection . connect!
102
+
103
+ # mimic a connection that hasn't checked and cached the server version yet i.e. without a raw_connection
104
+ connection . pool . instance_variable_set ( :@server_version , nil )
105
+ connection . raw_connection . stub ( :server_version , 0 ) do
106
+ error = assert_raises ActiveRecord ::ConnectionFailed do
107
+ connection . reconnect!
108
+ end
109
+ assert_equal "Could not determine PostgreSQL version" , error . message
110
+ end
111
+
112
+ # can reconnect after a bad connection
113
+ assert_nothing_raised do
114
+ connection . reconnect!
115
+ end
116
+ end
117
+
98
118
def test_database_exists_returns_false_when_the_database_does_not_exist
99
119
config = { database : "non_extant_database" , adapter : "postgresql" }
100
120
assert_not ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter . database_exists? ( config ) ,
You can’t perform that action at this time.
0 commit comments