Files
loki/scripts/db/dump-from-supabase.sh
T
Mao NakamotoandClaude Opus 4.7 9b80cc9137 chore(db): extend migration toolkit to OrangeCat / Supabase + both-on-one-host
Same job, one more source. Now the toolkit handles:
- FleetCrown out of Neon (`dump-from-neon.sh`)
- OrangeCat out of Supabase (`dump-from-supabase.sh`)
- Both consolidated onto one self-hosted Postgres box

scripts/db/dump-from-supabase.sh
  Mirrors dump-from-neon.sh's interface. Supabase-specific guards:
  - Refuses pooler URLs (port 6543 / pooler.supabase.com) because
    pg_dump fails midway through transaction-pooled connections.
  - Defaults to --schema=public only. Supabase's auth/storage/realtime/
    extensions schemas would create dangling-role FK errors on a vanilla
    Postgres target. FULL_DUMP=1 dumps everything when self-hosting
    gotrue too (bigger project for another day).
  - Same .sql + .manifest.txt output shape as the Neon script;
    restore-to-target.sh accepts either without modification.

scripts/db/BOTH_PRODUCTS_ONE_HOST.md
  Layers two databases (`fleetcrown`, `orangecat`) on a single Postgres
  instance with separate roles and per-role pg_hba.conf lines. Covers:
  - role + database setup
  - pg_hba rules: each role authenticates only to its own DB
  - migration sequence: FleetCrown first (it's broken), OrangeCat
    second (still functional on Supabase)
  - auth-stays-on-Supabase recommendation for OrangeCat (cheap, no
    gotrue self-host needed at this scale)
  - shared backup cron with off-box rsync mirror
  - PgBouncer add-on for when connection count balloons
  - explicit "when to split the box" thresholds (sustained 70% CPU,
    <95% cache hit ratio, p95 latency >50ms)

scripts/db/README.md
  Updated header + file table to mention both products. Migration
  cookbook now has separate Step 1 entries for Neon vs Supabase and
  points at BOTH_PRODUCTS_ONE_HOST.md for the dual-product path.

Capacity sanity for the curious: a Hetzner CX22 (€4.51/mo, 2 vCPU, 4 GB
RAM, 40 GB SSD) is over-provisioned by ~3 orders of magnitude for both
products at today's scale (single-user each). Upgrade in-place to CX32
when sustained CPU exceeds 70% — that's many months out.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 09:04:53 +02:00

91 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Dump the OrangeCat production database from Supabase to a portable SQL
# file. Companion to dump-from-neon.sh; same output shape, same manifest
# format, restore-to-target.sh accepts either.
#
# Usage (from inside the orangecat repo, or anywhere with the URL set):
# SOURCE_DATABASE_URL="postgresql://postgres:[password]@db.<ref>.supabase.co:5432/postgres" \
# scripts/db/dump-from-supabase.sh
#
# Supabase-specific notes:
# - Use the DIRECT connection URL (port 5432), NOT the pooler URL
# (port 6543). pg_dump doesn't play well with PgBouncer's transaction
# pooling mode.
# - By default we dump ONLY the public schema. Supabase reserves
# `auth`, `storage`, `realtime`, `extensions`, `graphql`, `vault`,
# etc. for its own components. Those tables come back when you
# restore Supabase Auth / Storage separately (or when you self-host
# gotrue). Including them in a vanilla-Postgres restore creates
# foreign-key errors against missing roles.
# - If you want EVERYTHING (because you're self-hosting gotrue too),
# set FULL_DUMP=1 and the script switches to a no-filter dump.
#
# Output: ./supabase-dump-<timestamp>.sql + .manifest.txt
set -euo pipefail
if [[ -z "${SOURCE_DATABASE_URL:-}" ]]; then
echo "error: SOURCE_DATABASE_URL not set" >&2
echo "usage: SOURCE_DATABASE_URL='postgresql://postgres:...@db.X.supabase.co:5432/postgres' $0" >&2
echo " (use the direct connection URL — port 5432, NOT pooler port 6543)" >&2
exit 64
fi
# Refuse pooler URLs — pg_dump on the pooler gets stuck in transaction
# pooling mode and fails midway.
if [[ "${SOURCE_DATABASE_URL}" == *":6543/"* ]] || [[ "${SOURCE_DATABASE_URL}" == *"pooler.supabase"* ]]; then
echo "error: SOURCE_DATABASE_URL points at the Supabase pooler (port 6543 or pooler.supabase.com)" >&2
echo " pg_dump needs the direct connection. Find it in:" >&2
echo " Supabase dashboard → Settings → Database → Connection string → 'Direct connection'" >&2
exit 64
fi
STAMP=$(date -u +%Y%m%d-%H%M%S)
OUT="supabase-dump-${STAMP}.sql"
MANIFEST="supabase-dump-${STAMP}.manifest.txt"
if [[ "${FULL_DUMP:-}" = "1" ]]; then
echo "==> FULL_DUMP=1 → dumping all schemas (including auth/storage/etc.)"
echo " Restore requires the matching Supabase roles to exist on the target."
pg_dump \
--no-owner --no-acl --no-tablespaces \
--format=plain --quote-all-identifiers \
--file="${OUT}" "${SOURCE_DATABASE_URL}"
else
echo "==> dumping public schema only (vanilla-Postgres-restorable)"
echo " Auth/storage stay on Supabase. Set FULL_DUMP=1 to dump everything."
pg_dump \
--no-owner --no-acl --no-tablespaces \
--schema=public \
--format=plain --quote-all-identifiers \
--file="${OUT}" "${SOURCE_DATABASE_URL}"
fi
SIZE=$(stat -c %s "${OUT}" 2>/dev/null || stat -f %z "${OUT}" 2>/dev/null)
echo " dump size: ${SIZE} bytes"
echo "==> writing manifest to ${MANIFEST}"
{
echo "Generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "Source host: $(echo "${SOURCE_DATABASE_URL}" | sed 's|.*@||; s|/.*||')"
echo "Source product: OrangeCat (Supabase)"
echo "Dump mode: ${FULL_DUMP:+all schemas}${FULL_DUMP:-public schema only}"
echo "Dump file: ${OUT}"
echo "Dump size: ${SIZE} bytes"
echo
echo "Row counts (for post-restore verification):"
psql "${SOURCE_DATABASE_URL}" -tAc "
SELECT schemaname || '.' || relname || ' ' || n_live_tup
FROM pg_stat_user_tables
WHERE schemaname = 'public'
ORDER BY relname
" | sed 's/^/ /'
} > "${MANIFEST}"
echo
echo "==> done"
echo " ${OUT}"
echo " ${MANIFEST}"
echo
echo "Next: scripts/db/restore-to-target.sh (any product, same script)"