Skip to content

Commit 7263da5

Browse files
committed
Deprecate ConnectionPool#connection
Replaced by `#lease_connection` to better reflect what it does. `ActiveRecord::Base#connection` is deprecated in the same way but without a removal timeline nor a deprecation warning. Inside the Active Record test suite, we do remove `Base.connection` to ensure it's not used internally. Some callsites have been converted to use `with_connection`, some other have been more simply migrated to `lease_connection` and will serve as a list of callsites to convert for rails#50793
1 parent 75e3407 commit 7263da5

File tree

277 files changed

+1467
-1390
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

277 files changed

+1467
-1390
lines changed

actioncable/test/subscription_adapter/postgresql_test.rb

Lines changed: 3 additions & 3 deletions
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.connect!
26+
ActiveRecord::Base.lease_connection.connect!
2727
rescue
2828
@rx_adapter = @tx_adapter = nil
2929
skip "Couldn't connect to PostgreSQL: #{database_config.inspect}"
@@ -68,7 +68,7 @@ def active?
6868
def test_default_subscription_connection_identifier
6969
subscribe_as_queue("channel") { }
7070

71-
identifiers = ActiveRecord::Base.connection.exec_query("SELECT application_name FROM pg_stat_activity").rows
71+
identifiers = ActiveRecord::Base.lease_connection.exec_query("SELECT application_name FROM pg_stat_activity").rows
7272
assert_includes identifiers, ["ActionCable-PID-#{$$}"]
7373
end
7474

@@ -81,7 +81,7 @@ def test_custom_subscription_connection_identifier
8181

8282
subscribe_as_queue("channel", adapter) { }
8383

84-
identifiers = ActiveRecord::Base.connection.exec_query("SELECT application_name FROM pg_stat_activity").rows
84+
identifiers = ActiveRecord::Base.lease_connection.exec_query("SELECT application_name FROM pg_stat_activity").rows
8585
assert_includes identifiers, ["hello-world-42"]
8686
end
8787
end

actionmailbox/test/migrations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ActionMailbox::MigrationsTest < ActiveSupport::TestCase
88
@original_verbose = ActiveRecord::Migration.verbose
99
ActiveRecord::Migration.verbose = false
1010

11-
@connection = ActiveRecord::Base.connection
11+
@connection = ActiveRecord::Base.lease_connection
1212
@original_options = Rails.configuration.generators.options.deep_dup
1313
end
1414

actionview/test/active_record_unit.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def setup
4444

4545
def reconnect
4646
return unless able_to_connect
47-
ActiveRecord::Base.connection.reconnect!
47+
ActiveRecord::Base.lease_connection.reconnect!
4848
load_schema
4949
end
5050

@@ -56,9 +56,9 @@ def setup_connection
5656
options = defaults.merge adapter: adapter, timeout: 500
5757
ActiveRecord::Base.establish_connection(options)
5858
ActiveRecord::Base.configurations = { "sqlite3_ar_integration" => options }
59-
ActiveRecord::Base.connection
59+
ActiveRecord::Base.lease_connection
6060

61-
Object.const_set :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name("type") unless Object.const_defined?(:QUOTED_TYPE)
61+
Object.const_set :QUOTED_TYPE, ActiveRecord::Base.lease_connection.quote_column_name("type") unless Object.const_defined?(:QUOTED_TYPE)
6262
else
6363
raise "Can't setup connection since ActiveRecord isn't loaded."
6464
end
@@ -67,7 +67,7 @@ def setup_connection
6767
# Load actionpack sqlite3 tables
6868
def load_schema
6969
File.read(File.expand_path("fixtures/db_definitions/sqlite.sql", __dir__)).split(";").each do |sql|
70-
ActiveRecord::Base.connection.execute(sql) unless sql.blank?
70+
ActiveRecord::Base.lease_connection.execute(sql) unless sql.blank?
7171
end
7272
end
7373

@@ -107,7 +107,7 @@ def run(*args)
107107
end
108108

