Files
loki/scripts/db/sync-user-data.sh
T
Mao NakamotoandClaude Opus 4.8 c6bc058cb4 chore: purge Vercel / Neon / managed-Supabase across the codebase
Self-hosted on Hetzner is the only reality now; the leftover references were
stale at best and a security hole at worst. Thorough sweep across code, config,
scripts, docs, and comments (84+ files). Self-hosted supabase.orangecat.ch is
kept; historical migration records stay accurate.

Functional / security fixes:
- cron-auth: prod detection was process.env.VERCEL (unset on the box!), so the
  fail-closed 'CRON_SECRET unset -> 503' gate never fired in production. Now
  keys on NODE_ENV=production. (security)
- auth trustHost: dropped the dead process.env.VERCEL===1 branch.
- AppFooter: read the baked-in build SHA instead of VERCEL_GIT_COMMIT_SHA/ENV.
- project bootstrap: removed 'neon projects create' + the Vercel deploy brief;
  DB option is now self-hosted Postgres, deploy guidance is the self-hosted flow.
- db-url: removed the legacy NEON_DATABASE_URL* fallbacks (+ updated its test).

Infra / removals:
- deleted .github/workflows/vercel-deploy.yml and scripts/db/dump-from-{neon,supabase}.sh.
- stripped NEON_* env fallbacks from db dump/restore/sync scripts.

User-facing:
- desktop offline overlay no longer blames a 'Vercel hiccup'; privacy page +
  PRIVACY_POLICY subprocessors now say Hetzner self-hosted / self-hosted Postgres.

Plus ~50 comment/doc rewrites (Vercel -> the cloud host/the box; Neon -> Postgres).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 10:46:43 +02:00

152 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copy user-owned rows from one Postgres database to another, rewriting user_id.
# Vendor-neutral — works for local→remote, box→box, etc.
#
# Usage:
# SOURCE_DATABASE_URL=... TARGET_DATABASE_URL=... TARGET_USER_ID=<uuid> \
# ./scripts/db/sync-user-data.sh [--dry-run] [--force]
#
# Env vars:
# SOURCE_DATABASE_URL Source (defaults to local cockpit Postgres)
# TARGET_DATABASE_URL Target direct URL. REQUIRED.
# SOURCE_USER_ID Source user_id to rewrite (default: dev seed UUID)
# TARGET_USER_ID Target user_id in destination. REQUIRED.
#
# Legacy aliases (still accepted): LOCAL_DATABASE_URL, LOCAL_USER_ID
#
# Prerequisites: psql + pg_dump in PATH.
set -euo pipefail
SOURCE_URL="${SOURCE_DATABASE_URL:-${LOCAL_DATABASE_URL:-postgresql://cockpit:cockpit_local@127.0.0.1:5432/cockpit}}"
TARGET_URL="${TARGET_DATABASE_URL:-}"
SOURCE_USER_ID="${SOURCE_USER_ID:-${LOCAL_USER_ID:-00000000-0000-0000-0000-000000000001}}"
TARGET_USER_ID="${TARGET_USER_ID:-}"
if [ -z "$TARGET_URL" ]; then
echo "ERROR: set TARGET_DATABASE_URL before running." >&2
exit 2
fi
if [ -z "$TARGET_USER_ID" ]; then
echo "ERROR: set TARGET_USER_ID to the destination user's UUID." >&2
echo "Find it via: psql \"\$TARGET_DATABASE_URL\" -c \"SELECT id, email FROM users\"" >&2
exit 2
fi
DRY_RUN=false
FORCE=false
for arg in "$@"; do
[[ "$arg" == "--dry-run" ]] && DRY_RUN=true
[[ "$arg" == "--force" ]] && FORCE=true
done
echo "=== Postgres user-data sync ==="
echo " Source user_id : $SOURCE_USER_ID"
echo " Target user_id : $TARGET_USER_ID"
$DRY_RUN && echo " MODE: DRY RUN (no writes to target)"
echo ""
TABLES=(
orgs
org_memberships
entities
attributes
entity_relations
interactions
alerts
user_projects
goals
subscriptions
commitments
events
actions
habits
habit_completions
orchestration_runs
orchestration_events
project_states
prompt_history
pending_commands
)
echo "Verifying target connection..."
TARGET_USER_COUNT=$(psql "$TARGET_URL" -t -c "SELECT count(*) FROM users;" 2>/dev/null | tr -d ' \n')
[[ -z "$TARGET_USER_COUNT" || "$TARGET_USER_COUNT" == "0" ]] && { echo "ERROR: target users table empty or unreachable."; exit 1; }
echo " Target users: $TARGET_USER_COUNT ✓"
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
IMPORTED=0
SKIPPED=0
for t in "${TABLES[@]}"; do
SOURCE_COUNT=$(psql "$SOURCE_URL" -t -c "SELECT count(*) FROM $t;" 2>/dev/null | tr -d ' \n')
TARGET_COUNT=$(psql "$TARGET_URL" -t -c "SELECT count(*) FROM $t;" 2>/dev/null | tr -d ' \n')
if [[ "$SOURCE_COUNT" == "0" ]]; then
echo " $t: 0 rows — skip"
SKIPPED=$((SKIPPED + 1))
continue
fi
if [[ "$TARGET_COUNT" != "0" && "$FORCE" == "false" ]]; then
echo " $t: already has $TARGET_COUNT rows — skip (--force to overwrite)"
SKIPPED=$((SKIPPED + 1))
continue
fi
echo -n " $t: $SOURCE_COUNT rows — "
COPY_FILE="$TMPDIR/${t}.copy"
pg_dump "$SOURCE_URL" \
--table="$t" \
--data-only \
--no-comments \
--no-owner \
--no-privileges \
2>/dev/null \
| awk '/^COPY /,/^\\./' \
| sed "s/$SOURCE_USER_ID/$TARGET_USER_ID/g" \
> "$COPY_FILE"
if $DRY_RUN; then
echo "would import (dry run)"
head -3 "$COPY_FILE"
continue
fi
if [[ "$TARGET_COUNT" != "0" && "$FORCE" == "true" ]]; then
psql "$TARGET_URL" -c "TRUNCATE TABLE $t CASCADE;" 2>/dev/null
fi
RESULT=$(psql "$TARGET_URL" -f "$COPY_FILE" 2>&1)
if echo "$RESULT" | grep -q "^COPY"; then
ROWS=$(echo "$RESULT" | grep "^COPY" | awk '{print $2}')
echo "imported $ROWS rows ✓"
IMPORTED=$((IMPORTED + 1))
else
echo "ERROR"
echo "$RESULT" | head -5
fi
done
echo ""
echo "=== Verification ==="
psql "$TARGET_URL" -c "
SELECT 'entities' as t, count(*) FROM entities
UNION ALL SELECT 'user_projects', count(*) FROM user_projects
UNION ALL SELECT 'goals', count(*) FROM goals
UNION ALL SELECT 'subscriptions', count(*) FROM subscriptions
UNION ALL SELECT 'commitments', count(*) FROM commitments
UNION ALL SELECT 'events', count(*) FROM events
UNION ALL SELECT 'interactions', count(*) FROM interactions
UNION ALL SELECT 'prompt_history', count(*) FROM prompt_history
UNION ALL SELECT 'orchestration_events', count(*) FROM orchestration_events
ORDER BY t;" 2>/dev/null
echo ""
echo "Done: $IMPORTED tables imported, $SKIPPED skipped."