Skip to content

Include option to update the updated_at value for methods in ActiveRe… - #26781

Closed
jtrost wants to merge 217 commits into
rails:masterfrom
jtrost:counter-cache-updated-at
Closed

jtrost wants to merge 217 commits into
rails:masterfrom
jtrost:counter-cache-updated-at

Conversation

@jtrost

@jtrost jtrost commented Oct 13, 2016

Copy link
Copy Markdown
Contributor

Summary

This fixes #26724. There are cases in which calling the methods in ActiveRecord::CounterCache should update the updated_at value of the affected records. For example, if a counter cache is included in a view that is fragment cached, it would be convenient if the fragment cache was expired when the counter cache was updated by changing the updated_at value.

Working with the API was a bit difficult here because each method in this module has a slightly different signature. This pull request ensures compatibility with the current implementation of these methods while providing this new feature. I'll go through each method to explain the changes:

.reset_counters accepts any number of counters as a splat argument, so the {updated_at: true} option is added as a double splat argument to separate it from the counters argument.

.update_counters accepts any number of counters as a hash. There is no way to add another argument at the end of this method signature since the current implementation looks like this:

Topic.update_counters(@topic.id, replies_count: 2, unique_replies_count: 2)

Therefore, I tacked updated_at: true onto the end of the counters argument, and it's removed from the hash before iterating through the counters.

.increment_counter and .decrement_counter accept a separate options hash much like .reset_counters does. These methods call .update_counters, so the options hash is merged into the counters hash.

meinac and others added 11 commits February 13, 2016 17:21
… partial_writes is false

This will fix rails#19663

Also with this fix, active record does not fire unnecassary update queries while partial_writes is true
Dynamically list available sql data types based on current database adapter.
…ion`

`ThroughReflection` initializes `@delegate_reflection` and delegate all
public methods to `delegate_reflection`. But `PolymorphicReflection`
does not initialize `@delegate_reflection`.
It is enough to inherit `AbstractReflection` (using `alias_candidate`
only).
Currently all adapters (postgresql, mysql2, sqlite3, oracle-enhanced,
and sqlserver) implemented `indexes` and schema dumper expects
implemented `indexes`.

https://github.com/rails/rails/blob/v5.0.0/activerecord/lib/active_record/schema_dumper.rb#L208

Therefore `respond_to?(:indexes)` checking is unnecessary.
Should be ActiveRecord::Attributes (ActiveModel::Attributes does not exist)
`ActiveRecord::Type::Value`

This is now defined in
`ActiveModel::Type::Value` (`ActiveRecord::Type::Value` still exists but
it's effectively an alias)
The default adapter in development & test environments is not `redis` but `async`.
@rails-bot

Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @kaspth (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

guilleiguaran and others added 3 commits October 13, 2016 11:59
Clarify default adapter in ActionCable guide  [ci skip]
If does not quote table name properly, invalid SQL is generated.
yahonda and others added 13 commits October 13, 2016 22:31
MySQL 8.0.0-dmr `SUB_PART` column of `information_schema.statistics`
changed to varbinary(12), which is bigint(3) in MySQL 5.6.

Addresses rails#26774
The methods Hash#transform_values and Hash#transform_values! have been
implemented in Ruby and they'll be available as part of the standard
library.

Here's the link to the discussion in Ruby's issue tracker:
https://bugs.ruby-lang.org/issues/12512

These methods are implemented in C so they're expected to perform
better.
Use built-in #transform_values when available.
Support index.length for MySQL 8.0.0-dmr
This replaces boilerplate in the “benchmark your code” section of the
contributors’ guide with an executable template. I also amended the text
to encourage best practices and codified it in the template.

For now this is only good for relatively self-contained changes that can
be inlined into a simple script. In the future, this can be expanded to
cover how to measure the difference between two commits.

The output looks like this:

```
==================================== Empty =====================================

Warming up --------------------------------------
              blank?   225.963k i/100ms
         fast_blank?   238.147k i/100ms
Calculating -------------------------------------
              blank?      8.825M (± 6.4%) i/s -     44.063M in   5.014824s
         fast_blank?      9.311M (± 6.3%) i/s -     46.439M in   5.009153s

Comparison:
         fast_blank?:  9310694.8 i/s
              blank?:  8824801.7 i/s - same-ish: difference falls within error


================================= Single Space =================================

Warming up --------------------------------------
              blank?    56.581k i/100ms
         fast_blank?   232.774k i/100ms
