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

Mutation tracker should be cleared before continuing around callbacks #33689

Merged
merged 1 commit into from Aug 28, 2018

Conversation

Projects
None yet
6 participants
@ypresto
Copy link
Contributor

ypresto commented Aug 22, 2018

Summary

changes_applied should be called before continuing around callback chain. Otherwise the mutation tracker returns old value for methods like changed? or id_in_database in around callbacks. Also methods depend on id_in_database, like update_column, are not working in around_create callbacks.

class Foo < ActiveRecord::Base
  around_create :around_create_callback

  def around_create_callback
    ...
    yield
    p id_in_database # => nil
    update_column(:generated_column, generate_value) # silently fails
  end

  ...
end

Other Information

We found this issue with update_column called from around_create in our code (our generated value contains id, so we should generate after actual save of record).
It was working in Rails 5.1 and not in Rails 5.2.
The bug of update_column seems to be emerged by 41484eb, which changes method to fetch primary key from id to id_in_database.

Thanks for debugging this issue: @kimihito @elim

@ypresto ypresto force-pushed the ypresto:ar-fix-dirty-in-around branch Aug 22, 2018

@rails-bot

This comment has been minimized.

Copy link

rails-bot commented Aug 22, 2018

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @eileencodes (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.

@ypresto ypresto force-pushed the ypresto:ar-fix-dirty-in-around branch Aug 23, 2018

Mutation tracker should be cleared before continuing around callbacks
`changes_applied` should be called before continuing around callback
chain. Otherwise the mutation tracker returns old value for methods like
`changed`? or `id_in_database` in around callbacks. Also methods depend
on `id_in_database`, like `update_column`, are not working in
`around_create` callbacks.

```
class Foo < ActiveRecord::Base
  around_create :around_create_callback

  def around_create_callback
    ...
    yield
    p id_in_database # => nil
    update_column(:generated_column, generate_value) # silently fails
  end

  ...
end
```

@ypresto ypresto force-pushed the ypresto:ar-fix-dirty-in-around branch to 34f075f Aug 28, 2018

partial_writes? ? super(keys_for_partial_write) : super
affected_rows = partial_writes? ? super(keys_for_partial_write) : super
changes_applied
affected_rows

This comment has been minimized.

@kamipo

kamipo Aug 28, 2018

Member

I'm prefer not to care about what kind of value is returned in this module.
Can you use ensure instead?

diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 233ee29fac..58613f5e88 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -169,10 +169,14 @@ def write_attribute_without_type_cast(attr_name, _)

         def _update_record(*)
           partial_writes? ? super(keys_for_partial_write) : super
+        ensure
+          changes_applied
         end

         def _create_record(*)
           partial_writes? ? super(keys_for_partial_write) : super
+        ensure
+          changes_applied
         end

         def keys_for_partial_write

This comment has been minimized.

@kamipo

kamipo Aug 28, 2018

Member

hm... I've confirmed using ensure would change current behavior if any exception is raised from database.
We should not use ensure at least in this point.

@kamipo kamipo merged commit 188daa9 into rails:master Aug 28, 2018

1 of 2 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
codeclimate All good!
Details
@kamipo

This comment has been minimized.

Copy link
Member

kamipo commented Aug 28, 2018

Thank you, and congratulations on your first PR contribution to Rails 🎉

@bogdanvlviv

This comment has been minimized.

Copy link
Contributor

bogdanvlviv commented Aug 28, 2018

@kamipo Should we backport it to 5-2-stable?

@kamipo

This comment has been minimized.

Copy link
Member

kamipo commented Aug 28, 2018

Yeah, backported in bd6ac44.

kamipo added a commit that referenced this pull request Aug 28, 2018

Merge pull request #33689 from ypresto/ar-fix-dirty-in-around
Mutation tracker should be cleared before continuing around callbacks

@ypresto ypresto deleted the ypresto:ar-fix-dirty-in-around branch Aug 28, 2018

@sgrif

This comment has been minimized.

Copy link
Member

sgrif commented Sep 5, 2018

This is a change in behavior that will cause breakage in apps. We need to go through a deprecation cycle for this the same way we did for after_* callbacks

@sgrif

This comment has been minimized.

Copy link
Member

sgrif commented Sep 5, 2018

Scratch that, I see it was a change in behavior between 5.1 and 5.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.