Skip to content

Commit 313dd2e

Browse files
authored
Merge pull request rails#44827 from rails/remove-legacy-connection-handling
Remove legacy_connection_handling
2 parents ca6d15b + ad52c0a commit 313dd2e

28 files changed

+60
-2278
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Remove `ActiveRecord.legacy_connection_handling`.
2+
3+
*Eileen M. Uchitelle*
4+
15
* `rails db:schema:{dump,load}` now checks `ENV["SCHEMA_FORMAT"]` before config
26

37
Since `rails db:structure:{dump,load}` was deprecated there wasn't a simple

activerecord/lib/active_record.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ module Tasks
178178
singleton_class.attr_accessor :schema_cache_ignored_tables
179179
self.schema_cache_ignored_tables = []
180180

181-
singleton_class.attr_accessor :legacy_connection_handling
182-
self.legacy_connection_handling = true
183-
184181
singleton_class.attr_reader :default_timezone
185182

186183
# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling

activerecord/lib/active_record/connection_adapters.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module ConnectionAdapters
1111
autoload :Column
1212
autoload :PoolConfig
1313
autoload :PoolManager
14-
autoload :LegacyPoolManager
1514
autoload :SchemaCache
1615
autoload :Deduplicable
1716

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

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,6 @@ def prevent_writes=(prevent_writes) # :nodoc:
8888
ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes] = prevent_writes
8989
end
9090

91-
# Prevent writing to the database regardless of role.
92-
#
93-
# In some cases you may want to prevent writes to the database
94-
# even if you are on a database that can write. +while_preventing_writes+
95-
# will prevent writes to the database for the duration of the block.
96-
#
97-
# This method does not provide the same protection as a readonly
98-
# user and is meant to be a safeguard against accidental writes.
99-
#
100-
# See +READ_QUERY+ for the queries that are blocked by this
101-
# method.
102-
def while_preventing_writes(enabled = true)
103-
unless ActiveRecord.legacy_connection_handling
104-
raise NotImplementedError, "`while_preventing_writes` is only available on the connection_handler with legacy_connection_handling"
105-
end
106-
107-
original, self.prevent_writes = self.prevent_writes, enabled
108-
yield
109-
ensure
110-
self.prevent_writes = original
111-
end
112-
11391
def connection_pool_names # :nodoc:
11492
owner_to_pool_manager.keys
11593
end
@@ -143,11 +121,7 @@ def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.curr
143121
payload[:config] = db_config.configuration_hash
144122
end
145123

146-
if ActiveRecord.legacy_connection_handling
147-
owner_to_pool_manager[pool_config.connection_specification_name] ||= LegacyPoolManager.new
148-
else
149-
owner_to_pool_manager[pool_config.connection_specification_name] ||= PoolManager.new
150-
end
124+
owner_to_pool_manager[pool_config.connection_specification_name] ||= PoolManager.new
151125
pool_manager = get_pool_manager(pool_config.connection_specification_name)
152126
pool_manager.set_pool_config(role, shard, pool_config)
153127

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,10 @@ def default_timezone
151151

152152
# Determines whether writes are currently being prevented.
153153
#
154-
# Returns true if the connection is a replica.
155-
#
156-
# If the application is using legacy handling, returns
157-
# true if +connection_handler.prevent_writes+ is set.
158-
#
159-
# If the application is using the new connection handling
160-
# will return true based on +current_preventing_writes+.
154+
# Returns true if the connection is a replica or returns
155+
# the value of +current_preventing_writes+.
161156
def preventing_writes?
162157
return true if replica?
163-
return ActiveRecord::Base.connection_handler.prevent_writes if ActiveRecord.legacy_connection_handling
164158
return false if connection_class.nil?
165159

166160
connection_class.current_preventing_writes

activerecord/lib/active_record/connection_adapters/legacy_pool_manager.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.

activerecord/lib/active_record/connection_handling.rb

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,12 @@ def connects_to(database: {}, shards: {})
135135
# Dog.first # finds first Dog record stored on the shard one replica
136136
# end
137137
def connected_to(role: nil, shard: nil, prevent_writes: false, &blk)
138-
if ActiveRecord.legacy_connection_handling
139-
if self != Base
140-
raise NotImplementedError, "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling."
141-
end
142-
else
143-
if self != Base && !abstract_class
144-
raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
145-
end
138+
if self != Base && !abstract_class
139+
raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
140+
end
146141