Calculating -------------------------------------
              blank?    813.985k (±16.7%) i/s -      4.017M in   5.076576s
         fast_blank?      9.547M (± 5.2%) i/s -     47.719M in   5.013204s

Comparison:
         fast_blank?:  9547414.0 i/s
              blank?:   813985.0 i/s - 11.73x  slower


================================== Two Spaces ==================================

Warming up --------------------------------------
              blank?    58.265k i/100ms
         fast_blank?   244.056k i/100ms
Calculating -------------------------------------
              blank?    823.343k (±16.2%) i/s -      4.020M in   5.014213s
         fast_blank?      9.484M (± 4.9%) i/s -     47.347M in   5.005339s

Comparison:
         fast_blank?:  9484021.6 i/s
              blank?:   823343.1 i/s - 11.52x  slower


=============================== Mixed Whitspaces ===============================

Warming up --------------------------------------
              blank?    53.919k i/100ms
         fast_blank?   237.103k i/100ms
Calculating -------------------------------------
              blank?    763.435k (±16.8%) i/s -      3.720M in   5.018029s
         fast_blank?      9.672M (± 5.8%) i/s -     48.369M in   5.019356s

Comparison:
         fast_blank?:  9672467.2 i/s
              blank?:   763435.4 i/s - 12.67x  slower


=============================== Very Long String ===============================

Warming up --------------------------------------
              blank?    34.037k i/100ms
         fast_blank?   240.366k i/100ms
Calculating -------------------------------------
              blank?    409.731k (± 8.9%) i/s -      2.042M in   5.028235s
         fast_blank?      9.794M (± 4.3%) i/s -     49.035M in   5.016328s

Comparison:
         fast_blank?:  9794225.2 i/s
              blank?:   409731.4 i/s - 23.90x  slower
```
When rendering arbitrary templates, it is helpful to not overwrite `env` keys with nil if they don't match any found in the `RACK_KEY_TRANSLATION`

This allows the developer to set the environment to exactly what is needed for rendering.
rothhound and others added 21 commits November 4, 2016 11:12
Under JRuby, the updates of the one shared variable interleaved,
causing threads to pick up each others' connections. I'm amazed
this worked on MRI.
Use different name for main and thread connection variable.
YAML and ERB were removed at 971d510
this test case fails with ruby 2.4, so we want to see what's actually happening
2.3: sprintf('%0.1f', 5.55)  #=> "5.5"
2.4: sprintf('%0.1f', 5.55)  #=> "5.6"

see: ruby/ruby@6ed8c79 and
ruby/ruby@295f60b
Update render yield docs to use `search` local variable instead of `@q` instance variable
Use local variable instead of instance variable [ci skip]
If `limit_value` is presented, records fetching order is very important
for performance. Should not unscope the order in the case.
…imit_value_present

Avoid `unscope(:order)` when `limit_value` is presented for `count`
Clear query cache during checkin, instead of an execution callback
  `bin/rails generate controller Comments` creates files
  app/assets/javascripts/comments.coffee
  app/assets/stylesheets/comments.scss
  not
  app/assets/javascripts/comment.coffee
  app/assets/stylesheets/comment.scss
Fix 'Getting Started with Rails' [ci skip]
Upgrade JRuby to satisfy gemfile
@kaspth

kaspth commented Nov 7, 2016

Copy link
Copy Markdown
Contributor

How about a :touch option as the public API for this:

# Uses `timestamp_attributes_for_update_in_model`:
Message.reset_counters [ 2, 18 ], comments_count: 5, touch: true

# Touch just one timestamp:
Message.reset_counters [ 2, 18 ], comments_count: 5, touch: :last_discussed_at

# The assumed rare case where you want to touch `updated_at` plus `last_discussed_at`:
Message.reset_counters [ 2, 18 ], comments_count: 5, touch: %i( updated_at last_discussed_at )

@jtrost

jtrost commented Nov 7, 2016

Copy link
Copy Markdown
Contributor Author

@kaspth I like that suggestion, and I updated this PR to use that API. I think that I may have made a mistake when rebasing with master because this PR now lists every commit merged into master between the time I originally opened this PR and now.

@kaspth

kaspth commented Nov 8, 2016

Copy link
Copy Markdown
Contributor

Yeah, it looks like it got messed up. Can you try rebasing again or something?

@jtrost jtrost closed this Nov 8, 2016
@jtrost
jtrost deleted the counter-cache-updated-at branch November 8, 2016 17:46
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.

ActiveRecord::CounterCache methods should have an option to change updated_at