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

Get rid of mattr_accessor in ActiveRecord::Core #42445

Merged
merged 12 commits into from
Jun 10, 2021

Conversation

casperisfine
Copy link
Contributor

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.

@casperisfine casperisfine force-pushed the active-record-cattr-2 branch 2 times, most recently from 1890a30 to d85c5bd Compare June 10, 2021 16:01
Otherwise initializer files with `Rails.application.active_record.*`
might break.
@byroot byroot merged commit 9bb2512 into rails:main Jun 10, 2021
Comment on lines +230 to +232
# So to preserve backward compatibility we copy the config a second time.
if ActiveRecord.respond_to?(setter)
ActiveRecord.send(setter, v)
Copy link
Member

@kamipo kamipo Jun 11, 2021

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?

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

Copy link
Contributor Author

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.

@casperisfine casperisfine deleted the active-record-cattr-2 branch June 11, 2021 06:48
Tonkpils added a commit to github/github-ds that referenced this pull request Jun 11, 2021
Rails 7 has introduced a breaking change which removed mattr_accessor
methods in place of module instance methods

rails/rails#42445
@rafaelfranca
Copy link
Member

Most of those methods were public API in ActiveRecord::Base. We need to deprecate them before removing.

@byroot
Copy link
Member

byroot commented Jun 14, 2021

Most of those methods were public API in ActiveRecord::Base. We need to deprecate them before removing.

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.

@zzak
Copy link
Member

zzak commented Jun 15, 2021

@byroot That is a bug in RDoc where these methods just don't show up in documentation. See ruby/rdoc#795.

@rafaelfranca
Copy link
Member

All methods that had a :singleton-method: comment are public API.


def reading_role # :nodoc:
ActiveSupport::Deprecation.warn(<<~MSG)
ActiveRecord::Base.reading_role is deprecated and will be removed in Rails 7.0.

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 ?

Copy link
Member

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.

CvX added a commit to discourse/rails_failover that referenced this pull request Jun 20, 2022
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)
```
oblakeerickson pushed a commit to discourse/rails_failover that referenced this pull request Apr 6, 2023
* 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
stefannibrasil added a commit to ConvertKit/impressionist that referenced this pull request Dec 26, 2023
since rails/rails#42445, this method has been
changed from cattr_accessor to attr_accessor in ActiveRecord.
otegami added a commit to otegami/redmine_full_text_search that referenced this pull request Mar 3, 2024
…to ActiveRecord

- ref: rails/rails#42445

It also solved the following CI failure.
- NoMethodError: undefined method `schema_format=' for ActiveRecord::Base:Class
kou pushed a commit to clear-code/redmine_full_text_search that referenced this pull request Mar 4, 2024
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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants