Skip to content

Fix Entity.order and Entity.section updates during sync - #4374

Merged
eemeli merged 7 commits into
mozilla:mainfrom
eemeli:fix-sections
Aug 7, 2026
Merged

Fix Entity.order and Entity.section updates during sync#4374
eemeli merged 7 commits into
mozilla:mainfrom
eemeli:fix-sections

Conversation

@eemeli

@eemeli eemeli commented Aug 6, 2026

Copy link
Copy Markdown
Member

Fixes #2115
Prerequisite for #4212

We currently don't have data for the correct order & section-level comments, so those won't be fixes in all cases until the relevant resources are sync'd (at which point they'll be fixed using data from the source repo), so we may want to do a force-sync for all projects at some point.

The data migration took about 20 seconds on my machine.

@eemeli
eemeli requested a review from mathjazz August 6, 2026 09:46

@mathjazz mathjazz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice work.

The order fix works, but if I delete the reorder block only test_change_entities and test_fluent_group_comment_change fail — one shuffles strings, the other removes one. Neither covers the case in the issue title: a string added at the top, pushing existing ones down.

Comment thread pontoon/test/factories.py

class EntityFactory(DjangoModelFactory):
resource = SubFactory(ResourceFactory)
section = SubFactory(SectionFactory, resource=SelfAttribute("..resource"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: EntityFactory now always creates a Section, so it models something that doesn't occur in practice.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For repo projects, entities are created only during sync, and there entities are always created with a section reference (here and here).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, I missed that.

+ model_update(prev_ent, "string", next_ent.string)
+ model_update(prev_ent, "comment", next_ent.comment)
+ model_update(prev_ent, "meta", next_ent.meta)
+ model_update(prev_ent, "section", next_ent.section)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

model_update() reads prev_ent.section and lazily fetches the section once per entity, on every changed resource, even when nothing changed.

Please add select_related("section").

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done, including a .defer() for most of the Section fields. The equality check only actually looks at the primary key. I'd've thought that the request would be optimized out, but apparently it isn't, even though it's not needed.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice refinement. It's safe today, but any future prev_ent.section.comment reintroduces exactly the N+1 this commit removed. A short comment ("Only section PK is fetched, do not read its fields here") would be a cheap insurance.

Comment thread pontoon/sync/core/entities.py Outdated
@eemeli
eemeli requested a review from mathjazz August 7, 2026 07:44
@eemeli
eemeli merged commit d98e0ac into mozilla:main Aug 7, 2026
7 checks passed
@eemeli
eemeli deleted the fix-sections branch August 7, 2026 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Order is not updated for old entities when new entities are added to a resource

2 participants