Files
warren 0fbc4518b0 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
2026-11-08 22:25:45 +08:00

95 lines
3.9 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# =============================================================
# momentry sample package (上架)
# 將 100 支 deep scan 已完成的 sample 影片上架到本機 momentry 系統
# 可在任何 momentry 機器上使用(可攜)
#
# 用法:
# bash install_sample.sh # 上架(在 203 上)
# MOMENTRY_DIR=~momentry bash install_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}"
# psql 路徑探測(203=/opt/homebrew, 部分機器=~/opt_pgsql)
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
if [ ! -f "$PKG/db/sample_data.sql" ]; then
echo "!! 找不到 $PKG/db/sample_data.sql — 這不是完整的 sample package"
exit 1
fi
echo "=== 上架 sample data 到 $MOMENTRY_DIR ==="
# 先清理可能存在的舊 sample 資料(避免 COPY duplicate key)
echo "[0/5] 清理舊 sample 資料(若有)..."
CLEAN_SQL=$(mktemp /tmp/clean.XXXXXX.sql)
cat > "$CLEAN_SQL" <<'SQL'
DO $$
DECLARE tbl regclass;
BEGIN
FOR tbl IN SELECT to_regclass('public.'||t) FROM unnest(ARRAY['videos','processor_results','monitor_jobs','scenes','chunk','speaker_detections','trace_profiles','trace_operation_history','tkg_nodes','tkg_edges','tkg_operation_log','pipeline_progress']) t LOOP
IF tbl IS NOT NULL THEN
EXECUTE 'TRUNCATE TABLE ' || quote_ident(tbl::text) || ' CASCADE';
END IF;
END LOOP;
END $$;
SQL
"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -q -v ON_ERROR_STOP=1 -f "$CLEAN_SQL" 2>/dev/null && echo "(已清理)" || echo "(無舊資料)"
rm -f "$CLEAN_SQL"
echo "[1/5] 複製影片($(ls "$PKG"/videos/*.mp4 2>/dev/null | wc -l | tr -d ' ') 支)→ $DEMO_DIR"
mkdir -p "$DEMO_DIR"
cp -f "$PKG"/videos/*.mp4 "$DEMO_DIR"/
echo "[2/5] 複製 deep scan 輸出 JSON → $OUT_DIR"
mkdir -p "$OUT_DIR"
cp -a "$PKG"/output/. "$OUT_DIR"/
echo "[3/5] 匯入 DB sample data → $DB"
"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -v ON_ERROR_STOP=1 -f "$PKG/db/sample_data.sql"
echo "[4/5] 校正 id sequences"
"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -q <<'SQL'
SELECT format(
'SELECT CASE WHEN %L IS NOT NULL THEN setval(%L, COALESCE((SELECT max(id) FROM %I), 1), false) END;',
pg_get_serial_sequence(c.oid::regclass::text, 'id'),
pg_get_serial_sequence(c.oid::regclass::text, 'id'),
c.oid::regclass::text
)
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relname IN ('videos','processor_results','monitor_jobs','scenes','chunk',
'speaker_detections','trace_profiles','trace_operation_history',
'tkg_nodes','tkg_edges','tkg_operation_log','pipeline_progress')
AND EXISTS (SELECT 1 FROM information_schema.columns ic
WHERE ic.table_schema='public' AND ic.table_name=c.relname AND ic.column_name='id')
\gexec
SQL
echo ""
echo "=== 上架完成。摘要 ==="
"$PSQL" -U "${MOMENTRY_PGUSER:-accusys}" -d "$DB" -t -A -c "
SELECT 'videos='||COUNT(*) FROM videos
UNION ALL SELECT 'processor_results='||COUNT(*) FROM processor_results
UNION ALL SELECT 'monitor_jobs='||COUNT(*) FROM monitor_jobs
UNION ALL SELECT 'scenes='||COUNT(*) FROM scenes
UNION ALL SELECT 'chunk='||COUNT(*) FROM chunk
UNION ALL SELECT 'speaker_detections='||COUNT(*) FROM speaker_detections
UNION ALL SELECT 'trace_profiles='||COUNT(*) FROM trace_profiles
UNION ALL SELECT 'tkg_nodes='||COUNT(*) FROM tkg_nodes;"
echo "影片檔案: $(ls "$DEMO_DIR"/*.mp4 2>/dev/null | wc -l | tr -d ' ') 支 | output JSON: $(find "$OUT_DIR" -maxdepth 1 -type f | wc -l | tr -d ' ') 個"