Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't replace connections without a writing role #40580

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/test_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ def setup_shared_connection_pool
pool_manager = handler.send(:owner_to_pool_manager)[name]
pool_manager.shard_names.each do |shard_name|
writing_pool_config = writing_pool_manager.get_pool_config(nil, shard_name)
next unless writing_pool_config

pool_manager.set_pool_config(nil, shard_name, writing_pool_config)
end
end
Expand All @@ -214,6 +216,8 @@ def setup_shared_connection_pool
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)
next unless writing_pool_config

pool_manager.role_names.each do |role|
next unless pool_manager.get_pool_config(role, shard_name)
pool_manager.set_pool_config(role, shard_name, writing_pool_config)
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,14 @@ def test_only_existing_connections_are_replaced
end
end

def test_connections_with_no_writing_role_are_skipped
AbstractCompany.connects_to(database: { reading: :readonly })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So Rails kind of requires that there's a writing role, at least how it's configured currently. If you don't include one and boot a console, and try to query the db, you'll get an error because the default is to lookup on the writing role. I think this will be easier to change with granular connection management, but I haven't gotten around to doing that. Description of the problem is here #39580

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the context, I hadn't seen #39580.

My actual goal here is #40384, so that I can write tests for some code in our application that switches connections manually. Since that requires pool sharing to be reversible, I'm trying to understand all of the edge cases so that I'm confident they'll reverse correctly.

It's possible to write production code that works fine with no writing connection, as long as you wrap every query in a connected_to block, so I thought it was worthwhile to make that code behave the same during transactional tests. If you'd prefer not to add code to handle this case though, since it's not really supported, I'm fine with ignoring it.

handler = ActiveRecord::Base.connection_handler
assert_not_nil handler.retrieve_connection_pool("AbstractCompany", role: :reading)
ensure
AbstractCompany.connected_to(role: :reading) { AbstractCompany.remove_connection }
end

private
def config
{ "default" => default_config, "readonly" => readonly_config }
Expand Down Expand Up @@ -1575,6 +1583,14 @@ def test_only_existing_connections_are_replaced
end
end

def test_connections_with_no_writing_role_are_skipped
AbstractCompany.connects_to(database: { reading: :readonly })
reading_handler = ActiveRecord::Base.connection_handlers[:reading]
assert_not_nil reading_handler.retrieve_connection_pool("AbstractCompany")
ensure
ActiveRecord::Base.connected_to(role: :reading) { AbstractCompany.remove_connection }
end

private
def config
{ "default" => default_config, "readonly" => readonly_config }
Expand Down