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

MONGOID-5274 / MONGOID-5142 - Rework #touch with embedded documents #5045

Merged
merged 20 commits into from
Oct 4, 2022

Conversation

johnnyshields
Copy link
Contributor

@johnnyshields johnnyshields commented Aug 20, 2021

Fixes MONGOID-5142
Fixes MONGOID-5274
Correct fix for MONGOID-5136 (the previous one was non-optimal)
Replaces PR #5039 (includes specs from it, which are passing here)

Changes

  • Perform all touch operations on the document in a single write operation (MONGOID-5142)
  • Remove touches from the atomic set operations, so that they are no longer there when saving the next time.
  • Cleanup/refactor touch method to be much more readable. The code is also now symmetrical for parents and child docs, everything is nicely recursive.

@johnnyshields johnnyshields changed the title MONGOID-5136 Fix touch with custom field on embedded documents MONGOID-5136 Fix touch with custom field on embedded documents & perform all touches on root + childs in single write Aug 20, 2021
@johnnyshields johnnyshields changed the title MONGOID-5136 Fix touch with custom field on embedded documents & perform all touches on root + childs in single write MONGOID-5136 Fix touch with custom field on embedded documents Aug 20, 2021
@johnnyshields
Copy link
Contributor Author

@p-mongo if you can help with the specs here would be greatly appreciated.

@p-mongo
Copy link
Contributor

p-mongo commented Oct 4, 2021

Hi @johnnyshields , can you please update the title and description of this PR to reflect what it contains? MONGOID-5136 is already fixed.

Can you also clarify whether this PR is ready for review? If it isn't it can be marked [wip] or draft.

@johnnyshields johnnyshields changed the title MONGOID-5136 Fix touch with custom field on embedded documents [WIP] MONGOID-5142 Fix touch with custom field on embedded documents Oct 5, 2021
@johnnyshields
Copy link
Contributor Author

I've raised #5202 to point out some strange/incorrect behavior re: #touch method. This PR is on hold until the issues are resolved.

@johnnyshields johnnyshields marked this pull request as draft April 25, 2022 15:01
@johnnyshields johnnyshields force-pushed the MONGOID-5136-super-touch branch from 604686f to e5e809c Compare June 4, 2022 18:34
@johnnyshields johnnyshields marked this pull request as ready for review June 4, 2022 20:41
@johnnyshields johnnyshields changed the title [WIP] MONGOID-5142 Fix touch with custom field on embedded documents MONGOID-5274 / MONGOID-5142 - Rework #touch with embedded documents Jun 4, 2022
@johnnyshields
Copy link
Contributor Author

@Neilshweky please review this.

@Neilshweky
Copy link
Contributor

Can you add a test for touching with grandchildren? I would like to see how this works with multiple generations of descendants.

@Neilshweky
Copy link
Contributor

If I understand this correctly, your PR makes the following changes:

  • touches are now persisted immediately (backwards breaking).
    • which causes touch callbacks to be called earlier.
  • touches are no longer included in the atomic_updates calculation on save.
    • can you put a test for this?
  • touch operations are done in one operation.
  • embedded touches no longer use after_touch callbacks to update associated documents.

Would you say this is a good summary of the effective changes in this PR?

I think the only backwards breaking change here would be the fact that touches are persisted immediately. Personally this doesn't bother me as a change, although release notes would have to be written about it. @p-mongo is that change okay in your opinion?

Copy link
Contributor

@Neilshweky Neilshweky left a comment

Choose a reason for hiding this comment

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

Please review my comments above.

lib/mongoid/touchable.rb Outdated Show resolved Hide resolved
lib/mongoid/touchable.rb Show resolved Hide resolved
lib/mongoid/touchable.rb Outdated Show resolved Hide resolved
spec/mongoid/touchable_spec.rb Show resolved Hide resolved
lib/mongoid/touchable.rb Show resolved Hide resolved
@johnnyshields
Copy link
Contributor Author

johnnyshields commented Jun 10, 2022

@Neilshweky thanks for the review. Let me first respond to your summary of changes so we're on same page:

  1. touches are now persisted immediately (backwards breaking). which causes touch callbacks to be called earlier.

