- desktop build clean, tsc clean, lint 0, home/ 89/89
- advances desktop-2/3: dispatches now produce durable events in ~/.fleetcrown/events.jsonl with proper runId/sentinel so stop-hooks and web control plane treat them as first-class
- still no embedded watcher (idle via co-running home/watcher.ts for now)
Refs: desktop-app.md phases, Cockpit.roadmap T1/T2, prior landing 2d98638
6.4 KiB
Postgres portability (studio-wide)
created_date: 2026-05-22 last_modified_date: 2026-05-23 last_modified_summary: Document Oracle bootstrap/cutover scripts and one-console-step flow.
Goal: switching Postgres host later is a config + dump/restore job, not a code rewrite.
Long-form narrative: The Database Kill Switch.
Applies to FleetCrown first; copy the same env pattern to other Drizzle/Prisma apps in ~/dev.
Principles
- Standard Postgres only — no vendor-specific SQL, extensions, or SDKs in app code.
- Two URLs in production — direct for migrations/LISTEN; pooled for serverless queries.
- Local dev never hits cloud —
.env.local→ Docker Postgres onlocalhost. - One database per project on a shared host (Oracle/Hetzner) when you migrate — not one Neon project per app.
- Supabase stays Supabase — orangecat/printcraft use auth, RLS, storage; do not force them onto plain Postgres without a rewrite.
Environment variables (SSOT)
| Variable | Purpose | Who sets it |
|---|---|---|
DATABASE_URL |
Direct session (:5432) — migrations, drizzle-kit push, LISTEN/NOTIFY |
Vercel + local dev |
DATABASE_POOL_URL |
Pooled session (PgBouncer :6432, Neon pooler, Supabase pooler) — app runtime queries |
Vercel production only |
Local dev: set DATABASE_URL only. Pool URL falls back to the same value.
Legacy (FleetCrown still reads these): NEON_DATABASE_URL → pool, NEON_DATABASE_URL_DIRECT → direct. Prefer the neutral names on new hosts.
Implementation: src/lib/db-url.ts — getDatabasePoolUrl(), getDatabaseDirectUrl().
Dev vs production
┌─────────────────────────────────────────────────────────────┐
│ Laptop: docker compose up db │
│ DATABASE_URL=postgresql://fleetcrown:changeme@localhost:5432/fleetcrown
│ (never the cloud URL in .env.local) │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Vercel (unchanged app code when host changes) │
│ DATABASE_URL=postgresql://user:pass@host:5432/fleetcrown │
│ DATABASE_POOL_URL=postgresql://user:pass@host:6432/cockpit │
└─────────────────────────────────────────────────────────────┘
Self-hosted target layout (Oracle or Hetzner)
One VM, Postgres 17, optional PgBouncer:
# On the VM (example)
createdb fleetcrown
createdb kivvi
createdb revampit
# … one DB per project
# PgBouncer listens on 6432 → forwards to localhost:5432
Point each Vercel project at its own database name on the same host.
Migration runbook (when you switch)
Full database (schema + data)
# 1. Backup source
DATABASE_URL="$OLD_DIRECT_URL" ./scripts/db/dump.sh fleetcrown-pre-migrate.sql.gz
# 2. Create empty DB on new host, push schema
DATABASE_URL="$NEW_DIRECT_URL" npm run migrate
# 3. Restore
DATABASE_URL="$NEW_DIRECT_URL" ./scripts/db/restore.sh fleetcrown-pre-migrate.sql.gz
# 4. Update Vercel env (both URLs), redeploy
# 5. Smoke test sign-in + one CRUD path + Control daemon push
User data only (schema already on target)
SOURCE_DATABASE_URL="$OLD" TARGET_DATABASE_URL="$NEW" TARGET_USER_ID="<uuid>" \
./scripts/db/sync-user-data.sh
Cutover checklist
DATABASE_URL(direct) andDATABASE_POOL_URL(pool) set on Vercel- Local
.env.localstill points at Docker — not new prod npm run migratesucceeds against new direct URL- Auth sign-in works
- Control SSE / daemon push updates
runtime_snapshots - Decommission old Neon project after 7 days
FleetCrown scripts
| Script | Purpose |
|---|---|
npm run test:db-url |
Assert URL resolution logic |
scripts/db/dump.sh |
Portable pg_dump backup |
scripts/db/restore.sh |
Restore into target DB |
scripts/db/sync-user-data.sh |
Copy user-owned rows between DBs |
scripts/migrate-local-to-neon.sh |
Wrapper → sync-user-data.sh |
npm run db:oracle-bootstrap |
SSH key + cloud-init for Oracle VM (one-time) |
npm run db:oracle-cutover |
Dump Neon → deploy VM stack → restore → Vercel env + redeploy |
Oracle cutover (minimal manual steps)
- One-time on laptop:
npm run db:oracle-bootstrap— writes~/.config/cockpit/oracle-migration.envandoracle-cloud-init.yaml. - One-time in Oracle Console (~5 min): Create VM.Standard.A1.Flex Ubuntu ARM instance; paste cloud-init; note public IP.
- Automated:
ORACLE_HOST=<ip> npm run db:oracle-cutover— dumps current prod, installs Docker stack on VM, restores, updates VercelDATABASE_URL+DATABASE_POOL_URL, redeploys.
Cloud-init template: infra/postgres-host/cloud-init.yaml. Public ports: 5432 (direct), 6432 (PgBouncer). Use a strong generated password only — no Neon-style SSL required for initial cutover (sslmode=disable on pool URL).
What we are not doing yet
- Supabase-native apps (orangecat) stay on Supabase
Interim production (2026-05-22)
Neon project bitbaum-pg replaced quota-exhausted ep-bold-shape-…. See The Database Kill Switch for the full postmortem and target architecture (Oracle/Hetzner trunk).
Vercel uses DATABASE_URL (direct) + DATABASE_POOL_URL (pooler). Local dev uses Docker only.
When the VM is ready, pg_dump from bitbaum-pg → restore on self-hosted → update Vercel env.
Other projects in ~/dev
| Pattern | Action |
|---|---|
| Drizzle/Prisma + Neon | Add DATABASE_POOL_URL on Vercel; local Docker only |
| Supabase-native (orangecat, printcraft) | Keep Supabase; skip this doc for DB |
| Local-only (reparaturbonus, datacat) | Already portable — use same dump/restore pattern |
Extract src/lib/db-url.ts into a shared snippet or copy per repo when you migrate the second app.