fix: resolve merge silent failure by fixing upsert_trace_profile SQL and backfilling data

- Fixed upsert_trace_profile() SQL: changed trace_profiles.xxx to tp.xxx in ON CONFLICT DO UPDATE
  to fix invalid reference to FROM-clause entry error
- Fixed cluster_agent.rs error handling: replaced 'let _ =' with proper error logging
- Created migration script to backfill trace_profiles from tkg_nodes for existing files
- Migration successfully inserted 98 missing trace_profiles records
- Verified merge now works correctly: traces_merged > 0 instead of silent no-op

This fixes both the rename 500 error and the merge silent failure, as they share
the same root cause in upsert_trace_profile() SQL.
This commit is contained in:
Accusys
2026-08-10 01:42:34 +08:00
parent 65da4b451a
commit aa28bf93fb
3 changed files with 70 additions and 10 deletions
+45
View File
@@ -0,0 +1,45 @@
-- Migration: Backfill trace_profiles from tkg_nodes
-- This script populates trace_profiles table for existing files that have
-- tkg_nodes face_track nodes but no corresponding trace_profiles records.
-- This is needed because upsert_trace_profile() had a SQL bug that was
-- silently failing, leaving trace_profiles empty for many files.
-- Step 1: Insert missing trace_profiles records
INSERT INTO trace_profiles (file_uuid, trace_id, name, status, created_at, updated_at)
SELECT
tn.file_uuid,
(tn.properties->>'trace_id')::int as trace_id,
COALESCE(tn.label, CONCAT('Person_', (tn.properties->>'trace_id')::int)) as name,
'pending' as status,
NOW() as created_at,
NOW() as updated_at
FROM tkg_nodes tn
WHERE tn.node_type = 'face_track'
AND tn.properties ? 'trace_id'
AND (tn.file_uuid, (tn.properties->>'trace_id')::int) NOT IN (
SELECT file_uuid, trace_id FROM trace_profiles
)
ON CONFLICT (file_uuid, trace_id) DO UPDATE SET
name = EXCLUDED.name,
updated_at = NOW();
-- Step 2: Verify migration
DO $$
DECLARE
v_count BIGINT;
v_tkg_count BIGINT;
BEGIN
SELECT COUNT(*) INTO v_count FROM trace_profiles;
SELECT COUNT(*) INTO v_tkg_count FROM tkg_nodes WHERE node_type = 'face_track' AND properties ? 'trace_id';
RAISE NOTICE 'Migration complete.';
RAISE NOTICE ' trace_profiles count: %', v_count;
RAISE NOTICE ' tkg_nodes face_track count: %', v_tkg_count;
IF v_count < v_tkg_count THEN
RAISE WARNING 'Some trace_profiles records may still be missing. This could be due to tkg_nodes without trace_id in properties.';
END IF;
END $$;
-- Step 3: Analyze table for query optimization
ANALYZE trace_profiles;
+19 -4
View File
@@ -259,21 +259,36 @@ pub async fn trigger_cluster_agent(
}; };
if let Some(all_trace_ids) = trace_list { if let Some(all_trace_ids) = trace_list {
let mut success_count = 0;
let mut error_count = 0;
for tid in &all_trace_ids { for tid in &all_trace_ids {
let name = format!("Person_{}", tid); let name = format!("Person_{}", tid);
let _ = schema::upsert_trace_profile( match schema::upsert_trace_profile(
state.db.pool(), state.db.pool(),
&file_uuid, &file_uuid,
*tid as i32, *tid as i32,
&name, &name,
None, None,
) )
.await; .await
{
Ok(_) => success_count += 1,
Err(e) => {
error_count += 1;
tracing::error!(
"[ClusterAgent] Failed to upsert trace profile for trace {}: {}",
tid,
e
);
}
}
} }
tracing::info!( tracing::info!(
"[ClusterAgent] Reset {} trace names to default for {}", "[ClusterAgent] Reset {} trace names to default for {} (success: {}, errors: {})",
all_trace_ids.len(), all_trace_ids.len(),
file_uuid file_uuid,
success_count,
error_count
); );
} }
} }
+6 -6
View File
@@ -58,12 +58,12 @@ pub async fn upsert_trace_profile(
LEFT JOIN {tp_table} tp ON tp.file_uuid = $1 AND tp.trace_id = $2 \ LEFT JOIN {tp_table} tp ON tp.file_uuid = $1 AND tp.trace_id = $2 \
ON CONFLICT (file_uuid, trace_id) \ ON CONFLICT (file_uuid, trace_id) \
DO UPDATE SET name = EXCLUDED.name{update_props}, \ DO UPDATE SET name = EXCLUDED.name{update_props}, \
start_frame = COALESCE(EXCLUDED.start_frame, trace_profiles.start_frame), \ start_frame = COALESCE(EXCLUDED.start_frame, tp.start_frame), \
end_frame = COALESCE(EXCLUDED.end_frame, trace_profiles.end_frame), \ end_frame = COALESCE(EXCLUDED.end_frame, tp.end_frame), \
frame_count = COALESCE(EXCLUDED.frame_count, trace_profiles.frame_count), \ frame_count = COALESCE(EXCLUDED.frame_count, tp.frame_count), \
key_frame = COALESCE(EXCLUDED.key_frame, trace_profiles.key_frame), \ key_frame = COALESCE(EXCLUDED.key_frame, tp.key_frame), \
key_face = COALESCE(EXCLUDED.key_face, trace_profiles.key_face), \ key_face = COALESCE(EXCLUDED.key_face, tp.key_face), \
avg_confidence = COALESCE(EXCLUDED.avg_confidence, trace_profiles.avg_confidence), \ avg_confidence = COALESCE(EXCLUDED.avg_confidence, tp.avg_confidence), \
updated_at = NOW()", updated_at = NOW()",
); );