Skip to content

Commit 0d0e6bd

Browse files
authored
Merge pull request rails#49378 from Shopify/ar-delay-version-check
Delay Adapter#check_version to #configure_connection
2 parents 0b2fb5d + 2da1852 commit 0d0e6bd

File tree

17 files changed

+45
-12
lines changed

17 files changed

+45
-12
lines changed

actioncable/test/subscription_adapter/postgresql_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def setup
2323
ActiveRecord::Base.establish_connection database_config
2424

2525
begin
26-
ActiveRecord::Base.connection
26+
ActiveRecord::Base.connection.connect!
2727
rescue
2828
@rx_adapter = @tx_adapter = nil
2929
skip "Couldn't connect to PostgreSQL: #{database_config.inspect}"

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def with_connection
243243

244244
# Returns true if a connection has already been opened.
245245
def connected?
246-
synchronize { @connections.any? }
246+
synchronize { @connections.any?(&:connected?) }
247247
end
248248

249249
# Returns an array containing the connections currently in the pool.
@@ -682,7 +682,6 @@ def remove_connection_from_thread_cache(conn, owner_thread = conn.owner)
682682
def new_connection
683683
connection = Base.public_send(db_config.adapter_method, db_config.configuration_hash)
684684
connection.pool = self
685-
connection.check_version
686685
connection
687686
rescue ConnectionNotEstablished => ex
688687
raise ex.set_pool(self)

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,13 @@ def check_all_foreign_keys_valid!
668668

669669
# CONNECTION MANAGEMENT ====================================
670670

671+
# Checks whether the connection to the database was established. This doesn't
672+
# include checking whether the database is actually capable of responding, i.e.
673+
# whether the connection is stale.
674+
def connected?
675+
!@raw_connection.nil?
676+
end
677+
671678
# Checks whether the connection to the database is still active. This includes
672679
# checking whether the database is actually capable of responding, i.e. whether
673680
# the connection isn't stale.
@@ -1215,6 +1222,7 @@ def build_result(columns:, rows:, column_types: {})
12151222
# Implementations may assume this method will only be called while
12161223
# holding @lock (or from #initialize).
12171224
def configure_connection
1225+
check_version
12181226
end
12191227

12201228
def default_prepared_statements

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,7 @@ def supports_rename_column?
870870
end
871871

872872
def configure_connection
873+
super
873874
variables = @config.fetch(:variables, {}).stringify_keys
874875

875876
# Increase timeout so the server doesn't disconnect us.

activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ def quote_string(string)
130130
# CONNECTION MANAGEMENT ====================================
131131
#++
132132

133+
def connected?
134+
!(@raw_connection.nil? || @raw_connection.closed?)
135+
end
136+
133137
def active?
134138
!!@raw_connection&.ping
135139
end

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ def initialize(...)
337337
@use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
338338
end
339339

340+
def connected?
341+
!(@raw_connection.nil? || @raw_connection.finished?)
342+
end
343+
340344
# Is this connection alive and ready for queries?
341345
def active?
342346
@lock.synchronize do
@@ -616,7 +620,9 @@ def use_insert_returning?
616620

617621
# Returns the version of the connected PostgreSQL server.
618622
def get_database_version # :nodoc:
619-
valid_raw_connection.server_version
623+
with_raw_connection do |conn|
624+
conn.server_version
625+
end
620626
end
621627
alias :postgresql_version :database_version
622628

@@ -998,6 +1004,8 @@ def reconnect
9981004
# Configures the encoding, verbosity, schema search path, and time zone of the connection.
9991005
# This is called by #connect and should not be called manually.
10001006
def configure_connection
1007+
super
1008+
10011009
if @config[:encoding]
10021010
@raw_connection.set_client_encoding(@config[:encoding])
10031011
end

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ def supports_concurrent_connections?
196196
!@memory_database
197197
end
198198

199-
def active?
200-
@raw_connection && !@raw_connection.closed?
199+
def connected?
200+
!(@raw_connection.nil? || @raw_connection.closed?)
201201
end
202202

203+
alias_method :active?, :connected?
204+
203205
def return_value_after_insert?(column) # :nodoc:
204206
column.auto_populated?
205207
end
@@ -723,6 +725,8 @@ def configure_connection
723725
end
724726
end
725727

728+
super
729+
726730
# Enforce foreign key constraints
727731
# https://www.sqlite.org/pragma.html#pragma_foreign_keys
728732
# https://www.sqlite.org/foreignkeys.html

activerecord/lib/active_record/connection_adapters/trilogy_adapter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ def quote_string(string)
131131
end
132132
end
133133

134+
def connected?
135+
!(@raw_connection.nil? || @raw_connection.closed?)
136+
end
137+
134138
def active?
135139
connection&.ping || false
136140
rescue ::Trilogy::Error

activerecord/lib/active_record/tasks/sqlite_database_tasks.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def connection
7070

7171
def establish_connection(config = db_config)
7272
ActiveRecord::Base.establish_connection(config)
73+
connection.connect!
7374
end
7475

7576
def run_cmd(cmd, args, out)

activerecord/lib/active_record/test_fixtures.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def setup_fixtures(config = ActiveRecord::Base)
158158
if connection_name
159159
begin
160160
connection = ActiveRecord::Base.connection_handler.retrieve_connection(connection_name, shard: shard)
161+
connection.connect! # eagerly validate the connection
161162
rescue ConnectionNotEstablished
162163
connection = nil
163164
end

0 commit comments

Comments
 (0)