147-
if name != connection_specification_name && !primary_class?
148-
raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
149-
end
142+
if name != connection_specification_name && !primary_class?
143+
raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
150144
end
151145

152146
unless role || shard
@@ -172,10 +166,6 @@ def connected_to(role: nil, shard: nil, prevent_writes: false, &blk)
172166
def connected_to_many(*classes, role:, shard: nil, prevent_writes: false)
173167
classes = classes.flatten
174168

175-
if ActiveRecord.legacy_connection_handling
176-
raise NotImplementedError, "connected_to_many is not available with legacy connection handling"
177-
end
178-
179169
if self != Base || classes.include?(Base)
180170
raise NotImplementedError, "connected_to_many can only be called on ActiveRecord::Base."
181171
end
@@ -196,10 +186,6 @@ def connected_to_many(*classes, role:, shard: nil, prevent_writes: false)
196186
# It is not recommended to use this method in a request since it
197187
# does not yield to a block like +connected_to+.
198188
def connecting_to(role: default_role, shard: default_shard, prevent_writes: false)
199-
if ActiveRecord.legacy_connection_handling
200-
raise NotImplementedError, "`connecting_to` is not available with `legacy_connection_handling`."
201-
end
202-
203189
prevent_writes = true if role == ActiveRecord.reading_role
204190

205191
append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
@@ -236,11 +222,7 @@ def shard_swapping_prohibited?
236222
# See +READ_QUERY+ for the queries that are blocked by this
237223
# method.
238224
def while_preventing_writes(enabled = true, &block)
239-
if ActiveRecord.legacy_connection_handling
240-
connection_handler.while_preventing_writes(enabled, &block)
241-
else
242-
connected_to(role: current_role, prevent_writes: enabled, &block)
243-
end
225+
connected_to(role: current_role, prevent_writes: enabled, &block)
244226
end
245227

246228
# Returns true if role is the current connected role.
@@ -254,23 +236,12 @@ def connected_to?(role:, shard: ActiveRecord::Base.default_shard)
254236
end
255237

256238
def lookup_connection_handler(handler_key) # :nodoc:
257-
if ActiveRecord.legacy_connection_handling
258-
handler_key ||= ActiveRecord.writing_role
259-
connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
260-
else
261-
ActiveRecord::Base.connection_handler
262-
end
239+
ActiveRecord::Base.connection_handler
263240
end
264241

265242
# Clears the query cache for all connections associated with the current thread.
266243
def clear_query_caches_for_current_thread
267-
if ActiveRecord.legacy_connection_handling
268-
ActiveRecord::Base.connection_handlers.each_value do |handler|
269-
clear_on_handler(handler)
270-
end
271-
else
272-
clear_on_handler(ActiveRecord::Base.connection_handler)
273-
end
244+
clear_on_handler(ActiveRecord::Base.connection_handler)
274245
end
275246

276247
# Returns the connection currently associated with the class. This can
@@ -362,19 +333,10 @@ def with_handler(handler_key, &blk)
362333
def with_role_and_shard(role, shard, prevent_writes)
363334
prevent_writes = true if role == ActiveRecord.reading_role
364335

365-
if ActiveRecord.legacy_connection_handling
366-
with_handler(role.to_sym) do
367-
connection_handler.while_preventing_writes(prevent_writes) do
368-
append_to_connected_to_stack(shard: shard, klasses: [self])
369-
yield
370-
end
371-
end
372-
else
373-
append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
374-
return_value = yield
375-
return_value.load if return_value.is_a? ActiveRecord::Relation
376-
return_value
377-
end
336+
append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
337+
return_value = yield
338+
return_value.load if return_value.is_a? ActiveRecord::Relation
339+
return_value
378340
ensure
379341
self.connected_to_stack.pop
380342
end

activerecord/lib/active_record/core.rb

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,6 @@ def self.connection_handler=(handler)
109109
ActiveSupport::IsolatedExecutionState[:active_record_connection_handler] = handler
110110
end
111111

