Repository navigation
Conversation
|
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (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. |
kaspth
left a comment
There was a problem hiding this comment.
Alright, here's a massive review for you 💪
I really like where this is going 😁
There was a problem hiding this comment.
def reset_counters(id, *counters, touch: nil, **options) should work, no?
There was a problem hiding this comment.
Still mentions updated_at: true.
There was a problem hiding this comment.
def update_counters(id, touch: nil, **counters)?
There was a problem hiding this comment.
Changing the method signature to this causes several of the tests to fail.
There was a problem hiding this comment.
Better to name the var and send it along:
def increment_counter(counter_name, id, touch: nil)
update_counters(id, { counter_name => 1 }.merge(touch: touch)
endThere was a problem hiding this comment.
Note that this mehtod now raises if a counter without that id is not found. Probably a good thing.
There was a problem hiding this comment.
I'd write it as:
touch_time = Time.now
timestamps = resolve_timestamp_columns(find(id), touch) # Note the expected KW arg use.
timestamps.map do |column|
updates << "#{connection.quote_column_name(column.to_s)} = '#{touch_time}'"
endFor MySQL we track columns with millisecond precision if it's configured right, so I think we must capture the touch time. Also we needn't bother with DateTime, Active Record should typecast it properly besides we'd need a require "date" at the top of the file here.
There was a problem hiding this comment.
Why is the column quoted with double quotes, while the value uses singular? Doesn't column quoting use backticks too?
There was a problem hiding this comment.
I'd go with:
touch_time = Time.now
resolve_timestamp_columns(object, touch).map do |column|
updates[column] = touch_time
endAlternatively, we could return a hash and go with:
# Here:
unscoped.where(primary_key => object.id).update_all(
updates.merge(touch_updates(object, touch))
)
# In `update_counters`:
touch_updates(object, touch).map do |column, touch_time|
updates << %(#{connection.quote_column_name(column.to_s)} = "#{touch_time}")
end
def touch_updates(object, touch)
touch = object.timestamp_attributes_for_updates_in_model if touch == true
touch_time = Time.now
Array(touch).map { |column| [ column, touch_time ] }.to_h
end|
@kaspth Sorry it took me so long to get around to this. I've updated the branch with the suggestions from your code review. There were two items that I had issues with, and I left replies to your comments. I worked on this on and off over the past several days, so if I missed something please let me know. |
rafaelfranca
left a comment
There was a problem hiding this comment.
Thank you for the pull request. I made some comments about the implementation.
There was a problem hiding this comment.
This should be using current_time_from_proper_timezone
There was a problem hiding this comment.
This should be using current_time_from_proper_timezone
There was a problem hiding this comment.
This method is now doing a query even when touch is not passed. I think we should only do that if touch is provided.
There was a problem hiding this comment.
There are trailing spaces here.
There was a problem hiding this comment.
This method should be indented one more level.
There was a problem hiding this comment.
When touch is false it is trying to update it as a column. We should handle this case properly.
|
@rafaelfranca I've resolved all of the issues from your review. |
kaspth
left a comment
There was a problem hiding this comment.
Almost there! Squash your commits once the latest review has been implemented and then I think it's merge time. 😄
There was a problem hiding this comment.
Think it's odd we split out the timestamp handling to two places. Consider moving it like so:
touch = counters.delete(:touch)
updates = counters.map # …
if touch
object = find(id)
touch_time = object.send(:current_time_from_proper_timezone)
resolve_timestamp_columns(object, touch).each do |column|
updates << "#{connection.quote_column_name(column.to_s)} = '#{touch_time}'"
end
endSpares the local variable and also fixes the timestamps.map to use each :)
There was a problem hiding this comment.
Let's borrow the documentation from increment_counter:
# * <tt>:touch</tt> option - Touch timestamp columns when updating.
# Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
# touch that column or an array of symbols to touch just those ones.There was a problem hiding this comment.
timestamp_attributes_for_updates_in_model is private, so I'd rather we said +updated_at+ and/or +updated_on+ like elsewhere 😁
160de74 to
1a8a9f0
Compare
|
I've made the changes from the latest review, and squashed my commits into one commit. |
|
Going to merge if/when the tests pass. @jtrost lovely work on this! |
|
@jtrost looks like there's some failures in the mysql tests 😄 |
There was a problem hiding this comment.
Looks like we cannot use update_counters to the column named touch.
There was a problem hiding this comment.
ActiveRecord::Callbacks defines #touch, so if you have a column named touch then it will raise ActiveRecord::DangerousAttributeError.
There was a problem hiding this comment.
Oh... you are right, thank you for your explanation!
|
@kaspth Some of the builds have failing tests with: The |
There was a problem hiding this comment.
Should be the below:
resolve_timestamp_columns(object, touch).each do |column|
updates << "#{connection.quote_column_name(column.to_s)} = #{connection.quote(touch_time)}"
end|
@jtrost looks like Travis isn't rerunning the build because it can't be merged. Can you rebase on latest master and squash your commits? (Just rebase out mine from yesterday.) |
0f6dfab to
2e70cd1
Compare
|
Strange how these tests fail when the timestamp tests use the same assertions: .Figure it might be MySQL not using Another option is that the |
|
@jtrost I fixed the tests and applied some more fixes — you've been through the ringer enough! 😄 Thanks so much for helping out with this, I think it's a great feature to have ❤️ |
…thod `touch` option was added to `increment!` (#27660) and `update_counters` (#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, #31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
…thod `touch` option was added to `increment!` (rails#27660) and `update_counters` (rails#26995). But that option behaves inconsistently with `Persistence#touch` method. If `touch` option is passed attribute names, it won't update update_at/on attributes unlike `Persistence#touch` method. Due to changed from `Persistence#touch` to `increment!` with `touch` option, rails#31405 has a regression that `counter_cache` with `touch` option which is passed attribute names won't update update_at/on attributes. I think that the inconsistency is not intended. To get back consistency, ensure that `touch` option updates update_at/on attributes.
After messing something up with the original PR I decided to close that one and start anew.
Summary
This fixes #26724. There are cases in which calling the methods in
ActiveRecord::CounterCacheshould update theupdated_atvalue 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.@kaspth suggested this public API, which is implemented in this pull request:
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 option. I'll go through each method to explain the changes:
.reset_counters accepts any number of counters as a splat argument, so the
{touch: true}option is added as a double splat argument to separate it from thecountersargument..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:
Therefore, I tacked
touch: trueonto the end of thecountersargument, 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_countersdoes. These methods call.update_counters, so the options hash is merged into thecountershash.cc @eileencodes