Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions activerecord/lib/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ module Tasks
autoload :TestDatabases, "active_record/test_databases"
autoload :TestFixtures, "active_record/fixtures"

singleton_class.attr_accessor :legacy_connection_handling
self.legacy_connection_handling = true

def self.eager_load!
super
ActiveRecord::Locking.eager_load!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def prevent_writes=(prevent_writes) # :nodoc:
# See +READ_QUERY+ for the queries that are blocked by this
# method.
def while_preventing_writes(enabled = true)
unless ActiveRecord::Base.legacy_connection_handling
unless ActiveRecord.legacy_connection_handling
raise NotImplementedError, "`while_preventing_writes` is only available on the connection_handler with legacy_connection_handling"
end

Expand Down Expand Up @@ -143,7 +143,7 @@ def establish_connection(config, owner_name: Base, role: ActiveRecord::Base.curr
payload[:config] = db_config.configuration_hash
end

if ActiveRecord::Base.legacy_connection_handling
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def use_metadata_table?
# will return true based on +current_preventing_writes+.
def preventing_writes?
return true if replica?
return ActiveRecord::Base.connection_handler.prevent_writes if ActiveRecord::Base.legacy_connection_handling
return ActiveRecord::Base.connection_handler.prevent_writes if ActiveRecord.legacy_connection_handling
return false if connection_klass.nil?

connection_klass.current_preventing_writes
Expand Down
14 changes: 7 additions & 7 deletions activerecord/lib/active_record/connection_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ 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 legacy_connection_handling
if ActiveRecord.legacy_connection_handling
if self != Base
raise NotImplementedError, "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling."
end
Expand Down Expand Up @@ -176,7 +176,7 @@ 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 legacy_connection_handling
if ActiveRecord.legacy_connection_handling
raise NotImplementedError, "connected_to_many is not available with legacy connection handling"
end

Expand All @@ -200,7 +200,7 @@ 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 legacy_connection_handling
if ActiveRecord.legacy_connection_handling
raise NotImplementedError, "`connecting_to` is not available with `legacy_connection_handling`."
end

Expand All @@ -221,7 +221,7 @@ def connecting_to(role: default_role, shard: default_shard, prevent_writes: fals
# See +READ_QUERY+ for the queries that are blocked by this
# method.
def while_preventing_writes(enabled = true, &block)
if legacy_connection_handling
if ActiveRecord.legacy_connection_handling
connection_handler.while_preventing_writes(enabled, &block)
else
connected_to(role: current_role, prevent_writes: enabled, &block)
Expand All @@ -239,7 +239,7 @@ def connected_to?(role:, shard: ActiveRecord::Base.default_shard)
end

def lookup_connection_handler(handler_key) # :nodoc:
if ActiveRecord::Base.legacy_connection_handling
if ActiveRecord.legacy_connection_handling
handler_key ||= ActiveRecord::Base.writing_role
connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
else
Expand All @@ -249,7 +249,7 @@ def lookup_connection_handler(handler_key) # :nodoc:

# Clears the query cache for all connections associated with the current thread.
def clear_query_caches_for_current_thread
if ActiveRecord::Base.legacy_connection_handling
if ActiveRecord.legacy_connection_handling
ActiveRecord::Base.connection_handlers.each_value do |handler|
clear_on_handler(handler)
end
Expand Down Expand Up @@ -358,7 +358,7 @@ def with_handler(handler_key, &blk)
def with_role_and_shard(role, shard, prevent_writes)
prevent_writes = true if role == reading_role

if ActiveRecord::Base.legacy_connection_handling
if ActiveRecord.legacy_connection_handling
with_handler(role.to_sym) do
connection_handler.while_preventing_writes(prevent_writes) do
self.connected_to_stack << { shard: shard, klasses: [self] }
Expand Down
10 changes: 4 additions & 6 deletions activerecord/lib/active_record/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ def self.configurations

class_attribute :default_shard, instance_writer: false

mattr_accessor :legacy_connection_handling, instance_writer: false, default: true

mattr_accessor :application_record_class, instance_accessor: false, default: nil

# Sets the async_query_executor for an application. By default the thread pool executor
Expand Down Expand Up @@ -225,7 +223,7 @@ def self.connection_handler=(handler)
end

def self.connection_handlers
if legacy_connection_handling
if ActiveRecord.legacy_connection_handling
else
raise NotImplementedError, "The new connection handling does not support accessing multiple connection handlers."
end
Expand All @@ -234,7 +232,7 @@ def self.connection_handlers
end

def self.connection_handlers=(handlers)
if legacy_connection_handling
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.
Expand Down Expand Up @@ -270,7 +268,7 @@ def self.asynchronous_queries_tracker # :nodoc:
# ActiveRecord::Base.current_role #=> :reading
# end
def self.current_role
if ActiveRecord::Base.legacy_connection_handling
if ActiveRecord.legacy_connection_handling
connection_handlers.key(connection_handler) || default_role
else
connected_to_stack.reverse_each do |hash|
Expand Down Expand Up @@ -311,7 +309,7 @@ def self.current_shard
# ActiveRecord::Base.current_preventing_writes #=> false
# end
def self.current_preventing_writes
if legacy_connection_handling
if ActiveRecord.legacy_connection_handling
connection_handler.prevent_writes
else
connected_to_stack.reverse_each do |hash|
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/query_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def uncached(&block)
def self.run
pools = []

if ActiveRecord::Base.legacy_connection_handling
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
Expand All @@ -42,7 +42,7 @@ def self.run
def self.complete(pools)
pools.each { |pool| pool.disable_query_cache! }

if ActiveRecord::Base.legacy_connection_handling
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?
Expand Down
19 changes: 15 additions & 4 deletions activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,22 @@ class Railtie < Rails::Railtie # :nodoc:
end

initializer "active_record.set_configs" do |app|
ActiveSupport.on_load(:active_record) do
configs = app.config.active_record
configs = app.config.active_record

configs.each do |k, v|
next if k == :encryption
setter = "#{k}="
if ActiveRecord.respond_to?(setter)
ActiveRecord.send(setter, v)
end
end

ActiveSupport.on_load(:active_record) do
configs.each do |k, v|
send "#{k}=", v if k != :encryption
next if k == :encryption
setter = "#{k}="
next if ActiveRecord.respond_to?(setter)
send(setter, v)
end
end
end
Expand All @@ -213,7 +224,7 @@ class Railtie < Rails::Railtie # :nodoc:
# and then establishes the connection.
initializer "active_record.initialize_database" do
ActiveSupport.on_load(:active_record) do
if ActiveRecord::Base.legacy_connection_handling
if ActiveRecord.legacy_connection_handling
self.connection_handlers = { writing_role => ActiveRecord::Base.default_connection_handler }
end
self.configurations = Rails.application.config.database_configuration
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/test_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def enlist_fixture_connections
# need to share a connection pool so that the reading connection
# can see data in the open transaction on the writing connection.
def setup_shared_connection_pool
if ActiveRecord::Base.legacy_connection_handling
if ActiveRecord.legacy_connection_handling
writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]

ActiveRecord::Base.connection_handlers.values.each do |handler|
Expand Down Expand Up @@ -236,7 +236,7 @@ def setup_shared_connection_pool
end

def teardown_shared_connection_pool
if ActiveRecord::Base.legacy_connection_handling
if ActiveRecord.legacy_connection_handling
@legacy_saved_pool_configs.each_pair do |handler, names|
names.each_pair do |name, shards|
shards.each_pair do |shard_name, pool_config|
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/adapter_prevent_writes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ def test_errors_when_an_insert_query_prefixed_by_a_double_dash_comment_containin

class AdapterPreventWritesLegacyTest < ActiveRecord::TestCase
def setup
@old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
@old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true

@connection = ActiveRecord::Base.connection
@connection_handler = ActiveRecord::Base.connection_handler
end

def teardown
clean_up_legacy_connection_handlers
ActiveRecord::Base.legacy_connection_handling = @old_value
ActiveRecord.legacy_connection_handling = @old_value
end

def test_preventing_writes_predicate_legacy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ class Mysql2AdapterPreventWritesLegacyTest < ActiveRecord::Mysql2TestCase
include DdlHelper

def setup
@old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
@old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true

@conn = ActiveRecord::Base.connection
@connection_handler = ActiveRecord::Base.connection_handler
end

def teardown
ActiveRecord::Base.legacy_connection_handling = @old_value
ActiveRecord.legacy_connection_handling = @old_value
end

def test_errors_when_an_insert_query_is_called_while_preventing_writes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ class PostgreSQLAdapterPreventWritesLegacyTest < ActiveRecord::PostgreSQLTestCas
include ConnectionHelper

def setup
@old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
@old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true

@connection = ActiveRecord::Base.connection
@connection_handler = ActiveRecord::Base.connection_handler
end

def teardown
ActiveRecord::Base.legacy_connection_handling = @old_value
ActiveRecord.legacy_connection_handling = @old_value
end

def test_errors_when_an_insert_query_is_called_while_preventing_writes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ class SQLite3AdapterPreventWritesLegacyTest < ActiveRecord::SQLite3TestCase
self.use_transactional_tests = false

def setup
@old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
@old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true

@conn = ActiveRecord::Base.connection

@connection_handler = ActiveRecord::Base.connection_handler
end

def teardown
ActiveRecord::Base.legacy_connection_handling = @old_value
ActiveRecord.legacy_connection_handling = @old_value
end

def test_errors_when_an_insert_query_is_called_while_preventing_writes
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/base_prevent_writes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ class BasePreventWritesTest < ActiveRecord::TestCase

class BasePreventWritesLegacyTest < ActiveRecord::TestCase
def setup
@old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
@old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true
ActiveRecord::Base.establish_connection :arunit
ARUnit2Model.establish_connection :arunit2
end

def teardown
clean_up_legacy_connection_handlers
ActiveRecord::Base.legacy_connection_handling = @old_value
ActiveRecord.legacy_connection_handling = @old_value
end

if !in_memory_db?
Expand Down
18 changes: 9 additions & 9 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1682,8 +1682,8 @@ def test_protected_environments_are_stored_as_an_array_of_string
end

test "cannot call connected_to on subclasses of ActiveRecord::Base with legacy connection handling" do
old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true

error = assert_raises(NotImplementedError) do
Bird.connected_to(role: :reading) { }
Expand All @@ -1692,7 +1692,7 @@ def test_protected_environments_are_stored_as_an_array_of_string
assert_equal "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling.", error.message
ensure
clean_up_legacy_connection_handlers
ActiveRecord::Base.legacy_connection_handling = old_value
ActiveRecord.legacy_connection_handling = old_value
end

test "cannot call connected_to with role and shard on non-abstract classes" do
Expand Down Expand Up @@ -1744,25 +1744,25 @@ def test_protected_environments_are_stored_as_an_array_of_string
end

test "#connecting_to doesn't work with legacy connection handling" do
old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true

assert_raises NotImplementedError do
SecondAbstractClass.connecting_to(role: :writing, prevent_writes: true)
end
ensure
ActiveRecord::Base.legacy_connection_handling = old_value
ActiveRecord.legacy_connection_handling = old_value
end

test "#connected_to_many doesn't work with legacy connection handling" do
old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true

assert_raises NotImplementedError do
ActiveRecord::Base.connected_to_many([SecondAbstractClass], role: :writing)
end
ensure
ActiveRecord::Base.legacy_connection_handling = old_value
ActiveRecord.legacy_connection_handling = old_value
end

test "#connected_to_many cannot be called on anything but ActiveRecord::Base" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class LegacyConnectionHandlersMultiDbTest < ActiveRecord::TestCase
fixtures :people

def setup
@old_value = ActiveRecord::Base.legacy_connection_handling
ActiveRecord::Base.legacy_connection_handling = true
@old_value = ActiveRecord.legacy_connection_handling
ActiveRecord.legacy_connection_handling = true
assert_deprecated do
ActiveRecord::Base.connection_handlers = { writing: ActiveRecord::Base.default_connection_handler }
end
Expand All @@ -29,7 +29,7 @@ def setup

def teardown
clean_up_legacy_connection_handlers
ActiveRecord::Base.legacy_connection_handling = @old_value
ActiveRecord.legacy_connection_handling = @old_value
end

class SecondaryBase < ActiveRecord::Base
Expand Down
Loading