wipe.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  4. # Activate repo virtualenv so python3 + deps are always found
  5. source "$SCRIPT_DIR/.venv/bin/activate"
  6. # Source .env (may override NEWS_MCP_DB_PATH)
  7. if [ -f "$SCRIPT_DIR/.env" ]; then
  8. while IFS='=' read -r key val; do
  9. # skip comments and blank lines
  10. [[ "$key" =~ ^#.*$ || -z "$key" ]] && continue
  11. # strip leading/trailing whitespace from key
  12. key="$(echo "$key" | xargs)"
  13. export "$key=$val"
  14. done < "$SCRIPT_DIR/.env"
  15. fi
  16. # Wipe everything except feed_state (which holds feed config)
  17. python3 - "$SCRIPT_DIR" <<'PYEOF'
  18. import os, sqlite3, sys
  19. from pathlib import Path
  20. script_dir = Path(sys.argv[1])
  21. db_path = Path(os.getenv("NEWS_MCP_DB_PATH", str(script_dir / "data" / "news.sqlite")))
  22. if not db_path.exists():
  23. print(f"DB not found: {db_path}", file=sys.stderr)
  24. sys.exit(1)
  25. TABLES_TO_CLEAR = [
  26. "cluster_entities",
  27. "cluster_keywords",
  28. "entity_metadata",
  29. "clusters",
  30. "meta",
  31. ]
  32. conn = sqlite3.connect(str(db_path))
  33. for t in TABLES_TO_CLEAR:
  34. conn.execute(f"DELETE FROM {t}")
  35. conn.commit()
  36. cur = conn.execute("SELECT COUNT(*) FROM feed_state")
  37. feeds = cur.fetchone()[0]
  38. conn.close()
  39. print(f"Wiped {', '.join(TABLES_TO_CLEAR)}. feed_state rows kept: {feeds}")
  40. PYEOF