#!/usr/bin/env bash set -euo pipefail # # wipe-slate.sh — clear ALL ticket + audit records from the tables the scanner # app currently uses, for a clean event run-through. Leaves the table SCHEMAS # intact and does NOT touch donor data. Reads NocoDB creds from backend/.env. # # Usage: # scripts/wipe-slate.sh # prompts for confirmation # scripts/wipe-slate.sh --yes # skip the prompt (for automation) # SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ENV_FILE="${ENV_FILE:-$SCRIPT_DIR/../backend/.env}" get() { grep -E "^$1=" "$ENV_FILE" | head -1 | cut -d= -f2-; } BASE_URL="$(get NOCODB_BASE_URL)" TOKEN="$(get NOCODB_API_TOKEN)" TICKETS="$(get NOCODB_TABLE_ID)" AUDIT="$(get NOCODB_AUDIT_TABLE_ID)" [ -n "$BASE_URL" ] && [ -n "$TOKEN" ] && [ -n "$TICKETS" ] || { echo "Missing NocoDB config in $ENV_FILE" >&2; exit 1; } YES=0 case "${1:-}" in -y|--yes) YES=1;; esac count() { curl -s -H "xc-token: $TOKEN" "$BASE_URL/api/v2/tables/$1/records?limit=1" \ | python3 -c 'import sys,json;print(json.load(sys.stdin).get("pageInfo",{}).get("totalRows",0))' } echo "Target: $BASE_URL" echo " tickets ($TICKETS): $(count "$TICKETS") records" [ -n "$AUDIT" ] && echo " audit ($AUDIT): $(count "$AUDIT") records" if [ "$YES" -ne 1 ]; then read -rp "Delete ALL of the above? This cannot be undone. [y/N] " ans case "$ans" in y|Y|yes|YES) ;; *) echo "aborted"; exit 1;; esac fi wipe() { local label="$1" table="$2" total=0 ids n while :; do ids="$(curl -s -H "xc-token: $TOKEN" "$BASE_URL/api/v2/tables/$table/records?limit=1000&fields=Id" \ | python3 -c 'import sys,json;print(json.dumps([{"Id":r["Id"]} for r in json.load(sys.stdin)["list"]]))')" n="$(printf '%s' "$ids" | python3 -c 'import sys,json;print(len(json.load(sys.stdin)))')" [ "$n" -eq 0 ] && break curl -s -o /dev/null -X DELETE -H "xc-token: $TOKEN" -H "Content-Type: application/json" \ "$BASE_URL/api/v2/tables/$table/records" --data "$ids" total=$((total + n)) done echo " $label: deleted $total" } wipe "tickets" "$TICKETS" [ -n "$AUDIT" ] && wipe "audit" "$AUDIT" echo "Done — slate is clean."