I’ve implemented multiple databases in Rails 6, but I didn’t implement multiple writers. Rails doesn’t know what your replica configuration is going to be like so for purposes of local development you should use the same database name for your primary and replica, and then have different user names. In production you should also use the same name (they are the same data you’re accessing) with different usernames (one set to readonly) but they shouldn’t be in the same place.
Ok after reading this over a few times I’ve realized that you wrote broken code on purpose to prove the switching was working.
Did you turn on the middleware that will automatically swap based on HTTP verb? If you didn’t your POST/GET requests won’t switch connections automatically.
If you’re in a model/controller/test you need to wrap it in a connected_to block like this
ActiveRecord::Base.connected_to(role: :reading) do
# read from your replica
end
Rails can’t know if your replica is up to date so we didn’t implement always select from the replica, you have to tell it you want that.
Hi Eileen, yes this way I had already tested and it works, but by keeping different usernames for same database doesn’t mean there are two database. But I would like to keep two database one is for Post/Put and other database just for GET and reading large queries no matter both the database are same just name is changed, is this possible with connects_to() ?
I’m curious what tutorial you followed, I’m wondering if it’s incorrect and we need to fix something. I also need to write docs in Rails itself before the 6 final is released.