Skip to content

MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6134

Merged
jamis merged 2 commits intomongodb:9.0-stablefrom
jamis:5751-autosave-improvements-9.0-stable
Apr 20, 2026
Merged

MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6134
jamis merged 2 commits intomongodb:9.0-stablefrom
jamis:5751-autosave-improvements-9.0-stable

Conversation

@jamis
Copy link
Copy Markdown
Contributor

@jamis jamis commented Apr 17, 2026

(Backport of #6133)

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 adds an option to change this behavior, ensuring that subtrees are only autosaved if there are any changed documents in the subtree.

Mongoid.autosave_saves_unchanged_documents = true

The default is true, allowing the legacy behavior to prevail. If your program depends on this legacy behavior, you are encouraged to rewrite the affected code and set the value to false. The default value of this option will be false in Mongoid 9.1, and will go away in Mongoid 10. At that point the legacy autosave behavior will be removed.

…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 jamis requested a review from a team as a code owner April 17, 2026 17:04
@jamis jamis requested review from comandeo-mongo and Copilot April 17, 2026 17:04
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

Adds a feature flag to avoid autosaving unchanged referenced-association subtrees (preventing unnecessary after_save callbacks), while preserving legacy behavior behind a configurable option and warning.

Changes:

  • Introduces Mongoid.autosave_saves_unchanged_documents configuration option plus a deprecation-style warning when legacy behavior is in effect.
  • Updates referenced autosave to skip saving unchanged documents unless they (or in-memory autosaved descendants) are new/changed/marked for destruction.
  • Adds a regression spec for MONGOID-5751 verifying after_save callbacks do not fire for pre-existing unchanged children when the flag is disabled.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
spec/mongoid/association/auto_save_spec.rb Adds regression coverage for autosave callback behavior across a parent/child/grandchild referenced tree.
lib/mongoid/warnings.rb Adds a one-time warning for the legacy “save unchanged autosave docs” behavior.
lib/mongoid/config.rb Adds the autosave_saves_unchanged_documents configuration option and documents its intended lifecycle.
lib/mongoid/association/referenced/auto_save.rb Implements recursive “changed in subtree” detection (cycle-guarded) and skips autosaving unchanged docs when configured.

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

Comment thread lib/mongoid/config.rb Outdated
Comment thread spec/mongoid/association/auto_save_spec.rb
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jamis jamis merged commit 933880f into mongodb:9.0-stable Apr 20, 2026
51 of 61 checks passed
@jamis jamis deleted the 5751-autosave-improvements-9.0-stable branch April 20, 2026 14:03
@jamis jamis added feature Adds a new feature, without breaking compatibility patch For use with the 'feature' label; indicates the PR should be treated as a patch for release purposes labels Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds a new feature, without breaking compatibility patch For use with the 'feature' label; indicates the PR should be treated as a patch for release purposes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants