Files
loki/scripts/db
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
..

scripts/db — FleetCrown + OrangeCat database migration toolkit

These scripts and runbooks let you migrate both products' production databases off their managed-DB providers (Neon for FleetCrown, Supabase for OrangeCat) and onto a single self-hosted Postgres box in ~30 min (Hetzner) or ~1 hour (Oracle Free Tier).

Why this exists

Both Neon and Supabase price on data transfer / row-egress beyond a generous-looking free tier, then cliff into "upgrade or be silently broken" once you trip a cap. We hit Neon's wall in early-June 2026 — every DB-dependent endpoint 500'd until the cap reset. Supabase's caps are similar in shape; OrangeCat would hit them eventually.

Self-hosting on a Hetzner CX22 (€4.51/mo) or Oracle Always-Free ARM (free forever) eliminates that class of failure entirely. Vercel-to-VM egress is uncharged on a box you control.

A CX22 is over-provisioned for both products combined at today's scale — see BOTH_PRODUCTS_ONE_HOST.md for capacity math.

Files

File Purpose
dump-from-neon.sh pg_dump FleetCrown out of Neon to a plain SQL file + row-count manifest.
dump-from-supabase.sh Same shape for OrangeCat. Defaults to public schema only (auth/storage stay on Supabase). FULL_DUMP=1 for everything.
restore-to-target.sh psql either dump into a fresh target, verify row counts match the manifest. Halts before flipping Vercel if anything mismatches.
SETUP_HETZNER.md Provision + install + secure a Hetzner CX22 for Postgres 17.
SETUP_ORACLE_FREE.md Same shape for Oracle Always-Free ARM.
BOTH_PRODUCTS_ONE_HOST.md Layer two databases on one Postgres instance — role + pg_hba + Vercel env setup, plus capacity math and a "when to split" trigger list.

End-to-end migration (cookbook)

For migrating both products together, follow BOTH_PRODUCTS_ONE_HOST.md — it sequences FleetCrown first (it's the broken one), OrangeCat second. The steps below cover one product at a time.

Step 1 — dump source

FleetCrown / Neon:

SOURCE_DATABASE_URL="$(vercel env pull --environment=production /tmp/env >/dev/null && grep ^DATABASE_URL= /tmp/env | cut -d= -f2- | tr -d '"')" \
  scripts/db/dump-from-neon.sh

OrangeCat / Supabase:

# Use the DIRECT URL (port 5432), NOT the pooler (port 6543).
# Supabase dashboard → Settings → Database → Connection string → "Direct connection"
SOURCE_DATABASE_URL="postgresql://postgres:...@db.<project>.supabase.co:5432/postgres" \
  scripts/db/dump-from-supabase.sh

Outputs (same shape for both):

  • <source>-dump-<timestamp>.sql
  • <source>-dump-<timestamp>.manifest.txt

If Neon fails with "Your project has exceeded the data transfer quota," either upgrade the Neon plan once (just long enough to dump) OR wait for the monthly reset. Supabase's caps are different — they typically allow dumps even past the cap as long as the direct connection still responds.

Step 2 — stand up target

Pick one:

  • Hetzner (recommended for paid simplicity): follow SETUP_HETZNER.md sections 1-5.
  • Oracle Free: follow SETUP_ORACLE_FREE.md sections 1-5.

You end with a working postgresql://fleetcrown:...@<host>:5432/fleetcrown?sslmode=require URL.

Step 3 — restore + verify

TARGET_DATABASE_URL="postgresql://fleetcrown:...@<host>:5432/fleetcrown?sslmode=require" \
DUMP_FILE="neon-dump-<timestamp>.sql" \
  scripts/db/restore-to-target.sh

The script halts if any table row count mismatches the source manifest.

Step 4 — flip Vercel

vercel env rm DATABASE_URL production
vercel env add DATABASE_URL production
# paste the new URL
vercel env rm DATABASE_POOL_URL production || true
vercel env add DATABASE_POOL_URL production
# paste the new URL

vercel --prod --yes

Step 5 — smoke

curl -s https://fleetcrown.vercel.app/api/health    # → 200
curl -s -o /dev/null -w "%{http_code}\n" https://fleetcrown.vercel.app/  # → 200

Browse /control, /settings, /system — should load without "Something went wrong."

Step 6 — keep Neon dump cold

After 24h of clean operation, pause Neon. Keep the dump file in cold storage (tar czf neon-dump-<timestamp>.tgz neon-dump-<timestamp>.{sql,manifest.txt} && mv neon-dump-<timestamp>.tgz ~/backups/) for at least 30 days before deleting the Neon project entirely.

Why not Drizzle migrations?

Drizzle's drizzle-kit push syncs schema, but data migration is not its job. pg_dump/psql is the well-trodden path for "move this database intact," and the SQL output is auditable in a way binary formats aren't.