Fixes #22251: Re-parent child ModuleBays when a Module is moved to a new bay#22336
Fixes #22251: Re-parent child ModuleBays when a Module is moved to a new bay#22336bctiemann wants to merge 5 commits into
Conversation
…new bay When Module.module_bay_id changes, re-save each child ModuleBay so that ModuleBay.save() can recalculate self.parent from module.module_bay. Without this, the MPTT parent pointers stayed anchored to the old bay even though Module.module_bay_id was updated, leaving children orphaned from the module's new location in the tree. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Documents that MPTT moves subtrees atomically when a direct child is re-parented, so grandchild bays also land in the correct tree after a top-level module is relocated. Preserves this invariant for future tree-backend changes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@claude /review and report only on areas of potential concern. |
|
Claude finished @bctiemann's task in 2m 10s —— View job Code ReviewThe fix is correct and the tests are solid. Two areas of potential concern: 1. Change-log noise and missing pre-change snapshots
More subtly, The straightforward fix is to call # modules.py:338-340
if old_module_bay_id is not None and old_module_bay_id != self.module_bay_id:
for child_bay in self.modulebays.all():
child_bay.snapshot() # ← record pre-change state for changelog
child_bay.save()Whether the changelog entries themselves are desirable at all is a separate design decision — if they're considered noise, a flag like 2. N+1 queries inside the loop
Adding for child_bay in self.modulebays.select_related('module__module_bay').all():Minor / non-blockingThe
|
ModuleBay.save() accesses self.module.module_bay; without prefetching
that is two extra queries per child bay on every module move. Adding
select_related('module__module_bay') to the queryset collapses this
to a single JOIN regardless of how many child bays the module has.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Without snapshot(), the changelog entry for each re-parented child bay has no pre-change state, so the diff shown in the UI is empty or incorrect. Calling snapshot() first records the current serialized state as _prechange_snapshot, giving the changelog an accurate before/after diff for the parent_id and tree_id changes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Both actionable items are addressed. |
Summary
Closes: #22251
When
Module.module_bay_idis updated (the module is moved to a different bay), the childModuleBayrecords belonging to that module were not having their MPTTparentpointers updated. They stayed anchored to the old bay in the tree even though the module had moved, producing the display anomaly reported in the issue.Root cause:
ModuleBay.save()derivesself.parentfromself.module.module_bay. On initial creation this runs automatically, but when only the module'smodule_bay_idchanges there is nothing to trigger a re-save of the child bays. Their storedparent_idgoes stale.Fix: In
Module.save(), snapshot the currentmodule_bay_idfrom the DB before saving. If it changed, iterate and re-save each childModuleBay. The existingModuleBay.save()logic then recalculatesparentfrom the now-updatedmodule.module_bayand MPTT moves each node to the correct position in the tree.Test plan
ModuleBayTestCase.test_moving_module_reparents_child_module_bayscreates a module with two child bays, moves the module to a different top-level bay, and asserts bothparent_idandtree_idon each child reflect the new location.ModuleBayTestCase(20 tests) passes.