Database Runbook
This runbook covers the migration workflow, Neon branch strategy, and RLS testing for Factory apps.Architecture
Every Factory app gets an isolated Neon project (not just a schema). This ensures:- No cross-app data leakage at the database level
- Independent scaling and compute pause per app
- Clean isolation during app transfer to a buyer
env.DB) for connection pooling and latency reduction.
Neon branch strategy
| Branch | Purpose |
|---|---|
main | Production database |
staging | Staging environment (branched from main) |
| Per-PR (optional) | Ephemeral branches for integration testing |
Running migrations
Migrations live insrc/db/migrations/ as numbered SQL files (001_init.sql, etc.).
Apply manually
Apply via CI
Themigrate.yml workflow runs on push to main and applies migrations automatically using the DATABASE_URL GitHub secret.
Inspecting the schema
Factory events table
Each app maintains its ownfactory_events table inside its own isolated Neon database. The schema is identical across all apps (shared DDL from the scaffold), but there is no central database — each app writes and queries its own events independently via @adrper79-dot/analytics.
A future factory_core Neon project will aggregate events across all apps when factory-admin is built. Until then, cross-app analytics require querying each app’s database separately.
Row-Level Security (RLS)
Factory apps use RLS to isolate tenant data when multiple tenants share a table.Enabling RLS
Setting the tenant context per request
Testing RLS in development
Backup and restore
Neon provides continuous branching which doubles as PITR (Point-in-Time Recovery). For explicit backups:Neon connection strings
Connection strings follow this format:{APP}_CONNECTION_STRING and pass to Wrangler: