Restore historic skeleton endpoint#1
Conversation
seankmartin
left a comment
There was a problem hiding this comment.
Overall looks good to me, thanks! Left two comments happy to chat
| }) | ||
|
|
||
|
|
||
| RESTORABLE_SKELETON_DELETE_LABEL = 'skeletons.remove' |
There was a problem hiding this comment.
Ideally I think it would be better if this is defined in one place and reused both here and in urls.py so that this label is kept in sync between the two.
If there is no good candidate place for that then feel free to leave it.
| skeleton_restore_lock_namespace = base_lock_id + 3 | ||
|
|
||
|
|
||
| def skeleton_restore_lock_id(skeleton_id): |
There was a problem hiding this comment.
I'll admit that I've not fully dug into the exact uses of these pg locks in catmaid, but I think there's a exceedingly rare problem that while is so unlikely it will pretty much never occur, I'd rather we explicitly avoid it. Technically our generated unsigned_lock_id could be the spatial update event lock or the history update event lock and it also makes it a bit hard in the future to add locks since the ID space is variable.
Looking at a different signature for the pg_advisory_xact_lock(key1 integer, key2 integer) we can use two keys which could possibly work here and seems intended for this kind of case. However the base_lock_id is too large in this case as key1. And we'd have to roll over on key2 or hash into 32 bit. But I think it might still work for this use case
Overview
This change adds an API endpoint that restores the latest deleted historic
version of a single skeleton:
UserRole.Annotate.class_instancetable.The existing skeleton deletion route is now wrapped with:
Older delete transactions may still be labeled
neurons.removeor may have no label; both are rejected by default. These older restore cases should remain a manual admin workflowFinding The Latest Deleted Skeleton
The lookup entry point is:
It searches
class_instance__historyfor historic rows where:idmatches the requested skeleton ID.project_idmatches the requested project.skeleton.sys_periodis closed, meaning the row was removed or replaced historically.The candidates are grouped by:
exec_transaction_idupper(sys_period)The newest candidate is selected by ordering
upper(sys_period)descending andtaking one row. This is the definition of "latest" for this endpoint.
After selecting the latest candidate, the same query checks how many deleted skeleton rows exist for that transaction in
class_instance__history. It only returns a row if that count is exactly one and that one skeleton is the requested skeleton.The query also joins
catmaid_transaction_infoon the transaction ID and execution time to retrieve the transaction label.The Restore View Flow
The view is:
The flow is:
Open
transaction.atomic()so all checks and inserts are one databasetransaction.
Take a transaction-scoped PostgreSQL advisory lock:
(The advisory lock blocks restore operations for the same skeleton id)
class_instancealready has the requested skeleton ID.Transactionobject from the candidate transaction ID and time.undelete_neuron()for the selected transaction.The response includes:
{ "skeleton_id": 1, "transaction_id": 123, "execution_time": "...", "source_label": "skeletons.remove", "success": "Restored skeleton 1 from history." }Tests
The added tests live in:
The restore-related coverage includes:
rows, and treenode edge rows are live again.
skeletons.removeandskeletons.restore.