Dual-write samples to Mongo and Postgres#3610
Merged
igboyes merged 3 commits intoJul 3, 2026
Merged
Conversation
Convert the sample data-layer mutations (create, delete, finalize, update) to write a consistent legacy_samples row plus label and subtraction join rows inside the same transaction as the Mongo write. Move the rights update out of the API handler into a dual-writing update_rights method and add get_owner_id, replacing the get_sample_owner helper. The rights endpoint now returns the full Sample it already declared, instead of a partial rights projection.
Convert LabelsData.delete() to both_transactions so the Mongo $pull, the legacy_sample_labels join-row deletion, and the label deletion commit atomically. Join rows are removed before the label to respect the un-cascaded foreign key.
Deleting a subtraction now removes the matching legacy_sample_subtractions rows in the same transaction as the Mongo $pull that unlinks it from samples' default subtractions. The logic is inlined into SubtractionsData.delete(), which already holds both sessions, replacing the single-caller unlink_default_subtractions helper.
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In
update_rights, the new logic treats any non-Nonegroupvalue as a real group (including strings like'none'), which will now raise aResourceConflictErrorinstead of clearing the group as before; if clearing via'none'or similar is still a supported API contract, consider explicitly handling that sentinel before the DB lookup. - The join-row cleanup logic for labels and subtractions (eg, in
SamplesData.update,LabelsData.delete, andSubtractionsData.delete) is now spread across several places with slightly different behaviors; consider extracting a shared helper or documenting the intended invariants to reduce the risk of subtle divergence over time.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In `update_rights`, the new logic treats any non-`None` `group` value as a real group (including strings like `'none'`), which will now raise a `ResourceConflictError` instead of clearing the group as before; if clearing via `'none'` or similar is still a supported API contract, consider explicitly handling that sentinel before the DB lookup.
- The join-row cleanup logic for labels and subtractions (eg, in `SamplesData.update`, `LabelsData.delete`, and `SubtractionsData.delete`) is now spread across several places with slightly different behaviors; consider extracting a shared helper or documenting the intended invariants to reduce the risk of subtle divergence over time.
## Individual Comments
### Comment 1
<location path="virtool/samples/api.py" line_range="330-339" />
<code_context>
):
raise APIInsufficientRights("Must be administrator or sample owner")
- group = data.get("group")
-
- if group is not None and group != "none":
- async with AsyncSession(pg) as session:
- result = await session.execute(
- select(SQLGroup.id).where(
- (SQLGroup.id == group)
- if isinstance(group, int)
- else (SQLGroup.legacy_id == group),
- ),
- )
-
- if not result.scalars().one_or_none():
- raise APIBadRequest("Group does not exist")
-
</code_context>
<issue_to_address>
**issue:** The previous special casing of `group == "none"` has been removed; this may change semantics for clients using that sentinel.
Previously, `group` was only resolved when `group is not None and group != "none"`, allowing clients to use the literal "none" to mean "no group" without a lookup. The new `update_rights` path treats any non-`None` `group` as an id and will try to resolve `legacy_id == "none"`, leading to `ResourceConflictError`/`APIBadRequest` for existing clients that still send "none".
If "none" is a supported client convention, consider preserving it by normalizing `group` before calling `update_rights` (e.g., mapping "none" to `None`) or explicitly handling this sentinel inside `update_rights`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Member
Author
|
Re: sharing a helper for the label/subtraction join-row cleanup across |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
create,delete,finalize,update) now writes a consistentlegacy_samplesrow plus label and subtraction join rows in the same transaction as the Mongo write. Mongo remains the read authority; no read paths change.update_rightsdata-layer method (with a newget_owner_id, replacingget_sample_owner). The endpoint now returns the fullSampleit already declared instead of a partial rights projection.legacy_sample_labels/legacy_sample_subtractionsatomically with their Mongo$pull.