fix: group delete semantics - unassign instead of destroy, self-heal tkg_nodes

Studio's "delete group" was calling hard_delete which physically removed
_faces points, tkg_nodes and trace_profiles (and silently failed to back
up to a non-existent _faces_trash collection, making undo impossible).
This permanently lost face groups and broke face-groups/cluster-results
(which are based on tkg_nodes) for any trace without a node row.

Changes:
- delete_trace hard-delete now means "restore to unassigned": keep
  _faces/tkg_nodes/trace_profiles, only reset name to "Face Trace {id}".
  Data is never destroyed so groups can be rebuilt and undo works.
- schema::ensure_face_track_node: create the face_track tkg_nodes row on
  write so a trace that lost its node (old hard-delete) reappears in
  face-groups/cluster-results (self-healing). Called from
  update_trace_profile_handler, update_trace_profile_group_handler and
  merge_groups_handler; the group handler also syncs tkg_nodes.label.
- restore_trace / undo_hard_delete_trace: when _faces_trash is empty
  (new semantics), rebuild the named group from the operation snapshot
  (tkg_nodes ON CONFLICT UPDATE + trace_profiles UPSERT) instead of 404.

Verified live on port 3002 (file bfba056f...): delete -> unassigned with
_faces preserved (4 points), rebuild group -> self-healing creates node
and group shows up, restore -> name restored to original. Also fixed the
pre-existing missing tkg_nodes row for trace 4621 via the same path.
This commit is contained in:
Accusys
2026-07-31 13:14:23 +08:00
parent d36132d93a
commit 40fc8c6a53
3 changed files with 439 additions and 187 deletions
+44 -4
View File
@@ -62,10 +62,7 @@ pub async fn upsert_trace_profile(
props_sub = tkg_sub("tn.properties"),
);
let mut query = sqlx::query(&sql)
.bind(file_uuid)
.bind(trace_id)
.bind(name);
let mut query = sqlx::query(&sql).bind(file_uuid).bind(trace_id).bind(name);
match properties {
Some(p) => {
let json = serde_json::to_string(p).unwrap_or_else(|_| "{}".to_string());
@@ -79,3 +76,46 @@ pub async fn upsert_trace_profile(
let res = query.execute(pool).await?;
Ok(res.rows_affected())
}
/// Ensure a face_track TKG node exists for the given trace. Self-healing:
/// when a group is (re)named but its tkg_nodes row was previously removed,
/// re-insert a default node so face-groups/cluster-results queries (which
/// read FROM tkg_nodes) keep working. Keeps an existing node untouched.
pub async fn ensure_face_track_node(
pool: &PgPool,
file_uuid: &str,
trace_id: i32,
name: &str,
) -> anyhow::Result<u64> {
let tkg_table = table_name("tkg_nodes");
let ext_id_face = format!("face_track_{}", trace_id);
let ext_id_trace = format!("trace_{}", trace_id);
let props = serde_json::json!({ "trace_id": trace_id });
let existing: Option<(i64,)> = sqlx::query_as(&format!(
"SELECT id FROM {tkg_table} WHERE file_uuid = $1 AND node_type = 'face_track' \
AND (external_id = $2 OR external_id = $3) LIMIT 1"
))
.bind(file_uuid)
.bind(&ext_id_face)
.bind(&ext_id_trace)
.fetch_optional(pool)
.await?;
if existing.is_some() {
return Ok(0);
}
let sql = format!(
"INSERT INTO {tkg_table} (file_uuid, node_type, external_id, label, properties) \
VALUES ($1, 'face_track', $2, $3, $4)"
);
let res = sqlx::query(&sql)
.bind(file_uuid)
.bind(&ext_id_trace)
.bind(name)
.bind(&props)
.execute(pool)
.await?;
Ok(res.rows_affected())
}