fix: sample install/uninstall robustness
- install: TRUNCATE sample tables before COPY to avoid duplicate key on re-install - uninstall: use single SQL session with temp table for reliable DELETE (previous version: multiple psql sessions lost temp table between calls) - uninstall: monitor_jobs deleted via video_id JOIN videos (source schema lacked file_uuid column, so file_uuid was NULL in live DB) - mktemp: macOS compatible template (/tmp/name.XXXXXX.sql) - Full cycle verified: install → uninstall → install → uninstall → install
This commit is contained in:
@@ -25,21 +25,46 @@ fi
|
||||
|
||||
[ -f "$PKG/manifest.tsv" ] || { echo "!! 找不到 manifest.tsv"; exit 1; }
|
||||
|
||||
UUIDS=$(cut -f1 "$PKG/manifest.tsv" | tr '\n' ',' | sed 's/,$//')
|
||||
FNAME_TABLES="processor_results monitor_jobs scenes chunk speaker_detections trace_profiles trace_operation_history tkg_nodes tkg_edges tkg_operation_log pipeline_progress"
|
||||
|
||||
echo "=== 下架 sample data($(wc -l < "$PKG/manifest.tsv" | tr -d ' ') 支影片)==="
|
||||
for t in $FNAME_TABLES videos; do
|
||||
"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -q -v ON_ERROR_STOP=1 <<SQL || echo "(略過 $t)"
|
||||
DO \$\$
|
||||
DECLARE t regclass := to_regclass('public.$t');
|
||||
|
||||
# 產生單一 SQL 檔(包含 temp table + 所有刪除,在**同一 session** 執行)
|
||||
SQL_TMP=$(mktemp /tmp/uninstall.XXXXXX.sql)
|
||||
{
|
||||
echo "BEGIN;"
|
||||
echo "CREATE TEMP TABLE _sample_uuids(uuid text PRIMARY KEY);"
|
||||
cut -f1 "$PKG/manifest.tsv" | while read -r uuid; do
|
||||
echo "INSERT INTO _sample_uuids VALUES ('$uuid');"
|
||||
done
|
||||
# monitor_jobs 的 file_uuid 為空(舊版 schema 無此欄),需透過 video_id JOIN videos 刪除
|
||||
# 必須在 videos 刪除之前執行
|
||||
cat <<'SQL'
|
||||
DO $$
|
||||
DECLARE tbl regclass := to_regclass('public.monitor_jobs');
|
||||
BEGIN
|
||||
IF t IS NOT NULL THEN
|
||||
EXECUTE format('DELETE FROM %I WHERE file_uuid IN (%s);', '$t', '$UUIDS');
|
||||
IF tbl IS NOT NULL THEN
|
||||
EXECUTE 'DELETE FROM monitor_jobs WHERE video_id IN (SELECT id FROM videos WHERE file_uuid IN (SELECT uuid FROM _sample_uuids))';
|
||||
END IF;
|
||||
END $$;
|
||||
SQL
|
||||
for t in videos $FNAME_TABLES; do
|
||||
[ "$t" = "monitor_jobs" ] && continue # 已在上一步處理
|
||||
cat <<SQL
|
||||
DO \$\$
|
||||
DECLARE tbl regclass := to_regclass('public.$t');
|
||||
BEGIN
|
||||
IF tbl IS NOT NULL THEN
|
||||
EXECUTE 'DELETE FROM ' || quote_ident(tbl::text) || ' WHERE file_uuid IN (SELECT uuid FROM _sample_uuids)';
|
||||
END IF;
|
||||
END \$\$;
|
||||
SQL
|
||||
done
|
||||
done
|
||||
echo "COMMIT;"
|
||||
} > "$SQL_TMP"
|
||||
|
||||
"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -q -v ON_ERROR_STOP=1 -f "$SQL_TMP" 2>&1 && echo "✅ DB 資料已清除" || echo "⚠️ DB 清除有警告"
|
||||
rm -f "$SQL_TMP"
|
||||
|
||||
echo "[1/3] 刪除 output JSON"
|
||||
while IFS=$'\t' read -r uuid fname; do
|
||||
|
||||
Reference in New Issue
Block a user