Skip to content

Commit

Permalink
Remove legacy_connection_handling
Browse files Browse the repository at this point in the history
This functionality has been deprecated since Rails 6.1 and can now be
removed. I've deleted all code, docs, references, and tests related to
this feature.
  • Loading branch information
eileencodes committed Apr 4, 2022
1 parent ca6d15b commit ad52c0a
Show file tree
Hide file tree
Showing 28 changed files with 60 additions and 2,278 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
* Remove `ActiveRecord.legacy_connection_handling`.

*Eileen M. Uchitelle*

* `rails db:schema:{dump,load}` now checks `ENV["SCHEMA_FORMAT"]` before config

Since `rails db:structure:{dump,load}` was deprecated there wasn't a simple
Expand Down
3 changes: 0 additions & 3 deletions activerecord/lib/active_record.rb
Expand Up @@ -178,9 +178,6 @@ module Tasks
singleton_class.attr_accessor :schema_cache_ignored_tables
self.schema_cache_ignored_tables = []

singleton_class.attr_accessor :legacy_connection_handling
self.legacy_connection_handling = true

singleton_class.attr_reader :default_timezone

# Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
Expand Down
1 change: 0 additions & 1 deletion activerecord/lib/active_record/connection_adapters.rb
Expand Up @@ -11,7 +11,6 @@ module ConnectionAdapters
autoload :Column
autoload :PoolConfig
autoload :PoolManager
autoload :LegacyPoolManager
autoload :SchemaCache
autoload :Deduplicable

Expand Down
Expand Up @@ -88,28 +88,6 @@ def prevent_writes=(prevent_writes) # :nodoc:
ActiveSupport::IsolatedExecutionState[:active_record_prevent_writes] = prevent_writes
end

# Prevent writing to the database regardless of role.
#
# In some cases you may want to prevent writes to the database
# even if you are on a database that can write. +while_preventing_writes+
# will prevent writes to the database for the duration of the block.
#
# This method does not provide the same protection as a readonly
# user and is meant to be a safeguard against accidental writes.
#
# See +READ_QUERY+ for the queries that are blocked by this
# method.
def while_preventing_writes(enabled = true)
unless ActiveRecord.legacy_connection_handling
raise NotImplementedError, "`while_preventing_writes` is only available on the connection_handler with legacy_connection_handling"
end

original, self.prevent_writes = self.prevent_writes, enabled
yield
ensure
self.prevent_writes = original
end

def connection_pool_names # :nodoc:
owner_to_pool_manager.keys
end
Expand Down Expand Up @@ -143,11 +121,7 @@ def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.curr
payload[:config] = db_config.configuration_hash
end

if ActiveRecord.legacy_connection_handling
owner_to_pool_manager[pool_config.connection_specification_name] ||= LegacyPoolManager.new
else
owner_to_pool_manager[pool_config.connection_specification_name] ||= PoolManager.new
end
owner_to_pool_manager[pool_config.connection_specification_name] ||= PoolManager.new
pool_manager = get_pool_manager(pool_config.connection_specification_name)
pool_manager.set_pool_config(role, shard, pool_config)

Expand Down
Expand Up @@ -151,16 +151,10 @@ def default_timezone

# Determines whether writes are currently being prevented.
#
# Returns true if the connection is a replica.
#
# If the application is using legacy handling, returns
# true if +connection_handler.prevent_writes+ is set.
#
# If the application is using the new connection handling
# will return true based on +current_preventing_writes+.
# Returns true if the connection is a replica or returns
# the value of +current_preventing_writes+.
def preventing_writes?
return true if replica?
return ActiveRecord::Base.connection_handler.prevent_writes if ActiveRecord.legacy_connection_handling
return false if connection_class.nil?

connection_class.current_preventing_writes
Expand Down

This file was deleted.

62 changes: 12 additions & 50 deletions activerecord/lib/active_record/connection_handling.rb
Expand Up @@ -135,18 +135,12 @@ def connects_to(database: {}, shards: {})
# Dog.first # finds first Dog record stored on the shard one replica
# end
def connected_to(role: nil, shard: nil, prevent_writes: false, &blk)
if ActiveRecord.legacy_connection_handling
if self != Base
raise NotImplementedError, "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling."
end
else
if self != Base && !abstract_class
raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
end
if self != Base && !abstract_class
raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
end

if name != connection_specification_name && !primary_class?
raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
end
if name != connection_specification_name && !primary_class?
raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
end

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

if ActiveRecord.legacy_connection_handling
raise NotImplementedError, "connected_to_many is not available with legacy connection handling"
end

if self != Base || classes.include?(Base)
raise NotImplementedError, "connected_to_many can only be called on ActiveRecord::Base."
end
Expand All @@ -196,10 +186,6 @@ def connected_to_many(*classes, role:, shard: nil, prevent_writes: false)
# It is not recommended to use this method in a request since it
# does not yield to a block like +connected_to+.
def connecting_to(role: default_role, shard: default_shard, prevent_writes: false)
if ActiveRecord.legacy_connection_handling
raise NotImplementedError, "`connecting_to` is not available with `legacy_connection_handling`."
end

prevent_writes = true if role == ActiveRecord.reading_role

append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
Expand Down Expand Up @@ -236,11 +222,7 @@ def shard_swapping_prohibited?
# See +READ_QUERY+ for the queries that are blocked by this
# method.
def while_preventing_writes(enabled = true, &block)
if ActiveRecord.legacy_connection_handling
connection_handler.while_preventing_writes(enabled, &block)
else
connected_to(role: current_role, prevent_writes: enabled, &block)
end
connected_to(role: current_role, prevent_writes: enabled, &block)
end

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

