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
Treat ActiveRecord::Base and ApplicationRecord as "primary" #36394
Treat ActiveRecord::Base and ApplicationRecord as "primary" #36394
Conversation
Does this mean that |
I'm not sure why you'd set up one that's different. ApplicationRecord already assumes that it's ActiveRecord::Base, it's just that we never call |
676574b
to
8666d09
Compare
When someone has a multi-db application their `ApplicationRecord` will look like: ```ruby class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to database: { writing: :primary, reading: :replica } end ``` This will cause us to open 2 connections to ActiveRecord::Base's database when we actually only want 1. This is because Rails sees `ApplicationRecord` and thinks it's a new connection, not the existing `ActiveRecord::Base` connection because the `connection_specification_name` is different. This PR changes `ApplicationRecord` classes to consider themselves the same as the "primary" connection. Fixes rails#36382
8666d09
to
2f8b397
Compare
To followup on this a bit Aaron and I talked on Slack about "why can't Rails just automatically connect on boot to the replica instead of manually connecting to the replica in ApplicationRecord?" There are a few reasons why:
As for the concerns regarding "what if someone is on a legacy app and they don't have an ApplicationRecord?"
And lastly "what if someone is connecting to one database for ActiveRecord::Base and another for ApplicationRecord?"
|
@@ -116,7 +116,7 @@ class User < ActiveRecord::Base | |||
RUBY | |||
|
|||
app_file "app/models/post.rb", <<-MODEL | |||
class Post < ActiveRecord::Base | |||
class Post < ApplicationRecord |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this because technically in a new app this is what Post would inherit from and checking defined?
loaded ApplicationRecord
in this test. In master if I change this line the same thing happens so I think that this more accurately demonstrates what a real app would load/unload.
…s-primary Treat ActiveRecord::Base and ApplicationRecord as "primary"
Fixed: rails#40559 When calling `connects_to` from `ApplicationRecord`, `owner_name` will be `ActiveRecord::Base` instead of `ApplicationRecord`. It is unexpected behavior. It means `ApplicationRecord.connection` can't resolve correct `owner_name` and can't fetch own `preventing_writes?`. This behavior introduced by rails#36394 But, I think that the new established connection is completely independent connection. If we want to consider the same as the `ActiveRecord::Base`, we can call `ActiveRecord::Base.connects_to` instead of. Co-authored-by: Ryuta Kamizono <kamipo@gmail.com>
When someone has a multi-db application their
ApplicationRecord
willlook like:
This will cause us to open 2 connections to ActiveRecord::Base's
database when we actually only want 1. This is because Rails sees
ApplicationRecord
and thinks it's a new connection, not the existingActiveRecord::Base
connection because theconnection_specification_name
is different.This PR changes
ApplicationRecord
classes to consider themselves thesame as the "primary" connection.
Fixes #36382
Note: this needs a backport to 6-0-stable
cc/ @tenderlove @jhawthorn @rafaelfranca @matthewd @morgoth