Make `reading_role` and `writing_role` module instance variables

Followup: https://github.com/rails/rails/pull/42442
This commit is contained in:
Jean Boussier 2021-06-10 15:49:12 +02:00
parent c6e4dbeebb
commit 8ae1339ae0
13 changed files with 59 additions and 41 deletions

View File

@ -179,6 +179,12 @@ module ActiveRecord
singleton_class.attr_accessor :default_timezone
self.default_timezone = :utc
singleton_class.attr_accessor :writing_role
self.writing_role = :writing
singleton_class.attr_accessor :reading_role
self.reading_role = :reading
def self.eager_load!
super
ActiveRecord::Locking.eager_load!

View File

@ -184,7 +184,7 @@ module ActiveRecord
raise NotImplementedError, "connected_to_many can only be called on ActiveRecord::Base."
end
prevent_writes = true if role == reading_role
prevent_writes = true if role == ActiveRecord.reading_role
connected_to_stack << { role: role, shard: shard, prevent_writes: prevent_writes, klasses: classes }
yield
@ -204,7 +204,7 @@ module ActiveRecord
raise NotImplementedError, "`connecting_to` is not available with `legacy_connection_handling`."
end
prevent_writes = true if role == reading_role
prevent_writes = true if role == ActiveRecord.reading_role
self.connected_to_stack << { role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self] }
end
@ -240,7 +240,7 @@ module ActiveRecord
def lookup_connection_handler(handler_key) # :nodoc:
if ActiveRecord.legacy_connection_handling
handler_key ||= ActiveRecord::Base.writing_role
handler_key ||= ActiveRecord.writing_role
connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
else
ActiveRecord::Base.connection_handler
@ -356,7 +356,7 @@ module ActiveRecord
end
def with_role_and_shard(role, shard, prevent_writes)
prevent_writes = true if role == reading_role
prevent_writes = true if role == ActiveRecord.reading_role
if ActiveRecord.legacy_connection_handling
with_handler(role.to_sym) do

View File

@ -144,10 +144,6 @@ module ActiveRecord
class_attribute :strict_loading_by_default, instance_accessor: false, default: false
class_attribute :strict_loading_mode, instance_accessor: true, default: :all
mattr_accessor :writing_role, instance_accessor: false, default: :writing
mattr_accessor :reading_role, instance_accessor: false, default: :reading
class_attribute :has_many_inversing, instance_accessor: false, default: false
mattr_accessor :sqlite3_production_warning, instance_accessor: false, default: true
@ -357,7 +353,7 @@ module ActiveRecord
end
self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
self.default_role = writing_role
self.default_role = ActiveRecord.writing_role
self.default_shard = :default
def self.strict_loading_violation!(owner:, reflection:) # :nodoc:
@ -459,6 +455,22 @@ module ActiveRecord
ActiveRecord.default_timezone
end
def reading_role # :nodoc:
ActiveSupport::Deprecation.warn(<<~MSG)
ActiveRecord::Base.reading_role is deprecated and will be removed in Rails 7.0.
Use `ActiveRecord.reading_role` instead.
MSG
ActiveRecord.reading_role
end
def writing_role # :nodoc:
ActiveSupport::Deprecation.warn(<<~MSG)
ActiveRecord::Base.writing_role is deprecated and will be removed in Rails 7.0.
Use `ActiveRecord.writing_role` instead.
MSG
ActiveRecord.writing_role
end
def initialize_generated_modules # :nodoc:
generated_association_methods
end

View File

@ -50,7 +50,7 @@ module ActiveRecord
private
def read_from_primary(&blk)
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role, prevent_writes: true) do
ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role, prevent_writes: true) do
instrumenter.instrument("database_selector.active_record.read_from_primary") do
yield
end
@ -58,7 +58,7 @@ module ActiveRecord
end
def read_from_replica(&blk)
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.reading_role, prevent_writes: true) do
ActiveRecord::Base.connected_to(role: ActiveRecord.reading_role, prevent_writes: true) do
instrumenter.instrument("database_selector.active_record.read_from_replica") do
yield
end
@ -66,7 +66,7 @@ module ActiveRecord
end
def write_to_primary(&blk)
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role, prevent_writes: false) do
ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role, prevent_writes: false) do
instrumenter.instrument("database_selector.active_record.wrote_to_primary") do
yield
ensure

View File

@ -224,7 +224,7 @@ To keep using the current cache store, you can turn off cache versioning entirel
initializer "active_record.initialize_database" do
ActiveSupport.on_load(:active_record) do
if ActiveRecord.legacy_connection_handling
self.connection_handlers = { writing_role => ActiveRecord::Base.default_connection_handler }
self.connection_handlers = { ActiveRecord.writing_role => ActiveRecord::Base.default_connection_handler }
end
self.configurations = Rails.application.config.database_configuration

View File

