← back to Restaurant Directory

scripts/sync-enrichment-to-kamatera.sh

41 lines

#!/usr/bin/env bash
#
# Sync facility_enrichment from local Mac Studio PG → Kamatera PG.
# Runs every 4 hours via launchd while the cuisine enrichment job is filling
# in 37,310 records. Once enrichment finishes, becomes a daily/weekly no-op.
#
# Idempotent: TRUNCATEs the Kamatera table and re-loads the full local snapshot.
# At ~37K rows × ~80 bytes ≈ 3 MB, this is cheap.
set -euo pipefail

cd /Users/macstudio3/Projects/restaurant-directory
mkdir -p logs
LOG="logs/cuisine-sync-$(date +%Y%m%d).log"

# Skip if the cuisine enrichment job is actively writing — pg_dump + a heavy
# UPSERT loop can race and abort the writer with "terminating connection due
# to administrator command" (lost data 2026-05-02 night). Better to skip a
# sync window than to nuke an in-flight 37K-record run.
if pgrep -f 'enrich-cuisine.ts' > /dev/null; then
  echo "[sync] $(date -Iseconds) SKIP — enrich-cuisine.ts still running" >> "$LOG"
  exit 0
fi

{
  echo "[sync] $(date -Iseconds) starting"
  LOCAL_N=$(psql -At -d restaurant_professional_directory -c "SELECT COUNT(*) FROM facility_enrichment WHERE enriched_at IS NOT NULL")
  echo "[sync] local enriched: $LOCAL_N"

  pg_dump -d restaurant_professional_directory --data-only --no-owner --disable-triggers \
    -t facility_enrichment 2>&1 \
    | ssh root@45.61.58.125 "sudo -u postgres psql -d restaurant_professional_directory -v ON_ERROR_STOP=1 -q -c 'TRUNCATE facility_enrichment CASCADE;' && sudo -u postgres psql -d restaurant_professional_directory -v ON_ERROR_STOP=1 -q"

  REMOTE_N=$(ssh root@45.61.58.125 "sudo -u postgres psql -At -d restaurant_professional_directory -c 'SELECT COUNT(*) FROM facility_enrichment;'")
  echo "[sync] kamatera enriched: $REMOTE_N"

  if [ "$LOCAL_N" != "$REMOTE_N" ]; then
    echo "[sync] WARN: local/remote count mismatch ($LOCAL_N vs $REMOTE_N)"
  fi
  echo "[sync] $(date -Iseconds) done"
} >> "$LOG" 2>&1