def lookup_connection_handler(handler_key) # :nodoc:
if ActiveRecord.legacy_connection_handling
handler_key ||= ActiveRecord.writing_role
connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
else
ActiveRecord::Base.connection_handler
end
ActiveRecord::Base.connection_handler
end

# Clears the query cache for all connections associated with the current thread.
def clear_query_caches_for_current_thread
if ActiveRecord.legacy_connection_handling
ActiveRecord::Base.connection_handlers.each_value do |handler|
clear_on_handler(handler)
end
else
clear_on_handler(ActiveRecord::Base.connection_handler)
end
clear_on_handler(ActiveRecord::Base.connection_handler)
end

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

if ActiveRecord.legacy_connection_handling
with_handler(role.to_sym) do
connection_handler.while_preventing_writes(prevent_writes) do
append_to_connected_to_stack(shard: shard, klasses: [self])
yield
end
end
else
append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
return_value = yield
return_value.load if return_value.is_a? ActiveRecord::Relation
return_value
end
append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
return_value = yield
return_value.load if return_value.is_a? ActiveRecord::Relation
return_value
ensure
self.connected_to_stack.pop
end
Expand Down
57 changes: 11 additions & 46 deletions activerecord/lib/active_record/core.rb
Expand Up @@ -109,33 +109,6 @@ def self.connection_handler=(handler)
ActiveSupport::IsolatedExecutionState[:active_record_connection_handler] = handler
end

def self.connection_handlers
if ActiveRecord.legacy_connection_handling
else
raise NotImplementedError, "The new connection handling does not support accessing multiple connection handlers."
end

@@connection_handlers ||= {}
end

def self.connection_handlers=(handlers)
if ActiveRecord.legacy_connection_handling
ActiveSupport::Deprecation.warn(<<~MSG)
Using legacy connection handling is deprecated. Please set
`legacy_connection_handling` to `false` in your application.
The new connection handling does not support `connection_handlers`
getter and setter.
Read more about how to migrate at: https://guides.rubyonrails.org/active_record_multiple_databases.html#migrate-to-the-new-connection-handling
MSG
else
raise NotImplementedError, "The new connection handling does not support multiple connection handlers."
end

@@connection_handlers = handlers
end

def self.asynchronous_queries_session # :nodoc:
asynchronous_queries_tracker.current_session
end
Expand All @@ -155,16 +128,12 @@ def self.asynchronous_queries_tracker # :nodoc:
# ActiveRecord::Base.current_role #=> :reading
# end
def self.current_role
if ActiveRecord.legacy_connection_handling
connection_handlers.key(connection_handler) || default_role
else
connected_to_stack.reverse_each do |hash|
return hash[:role] if hash[:role] && hash[:klasses].include?(Base)
return hash[:role] if hash[:role] && hash[:klasses].include?(connection_class_for_self)
end

default_role
connected_to_stack.reverse_each do |hash|
return hash[:role] if hash[:role] && hash[:klasses].include?(Base)
return hash[:role] if hash[:role] && hash[:klasses].include?(connection_class_for_self)
end

default_role
end

# Returns the symbol representing the current connected shard.
Expand Down Expand Up @@ -196,16 +165,12 @@ def self.current_shard
# ActiveRecord::Base.current_preventing_writes #=> false
# end
def self.current_preventing_writes
if ActiveRecord.legacy_connection_handling
connection_handler.prevent_writes
else
connected_to_stack.reverse_each do |hash|
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(Base)
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(connection_class_for_self)
end

false
connected_to_stack.reverse_each do |hash|
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(Base)
return hash[:prevent_writes] if !hash[:prevent_writes].nil? && hash[:klasses].include?(connection_class_for_self)
end

false
end

def self.connected_to_stack # :nodoc:
Expand Down Expand Up @@ -325,7 +290,7 @@ def find_by!(*args) # :nodoc:
end

%w(
reading_role writing_role legacy_connection_handling default_timezone index_nested_attribute_errors
reading_role writing_role default_timezone index_nested_attribute_errors
verbose_query_logs queues warn_on_records_fetched_greater_than maintain_test_schema
application_record_class action_on_strict_loading_violation schema_format error_on_ignored_order
timestamped_migrations dump_schema_after_migration dump_schemas suppress_multiple_database_warning
Expand Down
22 changes: 3 additions & 19 deletions activerecord/lib/active_record/query_cache.rb
Expand Up @@ -27,31 +27,15 @@ def uncached(&block)

def self.run
pools = []

if ActiveRecord.legacy_connection_handling
ActiveRecord::Base.connection_handlers.each do |key, handler|
pools.concat(handler.connection_pool_list.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
end
else
pools.concat(ActiveRecord::Base.connection_handler.all_connection_pools.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
end

pools.concat(ActiveRecord::Base.connection_handler.all_connection_pools.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
pools
end

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

if ActiveRecord.legacy_connection_handling
ActiveRecord::Base.connection_handlers.each do |_, handler|
handler.connection_pool_list.each do |pool|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
end
end
else
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
end
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
end
end

Expand Down
3 changes: 0 additions & 3 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -270,9 +270,6 @@ class Railtie < Rails::Railtie # :nodoc:
# and then establishes the connection.
initializer "active_record.initialize_database" do
ActiveSupport.on_load(:active_record) do
if ActiveRecord.legacy_connection_handling
self.connection_handlers = { ActiveRecord.writing_role => ActiveRecord::Base.default_connection_handler }
end
self.configurations = Rails.application.config.database_configuration

establish_connection
Expand Down

0 comments on commit ad52c0a

Please sign in to comment.