AFAIK the old code was the same. The old code has _root.collection.find(selector).update_one(...). Please double check this? I think this should be fixed in a separate PR so touch updates happen as part of the actual write on persistence (at least when doing touch: true on embedded relations.)

  1. touches are no longer included in the atomic_updates calculation on save. can you put a test for this?

No. touch_atomic_updates was never actually part of atomic_updates. Its code just happened to be in the same file, where it doesn't belong.

  1. touch operations are done in one operation.

Yes. This is the primary intention of the PR.

  1. embedded touches no longer use after_touch callbacks to update associated documents.

In the new code, calling the #touch method on an embedded now recursively touches its parent(s) within the #touch method itself, so there's no need for to use after_touch callbacks. So this is not actually a change to the end result behavior. In the future, for save/destroy of embedded relations we should actually be doing the touch operation as part of the persistence itself, or in a before callback so that it happens as part of the main save operation.

@p-mongo
Copy link
Contributor

p-mongo commented Jun 10, 2022

Neither https://jira.mongodb.org/browse/MONGOID-5142 nor https://jira.mongodb.org/browse/MONGOID-5142 specify making touch perform an immediate write, so I would say this shouldn't be done.

If you believe it is a desired behavior, @johnnyshields please feel free to create a ticket for it describing your use case for it. My thinking is that perhaps touch! could be added which would immediately persist the write (and that would have to deal with validations, for example what if there is a validation requiring updated_at to be older than a certain time, touching such a document could make it invalid).

@p-mongo
Copy link
Contributor

p-mongo commented Jun 10, 2022

https://jira.mongodb.org/browse/MONGOID-5274 was the other ticket.

@johnnyshields
Copy link
Contributor Author

johnnyshields commented Jun 11, 2022

perform an immediate write, so I would say this shouldn't be done.

@p-mongo as mentioned in comment above to Neil, I believe the existing code also performs an immediate write. See _root.collection.find(selector).update_one(...) in existing code.

I agree that an immediate write is not desirable, however, here I intentionally tried to keep the behavior consistent. I think we should defer the write in a follow-up ticket.

@johnnyshields
Copy link
Contributor Author

@Neilshweky I've responded to all your comments here. Please kindly resolve the comments or give further response.

@Neilshweky Neilshweky self-requested a review June 21, 2022 14:29
@Neilshweky
Copy link
Contributor

perform an immediate write, so I would say this shouldn't be done.

@p-mongo as mentioned in comment above to Neil, I believe the existing code also performs an immediate write. See _root.collection.find(selector).update_one(...) in existing code.

I agree that an immediate write is not desirable, however, here I intentionally tried to keep the behavior consistent. I think we should defer the write in a follow-up ticket.

I can confirm that the current functionality on master is to persist touch operations immediately.

@Neilshweky
Copy link
Contributor

Can you add a test for touching with grandchildren? I would like to see how this works with multiple generations of descendants.

@johnnyshields can you take care of this and then I'll give this another look?

Copy link
Contributor

@Neilshweky Neilshweky left a comment

Choose a reason for hiding this comment

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

Please make the testing changes mentioned above:

  • multi-generational test
  • third model with touch not set

lib/mongoid/touchable.rb Outdated Show resolved Hide resolved
@johnnyshields
Copy link
Contributor Author

@Neilshweky all comments addressed.

@johnnyshields
Copy link
Contributor Author

johnnyshields commented Oct 3, 2022

@Neilshweky I appreciate if you can take over this PR from here, if there are further changes needed. I have a busy few weeks ahead of me and won't be able to devote further attention for awhile.

@Neilshweky Neilshweky requested a review from comandeo October 3, 2022 16:25
@Neilshweky
Copy link
Contributor

@comandeo I am pretty much done with my review, if you wouldn't mind taking a look here.

@Neilshweky
Copy link
Contributor

Good job @johnnyshields, this really cleans up this part of the code.

Copy link
Contributor

@comandeo comandeo left a comment

Choose a reason for hiding this comment

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

Great job! 👏

@Neilshweky
Copy link
Contributor

Open questions/todos:

  • Should this go into 8.1/9.0?

