Skip to content

Fix counter_cache decremented twice#32372

Closed
malikolivier wants to merge 5 commits intorails:masterfrom
malikolivier:fix-31494
Closed

Fix counter_cache decremented twice#32372
malikolivier wants to merge 5 commits intorails:masterfrom
malikolivier:fix-31494

Conversation

@malikolivier
Copy link
Copy Markdown

@malikolivier malikolivier commented Mar 29, 2018

This commit fixes the bug reported in #31493 and #32079.

It includes the following test that would fail without the patch as
follows:

Failure:
BelongsToAssociationsTest#test_decrement_counter_cache_once [rails/activerecord/test/cases/associations/belongs_to_associations_test.rb:778]:
Expected: -1
  Actual: -2

The fix was proposed first in #31494 by "gm_zhou gm_zhou@ctrip.com".
I included a failing test case on top of his contribution.

@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.

@kaspth
Copy link
Copy Markdown
Contributor

kaspth commented Apr 2, 2018

Hey, thanks for following up on this!

For good measure, can you use http://contributors.rubyonrails.org/faq#credit-several-people so the original author also gets credit? :)

@kamipo can you take a look here?

@malikolivier
Copy link
Copy Markdown
Author

@kaspth Thanks. I updated my commit message to giving credits according to the contribution guidelines you linked me too. Sorry I missed this step.

In the mean time, I updated the PR to simplify the unit tests. The original PR was changing the definitions of two models, which was in fact not necessary to reproduce the bug. Hope it's better then way.

@malikolivier
Copy link
Copy Markdown
Author

@kamipo Any feedback? :) Will this fix still be released on the 5.2 branch?

@kamipo
Copy link
Copy Markdown
Member

kamipo commented Apr 17, 2018

I have been busy lately. I will visit again in weekend.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repace => replace here and above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought has_key?was deprecated in favor of key?? I prefer the latter anyway.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this test unwieldy, can we simplify to this?

def test_decrement_counter_cache_once
  original_trainer = DogLover.create!
  dog = Dog.create!(trainer: original_trainer)

  assert_difference -> { original_trainer.reload.trained_dogs_count }, -1 do
    dog.update!(trainer: DogLover.create!)
  end
end

@malikolivier
Copy link
Copy Markdown
Author

malikolivier commented Jul 29, 2018

@kaspth Thank you! I fixed each of your review point in a specific commit. There are conflicts with master though. Should I fix the conflicts before any further review?

This commit fixes the bug reported in rails#31493 and rails#32079.

It includes the following test that would fail without the patch as
follows:

Failure:
BelongsToAssociationsTest#test_decrement_counter_cache_once
[rails/activerecord/test/cases/associations/belongs_to_associations_test.rb:778]:
Expected: -1
  Actual: -2

The fix was proposed in rails#31494 by "gm_zhou <gm_zhou@ctrip.com>".
The test was added by the author of this commit.

[Guangming Zhou, Malik Olivier Boussejra]
Test case was using changing the a dog's nested attributes using
`accepts_nested_attributes_for`. However, this is not required to
reproduce the bug fixed by 6107b37.

Indeed, the bug can be reproduced simply by having a model with two different
`counter_cache`.

As we do not change the definitions of dogs or dog_lovers, the overall impact
of the PR over the project is reduced by this commit.
key? is preferable over has_key?
@malikolivier
Copy link
Copy Markdown
Author

I rebased and fixed the conflicts.

kamipo added a commit to kamipo/rails that referenced this pull request Sep 18, 2018
This is a 4th attempt to make counter cache transactional completely.

Past attempts: rails#9236, rails#14849, rails#23357.

All existing counter cache issues (increment/decrement twice, lost
increment) are caused due to updating counter cache on the outside of
the record saving transaction by assigning belongs_to record, even
though assigning that doesn't cause the record saving.

We have the `@_after_replace_counter_called` guard condition to mitigate
double increment/decrement issues, but we can't completely prevent that
inconsistency as long as updating counter cache on the outside of the
transaction, since saving the record is not always happened after that.

We already have handling counter cache after create/update/destroy,

https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/counter_cache.rb#L162-L189
https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/associations/builder/belongs_to.rb#L33-L59

so just removing assigning logic on the belongs_to association makes
counter cache transactional completely.

Closes rails#14849.
Closes rails#23357.
Closes rails#31493.
Closes rails#31494.
Closes rails#32372.
Closes rails#33113.
Closes rails#33117
Closes rails#33129.
Closes rails#33458.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants