Skip to content

MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6133

Merged
jamis merged 5 commits intomongodb:masterfrom
jamis:5751-autosave-improvements
Apr 17, 2026
Merged

MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6133
jamis merged 5 commits intomongodb:masterfrom
jamis:5751-autosave-improvements

Conversation

@jamis
Copy link
Copy Markdown
Contributor

@jamis jamis commented Apr 16, 2026

This PR fixes a bug with autosave: true associations, that caused after_save callbacks to be invoked on children even if they hadn't changed. It recursively checks the entire subtree and skips saving it if nothing in it has changed.

Summary

The legacy behavior of associations with autosave: true resulted in all #save being invoked on all children of those associations, whether those children actually needed it or not. All corresponding after_save hooks were invoked as well, recursively, clear to the bottom of the autosave tree.

This PR changes this behavior, ensuring that subtrees are only autosaved if there are any changed documents in the subtree. As there may be users that depend on the legacy behavior (expecting after_save hooks to be invoked even if the record has not changed, for instance), a feature flag has been added to allow the legacy behavior to be opted into.

Mongoid.autosave_saves_unchanged_documents = true

When true, the legacy behavior prevails. If your program depends on this legacy behavior, you are encouraged to rewrite the affected code, because this flag will go away in Mongoid 10, and the legacy autosave behavior will be removed.

Copilot AI review requested due to automatic review settings April 16, 2026 20:14
@jamis jamis requested a review from a team as a code owner April 16, 2026 20:14
@jamis jamis requested a review from comandeo-mongo April 16, 2026 20:14
@jamis jamis added the bug Fixes a bug, with no new features or broken compatibility label Apr 16, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses MONGOID-5751 by preventing autosave: true referenced associations from saving (and thus running after_save callbacks on) already-persisted, unchanged documents when a parent save cascades across the association graph.

Changes:

  • Add a regression spec that asserts after_save is only invoked for the parent and newly-added descendants, not for pre-existing unchanged children.
  • Update referenced autosave to skip saving unchanged documents while still recursively saving descendants when needed (via a recursive changed_for_autosave? check).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
spec/mongoid/association/auto_save_spec.rb Adds a regression test reproducing the unnecessary autosave/callback behavior for unchanged subtrees.
lib/mongoid/association/referenced/auto_save.rb Adds recursive “needs autosave?” detection and skips saving unchanged referenced documents during autosave traversal.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/mongoid/association/referenced/auto_save.rb
@jamis jamis merged commit a141ee0 into mongodb:master Apr 17, 2026
76 checks passed
@jamis jamis deleted the 5751-autosave-improvements branch April 17, 2026 16:38
jamis added a commit to jamis/mongoid that referenced this pull request Apr 17, 2026
…db#6133)

* MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees

* avoid loading HasMany proxies into memory, just to check if they're dirty

* account for belongs_to sometimes storing a literal object instead of a proxy

* Add Mongoid.autosave_saves_unchanged_documents feature flag to preserve legacy behavior
jamis added a commit that referenced this pull request Apr 20, 2026
* MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees (#6133)

* MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees

* avoid loading HasMany proxies into memory, just to check if they're dirty

* account for belongs_to sometimes storing a literal object instead of a proxy

* Add Mongoid.autosave_saves_unchanged_documents feature flag to preserve legacy behavior

* Update lib/mongoid/config.rb

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes a bug, with no new features or broken compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants