#!/bin/bash # Cleanup duplicate tkg_nodes entries # Usage: ./scripts/cleanup_duplicate_tkg_nodes.sh [file_uuid] cleanup_duplicates() { local uuid=$1 echo "🔄 Cleaning up duplicate tkg_nodes for $uuid..." # Find and remove duplicate face_track nodes # Keep trace_N, remove face_track_N for same trace_id cat << 'SQL' | psql -U accusys -d momentry -h localhost 2>/dev/null || echo "Note: Run manually if psql not available" WITH duplicates AS ( SELECT tn1.id as delete_id FROM tkg_nodes tn1 JOIN tkg_nodes tn2 ON tn1.file_uuid = tn2.file_uuid AND tn1.node_type = tn2.node_type AND tn1.properties->>'trace_id' = tn2.properties->>'trace_id' AND tn1.id < tn2.id WHERE tn1.file_uuid = '$uuid' AND tn1.node_type = 'face_track' AND tn1.external_id LIKE 'face_track_%' AND tn2.external_id LIKE 'trace_%' ) DELETE FROM tkg_nodes WHERE id IN (SELECT delete_id FROM duplicates); -- Show remaining nodes SELECT external_id, label, properties->>'trace_id' as trace_id FROM tkg_nodes WHERE file_uuid = '$uuid' AND node_type = 'face_track' ORDER BY (properties->>'trace_id')::int; SQL echo "✅ Cleanup complete for $uuid" } if [ -n "$1" ]; then cleanup_duplicates "$1" else echo "Usage: $0 " echo "Example: $0 b496a68800f90770aa7246c877f38985" fi