-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Get rid of mattr_accessor
in ActiveRecord::Core
#42445
Conversation
1890a30
to
d85c5bd
Compare
d85c5bd
to
427dbcb
Compare
427dbcb
to
85622b9
Compare
Otherwise initializer files with `Rails.application.active_record.*` might break.
d602f66
to
ba877bd
Compare
# So to preserve backward compatibility we copy the config a second time. | ||
if ActiveRecord.respond_to?(setter) | ||
ActiveRecord.send(setter, v) |
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.
Do we still need copying the config a first time in L215-L221?
rails/activerecord/lib/active_record/railtie.rb
Lines 212 to 237 in ba877bd
initializer "active_record.set_configs" do |app| | |
configs = app.config.active_record | |
configs.each do |k, v| | |
next if k == :encryption | |
setter = "#{k}=" | |
if ActiveRecord.respond_to?(setter) | |
ActiveRecord.send(setter, v) | |
end | |
end | |
ActiveSupport.on_load(:active_record) do | |
configs.each do |k, v| | |
next if k == :encryption | |
setter = "#{k}=" | |
# Some existing initializers might rely on Active Record configuration | |
# being copied from the config object to their actual destination when | |
# `ActiveRecord::Base` is loaded. | |
# So to preserve backward compatibility we copy the config a second time. | |
if ActiveRecord.respond_to?(setter) | |
ActiveRecord.send(setter, v) | |
else | |
send(setter, v) | |
end | |
end | |
end |
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.
Yes, it's a bit unfortunate, but some places where looking up AR::Base.some_config
causing AR::Base
autoloading and the on_load
callback.
But now that they access AR.some_config
they no longer trigger it.
I'm not particularly happy with this situation and am open to suggestion, because it's a bit unclear when configs are supposed to be accessible, and this reliance on autoload means referencing AR::Base
early can cause the copy to happen earlier than expected.
Rails 7 has introduced a breaking change which removed mattr_accessor methods in place of module instance methods rails/rails#42445
Most of those methods were public API in |
Ok, no problem I'll add deprecated delegators for all of them first thing tomorrow. Quick question, how do you know what's public API? I checked for these in https://api.rubyonrails.org/, and since they didn't show I assumed they were private API. |
@byroot That is a bug in RDoc where these methods just don't show up in documentation. See ruby/rdoc#795. |
All methods that had a |
|
||
def reading_role # :nodoc: | ||
ActiveSupport::Deprecation.warn(<<~MSG) | ||
ActiveRecord::Base.reading_role is deprecated and will be removed in Rails 7.0. |
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.
does this means this will be released in 6.2 or 6.1.x ?
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.
No it was a typo that got corrected later.
Introduced in rails/rails#42445 Example of a warning: ``` DEPRECATION WARNING: ActiveRecord::Base.writing_role is deprecated and will be removed in Rails 7.1. Use `ActiveRecord.writing_role` instead. (called from block (2 levels) in <top (required)> at /__w/discourse/discourse/spec/rails_helper.rb:304) ```
* DEV: Fix reading_role/writing_role deprecations Introduced in rails/rails#42445 Example of a warning: ``` DEPRECATION WARNING: ActiveRecord::Base.writing_role is deprecated and will be removed in Rails 7.1. Use `ActiveRecord.writing_role` instead. (called from block (2 levels) in <top (required)> at /__w/discourse/discourse/spec/rails_helper.rb:304) ``` * changelog * Add rails 7.0 to the ci matrix * reorder
since rails/rails#42445, this method has been changed from cattr_accessor to attr_accessor in ActiveRecord.
…to ActiveRecord - ref: rails/rails#42445 It also solved the following CI failure. - NoMethodError: undefined method `schema_format=' for ActiveRecord::Base:Class
related: #126 In Rails 7, the .schema_format= API is moved from ActiveRecord::Base to ActiveRecord - ref: rails/rails#42445 It also solved the following CI failure. ``` NoMethodError: undefined method `schema_format=' for ActiveRecord::Base:Class ```
See #42442 for full context.
In short class variables are slow, particularly in classes with many ancestors, and
ActiveRecord::Base
has over 60 ancestors.Only a small handful of these really matter performance wise, however the idea is to migrate them all regardless for consistency and to ensure future contributors will follow the new pattern rather than add new
mattr
.There are a few more in various Active Record concerns, I'll clean them up in a followup.
I'm opening mostly to get a green CI as the general principle was agreed by others.