0fbc4518b0
- 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
82 lines
2.8 KiB
Bash
82 lines
2.8 KiB
Bash
#!/bin/bash
|
||
# =============================================================
|
||
# momentry sample package (下架)
|
||
# 將 sample 影片與其 deep scan 資料從本機 momentry 系統移除
|
||
#
|
||
# 用法:
|
||
# bash uninstall_sample.sh
|
||
# =============================================================
|
||
set -euo pipefail
|
||
|
||
PKG="$(cd "$(dirname "$0")" && pwd)"
|
||
MOMENTRY_DIR="${MOMENTRY_DIR:-$HOME/momentry}"
|
||
DEMO_DIR="$MOMENTRY_DIR/var/sftpgo/data/demo"
|
||
OUT_DIR="$MOMENTRY_DIR/output"
|
||
DB="${MOMENTRY_DB:-momentry}"
|
||
|
||
if [ -x /opt/homebrew/bin/psql ]; then
|
||
PSQL=/opt/homebrew/bin/psql
|
||
elif [ -x "$HOME/opt_pgsql/bin/psql" ]; then
|
||
PSQL="$HOME/opt_pgsql/bin/psql"
|
||
else
|
||
PSQL=$(command -v psql || true)
|
||
[ -n "$PSQL" ] || { echo "!! 找不到 psql"; exit 1; }
|
||
fi
|
||
|
||
[ -f "$PKG/manifest.tsv" ] || { echo "!! 找不到 manifest.tsv"; exit 1; }
|
||
|
||
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 ' ') 支影片)==="
|
||
|
||
# 產生單一 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 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
|
||
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
|
||
rm -f "$OUT_DIR"/"$uuid".* 2>/dev/null || true
|
||
[ -d "$OUT_DIR/$uuid" ] && rm -rf "$OUT_DIR/$uuid" || true
|
||
done < "$PKG/manifest.tsv"
|
||
|
||
echo "[2/3] 刪除影片檔"
|
||
while IFS=$'\t' read -r uuid fname; do
|
||
[ -n "$fname" ] && rm -f "$DEMO_DIR/$fname" || true
|
||
done < "$PKG/manifest.tsv"
|
||
|
||
echo "[3/3] 完成"
|
||
"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -t -A -c "SELECT 'videos 殘留='||COUNT(*) FROM videos;"
|