Let's do 9.0 since the repairing of touch: false on embedded associations can be a BC break.

  • What is default behavior of touch option for embedded associations?

Does this PR change the default touch behavior for embedded associations?

  • Since we have decided to put this into 9.0 there needs to be release notes stating that touch: false for embedded associations has been repaired... @johnnyshields can you handle that? You can even open a separate PR if you like...

@johnnyshields
Copy link
Contributor Author

johnnyshields commented Oct 4, 2022

@Neilshweky agreed it should be 9.0.

I'm happy to help with release notes, BUT:

Are we also going to change the default value to touch: true for embedded? That will have some bearing on the release notes. AFAIK from this PR the main notable things are:

  1. #touch on embedded documents now happen in a single DB call; and
  2. #touch on embedded documents no longer automatically touch the parent documents by default, due to a bugfix.

However, if touch: true, then (2) in effect won't change by default. (The change is less impactful if we set touch: true)

@Neilshweky
Copy link
Contributor

I would love to change the default of touch to true for embedded associations, and I will write up a proposal for it in a ticket (MONGOID-5016?)... Assume that we will be changing the default to true, so that (2) will not change... Are there any backwards breaking changes in this PR? Anything that we would need to put in the release notes?

@johnnyshields
Copy link
Contributor Author

johnnyshields commented Oct 4, 2022

There are no backwards breaking changes. The release notes for the touch: true could have a line like:

In addition, there have been some refactor of the #touch method to generate fewer queries, and edge cases where touch: false was not working previously have been corrected.

Important note - touch: true would only be the new default for embedded_in relations (i.e. touch child causes touch on parent). Referenced relations need to stay as touch: false.

There's also some ticket to remove touch: false support on embedded_in, but I think that's a bridge too far for Mongoid 9.0 in terms of preserving behavior. Should revisit that later.

@Neilshweky
Copy link
Contributor

@johnnyshields take a look at the last commit I made here. Turns out in one of the tests that used a meth, the shared_examples wasn't using it. I changed that and fixed the test (there's a problem in MONGOID-5504 that I will have to fix)... I also explicitly specified the touch option on all models so that when we flip the default, the tests won't fail... I'll add new tests when I do MONGOID-5016

@johnnyshields
Copy link
Contributor Author

Ah yep, great catch! Last commit is good.

@Neilshweky
Copy link
Contributor

Neilshweky commented Oct 4, 2022

In the interest of keeping my head on straight, here are the TODOs and followup work:

  • write 9.0 release notes @johnnyshields is handling this (if necessary)
    • touch: false works for embedded associations
    • made more efficient
  • Followup: flip the default of the touch option on embedded associations to true MONGOID-5016
  • Followup: verify that the touch: :field functionality works correctly MONGOID-5508

@Neilshweky
Copy link
Contributor

There are no backwards breaking changes. The release notes for the touch: true could have a line like:

I think this might be a good idea but we can do this as part of MONGOID-5016

In addition, there have been some refactor of the #touch method to generate fewer queries, and edge cases where touch: false was not working previously have been corrected.

Important note - touch: true would only be the new default for embedded_in relations (i.e. touch child causes touch on parent). Referenced relations need to stay as touch: false.

This is MONGOID-5016 which I repurposed for switching the default

There's also some ticket to remove touch: false support on embedded_in, but I think that's a bridge too far for Mongoid 9.0 in terms of preserving behavior. Should revisit that later.

@Neilshweky
Copy link
Contributor

Neilshweky commented Oct 4, 2022

@johnnyshields even though there are no backwards breaking changes, the touch: false option has been fixed and I think that warrants a release note, and you can add the note about generating fewer queries with it.

docs/release-notes/mongoid-9.0.txt Outdated Show resolved Hide resolved
@johnnyshields
Copy link
Contributor Author

@Neilshweky your spec change adding touch: false seems to have broken a few specs. Suggest to revert it and fix it in the next PR.

@Neilshweky
Copy link
Contributor

I will fix it and merge when its done

@Neilshweky
Copy link
Contributor

@johnnyshields specs should be green now.

@Neilshweky Neilshweky merged commit 604a407 into mongodb:master Oct 4, 2022
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.

4 participants