feat(snippets): un-merge — reverse one source out of a merged survivor
CI & Build / Python lint (push) Successful in 3s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 36s
CI & Build / Python tests (push) Successful in 54s
CI & Build / Build & push image (push) Successful in 40s
CI & Build / Python lint (push) Successful in 3s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 36s
CI & Build / Python tests (push) Successful in 54s
CI & Build / Build & push image (push) Successful in 40s
Closes the last of milestone #232. The task said to settle the design before coding; here is what was settled and why. THE HAZARD. Restoring a merged-in source from the trash brought the record back but never stripped its locations off the survivor, so both claimed the same call sites and the reverse lookup read the duplicate claims as real. Subtracting blindly is not a fix: a location can arrive from a source AND genuinely be the survivor's own, and _normalize_locations dedups them into one, so blind subtraction would strip a call site the survivor owns. Same problem defeated partial un-merge — `merged_from` recorded ids, not which locations came from which source. THE ANSWER. Record per-source attribution AT MERGE TIME, where it is known exactly: each entry keeps only what that source ADDED, computed incrementally as sources fold in. Anything the survivor already had, or an earlier source already brought, is attributed to nobody. Both open questions fall out of that one change — partial un-merge is exact, and a survivor-owned location can never be stripped, because it was never attributed in the first place. The shape moved from [id] to [{id, locations, tags}]. Free to do: the corpus holds one snippet and zero merges, so there is no legacy data (rule #22). A bare int still normalizes to {"id": n} — not legacy tolerance, but because snippet_fields falls back to PARSING THE BODY when a row has no `data`, and the body's provenance line can only carry ids. Such an entry shows history and refuses un-merge with a reason rather than guessing. WHICH SURFACE. Neither option in the task, quite. Making trash-restore notice the merge would teach the generic trash path snippet semantics for one record type. Instead un-merge OWNS the restore: one operation, one authorization check, trash stays ignorant. Restoring by hand is still allowed and still leaves both records claiming the same places — so un-merge treats an already-alive source as the normal case and goes straight to the subtraction that repairs it. That is the state that motivated the feature, not an error. Adds trash.restore_entity(user_id, type, id) — the missing inverse of delete(), which returns a batch id callers don't keep. Restores the whole batch, since the batch is the entity plus its cascade. Refs #2165 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UaYUaouG9jjhATyuxCKrQs
This commit is contained in:
@@ -181,6 +181,36 @@ async def restore(user_id: int, batch_id: str) -> int:
|
||||
return n
|
||||
|
||||
|
||||
async def restore_entity(user_id: int, entity_type: str, entity_id: int) -> int | None:
|
||||
"""Restore ONE trashed entity by id, by reviving the batch that took it.
|
||||
|
||||
The inverse of `delete(user_id, entity_type, entity_id)`, which returns a
|
||||
batch id the caller usually doesn't keep. Callers that need to undo their own
|
||||
soft-delete later — snippet un-merge (#2165) — know the entity id, not the
|
||||
batch, and looking it up here keeps them from reaching into the column set.
|
||||
|
||||
Restores the whole batch on purpose, not just the row: the batch is the
|
||||
entity plus its cascaded descendants, so reviving the parent alone would
|
||||
leave them orphaned in the trash. That is the same thing the trash UI does.
|
||||
|
||||
Returns rows restored, or None if the entity isn't trashed / not owned.
|
||||
"""
|
||||
model = next((m for m, label in _TYPE.items() if label == entity_type), None)
|
||||
if model is None:
|
||||
return None
|
||||
async with async_session() as session:
|
||||
batch = (await session.execute(
|
||||
select(model.deleted_batch_id).where(
|
||||
model.id == entity_id,
|
||||
_owner_clause(model, user_id),
|
||||
model.deleted_at.isnot(None),
|
||||
)
|
||||
)).scalars().first()
|
||||
if not batch:
|
||||
return None
|
||||
return await restore(user_id, batch)
|
||||
|
||||
|
||||
async def purge(user_id: int, batch_id: str) -> int:
|
||||
"""Hard-delete every row in the batch. Irreversible."""
|
||||
from sqlalchemy import delete as sql_delete
|
||||
|
||||
Reference in New Issue
Block a user