109109
def capture_sql
110-
ActiveRecord::Base.connection.materialize_transactions
110+
ActiveRecord::Base.lease_connection.materialize_transactions
111111
SQLCounter.clear_log
112112
yield
113113
SQLCounter.log.dup

activerecord/CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
* Deprecate `ActiveRecord::Base.connection` in favor of `.lease_connection`
2+
3+
The method has been renamed as `lease_connection` to better reflect that the returned
4+
connection will be held for the duration of the request or job.
5+
6+
This deprecation is a soft deprecation, no warnings will be issued and there is no
7+
current plan to remove the method.
8+
9+
*Jean Boussier*
10+
11+
* Deprecate `ActiveRecord::ConnectionAdapters::ConnectionPool#connection`
12+
13+
The method has been renamed as `lease_connection` to better reflect that the returned
14+
connection will be held for the duration of the request or job.
15+
16+
*Jean Boussier*
17+
118
* Expose a generic fixture accessor for fixture names that may conflict with Minitest
219

320
```ruby

activerecord/examples/performance.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def self.email
176176
end
177177

178178
x.report "Model.log" do
179-
Exhibit.connection.send(:log, "hello", "world") { }
179+
Exhibit.lease_connection.send(:log, "hello", "world") { }
180180
end
181181

182182
x.report "AR.execute(query)" do
183-
ActiveRecord::Base.connection.execute("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}")
183+
ActiveRecord::Base.lease_connection.execute("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}")
184184
end
185185
end

activerecord/lib/active_record/associations/association.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def reset_negative_cache # :nodoc:
6363
# Reloads the \target and returns +self+ on success.
6464
# The QueryCache is cleared if +force+ is true.
6565
def reload(force = false)
66-
klass.connection.clear_query_cache if force && klass
66+
klass.connection_pool.clear_query_cache if force && klass
6767
reset
6868
reset_scope
6969
load_target
@@ -231,12 +231,14 @@ def find_target
231231
end
232232

233233
binds = AssociationScope.get_bind_values(owner, reflection.chain)
234-
sc.execute(binds, klass.connection) do |record|
235-
set_inverse_instance(record)
236-
if owner.strict_loading_n_plus_one_only? && reflection.macro == :has_many
237-
record.strict_loading!
238-
else
239-
record.strict_loading!(false, mode: owner.strict_loading_mode)
234+
klass.with_connection do |c|
235+
sc.execute(binds, c) do |record|
236+
set_inverse_instance(record)
237+
if owner.strict_loading_n_plus_one_only? && reflection.macro == :has_many
238+
record.strict_loading!
239+
else
240+
record.strict_loading!(false, mode: owner.strict_loading_mode)
241+
end
240242
end
241243
end
242244
end

activerecord/lib/active_record/associations/join_dependency/join_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def strict_loading?
9191
def append_constraints(join, constraints)
9292
if join.is_a?(Arel::Nodes::StringJoin)
9393
join_string = Arel::Nodes::And.new(constraints.unshift join.left)
94-
join.left = Arel.sql(base_klass.connection.visitor.compile(join_string))
94+
join.left = Arel.sql(base_klass.lease_connection.visitor.compile(join_string))
9595
else
9696
right = join.right
9797
right.expr = Arel::Nodes::And.new(constraints.unshift right.expr)

activerecord/lib/active_record/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ module ActiveRecord # :nodoc:
233233
#
234234
# Connections are usually created through
235235
# {ActiveRecord::Base.establish_connection}[rdoc-ref:ConnectionHandling#establish_connection] and retrieved
236-
# by ActiveRecord::Base.connection. All classes inheriting from ActiveRecord::Base will use this
236+
# by ActiveRecord::Base.lease_connection. All classes inheriting from ActiveRecord::Base will use this
237237
# connection. But you can also set a class-specific connection. For example, if Course is an
238238
# ActiveRecord::Base, but resides in a different database, you can just say <tt>Course.establish_connection</tt>
239239
# and Course and all of its subclasses will use this connection instead.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def flush_idle_connections!(role = nil)
190190
# for (not necessarily the current class).
191191
def retrieve_connection(connection_name, role: ActiveRecord::Base.current_role, shard: ActiveRecord::Base.current_shard) # :nodoc:
192192
pool = retrieve_connection_pool(connection_name, role: role, shard: shard, strict: true)
193-
pool.connection
193+
pool.lease_connection
194194
end
195195

196196
# Returns true if a connection that's accessible to this class has

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

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def db_config
6969
# Connections can be obtained and used from a connection pool in several
7070
# ways:
7171
#
72-
# 1. Simply use {ActiveRecord::Base.connection}[rdoc-ref:ConnectionHandling.connection].
72+
# 1. Simply use {ActiveRecord::Base.lease_connection}[rdoc-ref:ConnectionHandling.connection].
7373
# When you're done with the connection(s) and wish it to be returned to the pool, you call
7474
# {ActiveRecord::Base.connection_handler.clear_active_connections!}[rdoc-ref:ConnectionAdapters::ConnectionHandler#clear_active_connections!].
7575
# This is the default behavior for Active Record when used in conjunction with
@@ -139,46 +139,46 @@ def clear(connection)
139139
end
140140
end
141141

142-
if ObjectSpace.const_defined?(:WeakKeyMap) # RUBY_VERSION >= 3.3
143-
WeakKeyMap = ::ObjectSpace::WeakKeyMap # :nodoc:
144-
else
145-
class WeakKeyMap # :nodoc:
146-
def initialize
147-
@map = ObjectSpace::WeakMap.new
148-
@values = nil
149-
@size = 0
150-
end
151-
152-
alias_method :clear, :initialize
142+
class LeaseRegistry # :nodoc:
143+
if ObjectSpace.const_defined?(:WeakKeyMap) # RUBY_VERSION >= 3.3
144+
WeakKeyMap = ::ObjectSpace::WeakKeyMap # :nodoc:
145+
else
146+
class WeakKeyMap # :nodoc:
147+
def initialize
148+
@map = ObjectSpace::WeakMap.new
149+
@values = nil
150+
@size = 0
151+
end
153152

154-
def [](key)
155-
prune if @map.size != @size
156-
@map[key]
157-
end
153+
alias_method :clear, :initialize
158154

159-
def []=(key, value)
160-
@map[key] = value
161-
prune if @map.size != @size
162-
value
163-
end
155+
def [](key)
156+
prune if @map.size != @size
157+
@map[key]
158+
end
164159

165-
def delete(key)
166-
if value = self[key]
167-
self[key] = nil
168-
prune
160+
def []=(key, value)
161+
@map[key] = value
162+
prune if @map.size != @size
163+
value
169164
end
170-
value
171-
end
172165

173-
private
174-
def prune(force = false)
175-
@values = @map.values
176-
@size = @map.size
166+
def delete(key)
167+
if value = self[key]
168+
self[key] = nil
169+
prune
170+
end
171+
value
177172
end
173+
174+
private
175+
def prune(force = false)
176+
@values = @map.values
177+
@size = @map.size
178+
end
179+
end
178180
end
179-
end
180181

181-
class LeaseRegistry # :nodoc:
182182
def initialize
183183
@mutex = Mutex.new
184184
@map = WeakKeyMap.new
@@ -293,7 +293,13 @@ def lease_connection
293293
lease.connection ||= checkout
294294
end
295295

296-
alias_method :connection, :lease_connection # TODO: deprecate
296+
def connection
297+
ActiveRecord.deprecator.warn(<<~MSG)
298+
ConnectionPoool#connection is deprecated and will be removed
299+
in Rails 7.3. Use #lease_connection instead
300+
MSG
301+
lease_connection
302+
end
297303

298304
def pin_connection!(lock_thread) # :nodoc:
299305
raise "There is already a pinned connection" if @pinned_connection

0 commit comments

Comments
 (0)