@ -194,7 +194,7 @@ module ActiveRecord
# can see data in the open transaction on the writing connection.
def setup_shared_connection_pool
if ActiveRecord.legacy_connection_handling
writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]
writing_handler = ActiveRecord::Base.connection_handlers[ActiveRecord.writing_role]
ActiveRecord::Base.connection_handlers.values.each do |handler|
if handler != writing_handler
@ -221,7 +221,7 @@ module ActiveRecord
handler.connection_pool_names.each do |name|
pool_manager = handler.send(:owner_to_pool_manager)[name]
pool_manager.shard_names.each do |shard_name|
writing_pool_config = pool_manager.get_pool_config(ActiveRecord::Base.writing_role, shard_name)
writing_pool_config = pool_manager.get_pool_config(ActiveRecord.writing_role, shard_name)
@saved_pool_configs[name][shard_name] ||= {}
pool_manager.role_names.each do |role|
next unless pool_config = pool_manager.get_pool_config(role, shard_name)

View File

@ -2,6 +2,6 @@
class <%= abstract_class_name %> < ApplicationRecord
self.abstract_class = true
connects_to database: { <%= ActiveRecord::Base.writing_role %>: :<%= database -%> }
connects_to database: { <%= ActiveRecord.writing_role %>: :<%= database -%> }
end
<% end -%>

View File

@ -285,8 +285,8 @@ module ActiveRecord
end
def test_default_handlers_are_writing_and_reading
assert_equal :writing, ActiveRecord::Base.writing_role
assert_equal :reading, ActiveRecord::Base.reading_role
assert_equal :writing, ActiveRecord.writing_role
assert_equal :reading, ActiveRecord.reading_role
end
if Process.respond_to?(:fork)

View File

@ -362,21 +362,21 @@ module ActiveRecord
end
def test_default_handlers_are_writing_and_reading
assert_equal :writing, ActiveRecord::Base.writing_role
assert_equal :reading, ActiveRecord::Base.reading_role
assert_equal :writing, ActiveRecord.writing_role
assert_equal :reading, ActiveRecord.reading_role
end
def test_an_application_can_change_the_default_handlers
old_writing = ActiveRecord::Base.writing_role
old_reading = ActiveRecord::Base.reading_role
ActiveRecord::Base.writing_role = :default
ActiveRecord::Base.reading_role = :readonly
old_writing = ActiveRecord.writing_role
old_reading = ActiveRecord.reading_role
ActiveRecord.writing_role = :default
ActiveRecord.reading_role = :readonly
assert_equal :default, ActiveRecord::Base.writing_role
assert_equal :readonly, ActiveRecord::Base.reading_role
assert_equal :default, ActiveRecord.writing_role
assert_equal :readonly, ActiveRecord.reading_role
ensure
ActiveRecord::Base.writing_role = old_writing
ActiveRecord::Base.reading_role = old_reading
ActiveRecord.writing_role = old_writing
ActiveRecord.reading_role = old_reading
end
end
end

View File

@ -424,21 +424,21 @@ module ActiveRecord
end
def test_default_handlers_are_writing_and_reading
assert_equal :writing, ActiveRecord::Base.writing_role
assert_equal :reading, ActiveRecord::Base.reading_role
assert_equal :writing, ActiveRecord.writing_role
assert_equal :reading, ActiveRecord.reading_role
end
def test_an_application_can_change_the_default_handlers
old_writing = ActiveRecord::Base.writing_role
old_reading = ActiveRecord::Base.reading_role
ActiveRecord::Base.writing_role = :default
ActiveRecord::Base.reading_role = :readonly
old_writing = ActiveRecord.writing_role
old_reading = ActiveRecord.reading_role
ActiveRecord.writing_role = :default
ActiveRecord.reading_role = :readonly
assert_equal :default, ActiveRecord::Base.writing_role
assert_equal :readonly, ActiveRecord::Base.reading_role
assert_equal :default, ActiveRecord.writing_role
assert_equal :readonly, ActiveRecord.reading_role
ensure
ActiveRecord::Base.writing_role = old_writing
ActiveRecord::Base.reading_role = old_reading
ActiveRecord.writing_role = old_writing
ActiveRecord.reading_role = old_reading
end
end
end

View File

@ -760,7 +760,7 @@ class QueryCacheTest < ActiveRecord::TestCase
private
def with_temporary_connection_pool
pool_config = ActiveRecord::Base.connection_handler.send(:owner_to_pool_manager).fetch("ActiveRecord::Base").get_pool_config(ActiveRecord::Base.writing_role, :default)
pool_config = ActiveRecord::Base.connection_handler.send(:owner_to_pool_manager).fetch("ActiveRecord::Base").get_pool_config(ActiveRecord.writing_role, :default)
new_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(pool_config)
pool_config.stub(:pool, new_pool) do

View File

@ -93,7 +93,7 @@ class ActiveStorage::Preview
def process
previewer.preview(service_name: blob.service_name) do |attachable|
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role) do
image.attach(attachable)
end
end

View File

@ -41,7 +41,7 @@ class ActiveStorage::VariantWithRecord
def create_or_find_record(image:)
@record =
ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
ActiveRecord::Base.connected_to(role: ActiveRecord.writing_role) do
blob.variant_records.create_or_find_by!(variation_digest: variation.digest) do |record|
record.image.attach(image)
end