Hi guys,
A little bit context: I’m using Rails 5.2 with the Octopus
gem (https://github.com/thiagopradi/octopus) to manage multiple databases and I’m looking to upgrade to Rails 6. I have checked the docs (Multiple Databases with Active Record — Ruby on Rails Guides) but there are a few things I’m not clear about. Can you help me with them please. I need manual switching because there are cases like code running in a background job which I think the automatic connection switching doesn’t work.
1/ With Octopus
, I can do something like this:
users = User.using(:replica).where(...)
# after this, anything I read from users with be from the replica database, even if I'm passing it to a function some_func(users)
Is this the equivalent in Rails 6?
# Assume the reading role read from the replica database
ActiveRecord::Base.connected_to(role: :reading) do
users = User.where(...)
end
# what happen to users if I use it here?
ActiveRecord::Base.connected_to(role: :writing) do
# do something with users, e.g. users.each ....
# Is it still using the reading role?
end
2/ What happens if I have 2 nested connected_to
blocks? I know usually this can be refactored to 2 separate blocks but I want to be sure about its behavior.
3/ How does manual database connection switch work with schema cache? I haven’t seen any discussion about this so I’m assume there is no extra step to make it work for multiple replicas/shards to use the same schema cache file.
Thank you!