112-
def self.connection_handlers
113-
if ActiveRecord.legacy_connection_handling
114-
else
115-
raise NotImplementedError, "The new connection handling does not support accessing multiple connection handlers."
116-
end
117-
118-
@@connection_handlers ||= {}
119-
end
120-
121-
def self.connection_handlers=(handlers)
122-
if ActiveRecord.legacy_connection_handling
123-
ActiveSupport::Deprecation.warn(<<~MSG)
124-
Using legacy connection handling is deprecated. Please set
125-
`legacy_connection_handling` to `false` in your application.
126-
127-
The new connection handling does not support `connection_handlers`
128-
getter and setter.
129-
130-
Read more about how to migrate at: https://guides.rubyonrails.org/active_record_multiple_databases.html#migrate-to-the-new-connection-handling
131-
MSG
132-
else
133-
raise NotImplementedError, "The new connection handling does not support multiple connection handlers."
134-
end
135-
136-
@@connection_handlers = handlers
137-
end
138-
139112
def self.asynchronous_queries_session # :nodoc:
140113
asynchronous_queries_tracker.current_session
141114
end
@@ -155,16 +128,12 @@ def self.asynchronous_queries_tracker # :nodoc:
155128
# ActiveRecord::Base.current_role #=> :reading
156129
# end
157130
def self.current_role
158-
if ActiveRecord.legacy_connection_handling
159-
connection_handlers.key(connection_handler) || default_role
160-
else
161-
connected_to_stack.reverse_each do |hash|
162-
return hash[:role] if hash[:role] && hash[:klasses].include?(Base)
163-
return hash[:role] if hash[:role] && hash[:klasses].include?(connection_class_for_self)
164-
end
165-
166-
default_role
131+
connected_to_stack.reverse_each do |hash|
132+
return hash[:role] if hash[:role] && hash[:klasses].include?(Base)
133+
return hash[:role] if hash[:role] && hash[:klasses].include?(connection_class_for_self)
167134
end
135+
136+
default_role
168137
end
169138

170139
# Returns the symbol representing the current connected shard.
@@ -196,16 +165,12 @@ def self.current_shard
196165
# ActiveRecord::Base.current_preventing_writes #=> false
197166
# end
198167
def self.current_preventing_writes
199-
if ActiveRecord.legacy_connection_handling
200-
connection_handler.prevent_writes
201-
else
202-
connected_to_stack.reverse_each do |hash|
203-
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(Base)
204-
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(connection_class_for_self)
205-
end
206-
207-
false
168+
connected_to_stack.reverse_each do |hash|
169+
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(Base)
170+
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(connection_class_for_self)
208171
end
172+
173+
false
209174
end
210175

211176
def self.connected_to_stack # :nodoc:
@@ -325,7 +290,7 @@ def find_by!(*args) # :nodoc:
325290
end
326291

327292
%w(
328-
reading_role writing_role legacy_connection_handling default_timezone index_nested_attribute_errors
293+
reading_role writing_role default_timezone index_nested_attribute_errors
329294
verbose_query_logs queues warn_on_records_fetched_greater_than maintain_test_schema
330295
application_record_class action_on_strict_loading_violation schema_format error_on_ignored_order
331296
timestamped_migrations dump_schema_after_migration dump_schemas suppress_multiple_database_warning

activerecord/lib/active_record/query_cache.rb

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,15 @@ def uncached(&block)
2727

2828
def self.run
2929
pools = []
30-
31-
if ActiveRecord.legacy_connection_handling
32-
ActiveRecord::Base.connection_handlers.each do |key, handler|
33-
pools.concat(handler.connection_pool_list.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
34-
end
35-
else
36-
pools.concat(ActiveRecord::Base.connection_handler.all_connection_pools.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
37-
end
38-
30+
pools.concat(ActiveRecord::Base.connection_handler.all_connection_pools.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
3931
pools
4032
end
4133

4234
def self.complete(pools)
4335
pools.each { |pool| pool.disable_query_cache! }
4436

45-
if ActiveRecord.legacy_connection_handling
46-
ActiveRecord::Base.connection_handlers.each do |_, handler|
47-
handler.connection_pool_list.each do |pool|
48-
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
49-
end
50-
end
51-
else
52-
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
53-
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
54-
end
37+
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
38+
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
5539
end
5640
end
5741

activerecord/lib/active_record/railtie.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,6 @@ class Railtie < Rails::Railtie # :nodoc:
270270
# and then establishes the connection.
271271
initializer "active_record.initialize_database" do
272272
ActiveSupport.on_load(:active_record) do
273-
if ActiveRecord.legacy_connection_handling
274-
self.connection_handlers = { ActiveRecord.writing_role => ActiveRecord::Base.default_connection_handler }
275-
end
276273
self.configurations = Rails.application.config.database_configuration
277274

278275
establish_connection

0 commit comments

Comments
 (0)