Files
zeekayandHanzo Dev 8083a487c9 feat(managed): Hanzo SQL — per-tenant SQLite product spec, deploy CRs, pg→SQLite ETL fix
Replaces the shared multi-tenant Postgres (ghcr.io/hanzoai/sql:18) with the
per-tenant, SQLite-backed, provision-on-demand model — the relational analog of
docdb (FerretDB-on-SQLite). Builds under sql/managed/, never touches the
Postgres source tree.

Crux decided by real prisma output (evidence/):
- Option A (pglite pg-wire): the UNMODIFIED dataroom pg schema (62 tables, 18
  enums, 11 array cols, @db.* native types) `prisma db push`es and runs a live
  enum+text[] CRUD roundtrip → the pg-wire escape hatch for un-rewritable
  schemas / external ManagedDatabase tenants.
- Option B/C (SQLite dialect): the same schema is rejected at Prisma validation
  (P1012, 11 scalar-list + 6 native-type errors) before any backend — no
  pg-wire-over-SQLite proxy can fix it. postlite rejected.
- Primary path stays file:SQLite (esign/Service.persistence): rewrite the schema
  (arrays→JSON String) + one-time data ETL. esign already runs this in prod.

Deliverables:
- LLM.md: product spec — existing operator model (Service.persistence, SQL/
  ManagedDatabase datastore Kinds), per-tenant layout, provisioning, DNS,
  DATABASE_URL handoff (KMSSecret→env, mirrors docdb MONGO_URI), image
  ghcr.io/hanzoai/sql-managed (semver only), blockers (DatastoreSpec fsGroup gap,
  multi-tenant router, inert credentialsSecret).
- deploy/sql.yaml (path A, SQL Kind, sql-managed image) +
  service-persistence.example.yaml (path C, esign pattern).
- etl/enum-array-parser.patch: fixes a real data-loss bug in esign's
  backfill-pg-to-sqlite.ts — node-pg returns enum arrays (Role[]) as the raw
  string {ADMIN,USER}, aborting the load (User.roles is set for every user).
  Registers node-pg's text-array parser for enum-array OIDs. Validated pg→SQLite
  on 4554 synthetic rows: full parity + byte-exact type fidelity + esign Prisma
  read-back (evidence/etl-pg-to-sqlite-validation.log).
- pgwire/: tested pglite pg-wire prototype (server.mjs, provision.mjs, Dockerfile
  FROM ghcr.io/hanzoai/nodejs:v24.18.0) — the Option-A follow-up.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-22 15:16:23 -07:00

41 lines
1.7 KiB
JavaScript

// Hanzo SQL — tenant provisioning helper (pg-wire path).
//
// Declarative provisioning is the operator: one `ManagedDatabase`/`SQL` CR per
// tenant reconciles to a pglite pod + PVC + Service `<tenant>.<ns>.svc:5432`.
// This helper is the imperative dev/CI counterpart and the DATABASE_URL emitter
// the operator status would otherwise carry: it pre-initialises a tenant's data
// directory (so first connect is not cold) and prints the connection string a
// consumer receives (synced into a k8s Secret via a KMSSecret CR, then injected
// as env `DATABASE_URL` valueFrom secretKeyRef — same mechanism docdb uses for
// MONGO_URI).
//
// node provision.mjs <tenant> [--data /data] [--svc <tenant>.hanzo.svc] [--port 5432]
import { PGlite } from '@electric-sql/pglite'
import { mkdirSync } from 'node:fs'
import { join } from 'node:path'
const args = process.argv.slice(2)
const tenant = args[0]
if (!tenant || tenant.startsWith('--')) {
console.error('usage: node provision.mjs <tenant> [--data DIR] [--svc HOST] [--port N]')
process.exit(2)
}
const opt = (name, def) => {
const i = args.indexOf(name)
return i >= 0 && args[i + 1] ? args[i + 1] : def
}
const dataRoot = opt('--data', '/data')
const svc = opt('--svc', `${tenant}.hanzo.svc.cluster.local`)
const port = opt('--port', '5432')
const dir = join(dataRoot, tenant)
mkdirSync(dir, { recursive: true })
const db = await PGlite.create(dir)
await db.waitReady
await db.close()
// pglite runs a single superuser role `postgres`, no password (auth is network
// isolation + one-tenant-per-endpoint). Prisma needs `?schema=public`.
const url = `postgresql://postgres@${svc}:${port}/postgres?schema=public`
console.log(url)