Repository navigation
Closed
Labels
ready-for-agentFully specified, ready for an AFK agent
Description
Part of #193. Implements spec §3 and ADR-0009; decisions One Postgres database per company, the pooling research and, for the dev layer, the PGlite research.
What to build
packages/company-database (@patchy/company-database), born with its CONTEXT.md (Company database, Placement, Namespace, Inventory, Patch lock, Provisioning login) and migration 0004_company_database_baseline (company_databases: company_id, server_id, database_name, placement_version, status, created_at, ready_at), spread to the server, the dev runner and the vitest template.
CompanyDatabases:claim(companyId)inserts the placement row as a claim, idempotent under a race;ensureReady(companyId)creates the database through the provisioning login (PATCHY_COMPANY_DB_ADMIN_URL,CREATEDB, fromtemplate1, outside any transaction), applies settings and grants, initialises schemapatchy(spec §3's inventory tables pluspatchy.filesfrom §5) and movesstatustoready; an interrupted create resumes from the row.withCompany(companyId)(effect)leases aSqlClientfrom a scopedRcMapregistry (pool max 4, min 0, idle TTL 60 s, capacity 100,PATCHY_COMPANY_DB_MAX_BACKENDSdefault 200; exhaustion fails withbusy).withPatchLock(patchId)(effect)runspg_advisory_xact_lockon a stable key inside the company transaction. One server,server_id = "primary";PATCHY_COMPANY_DB_URLis the data-role template; both required at startup.Inventory: read a patch's cumulative inventory, write rows, bump the schema revision. Physical namespacep_<patchId>, identifiers quoted as written.- The rule callers follow: the platform patch-row lock always; the company transaction and
withPatchLockonly when the patch has inventory or the operation introduces resources. - The orphan sweep: a
p_*schema with no patch row and older than a day, and a file object no index row names and older than a day, dropped on the existing sweep's schedule. - A PGlite layer of the same services (
PgliteClient, one directory, one connection, one statement persql.unsafe, codecs normalised forint8andDATE) for the dev runtime, importing no production bootstrap. pnpm devpoints both URLs at the embedded Postgres; the vitest suites create company databases lazily against the embedded Postgres and drop them when the block closes.
Done when
- Two concurrent
ensureReadycalls for one company create one database and both seeready; a create interrupted afterCREATE DATABASEresumes and finishes; capacity exhaustion fails fast withbusy. - The lock serialises two DDL transactions on one patch on real Postgres; the sweep drops a namespace with no patch row and leaves one with a row.
- The suite passes over the PGlite layer for everything but concurrency.
- ADR-0009 written;
CONTEXT-MAP.mdlists Company database under Infrastructure;AGENTS.mdanddocs/DEVELOPMENT.mdupdated.
Surfaces
Migrations, the dev loop (docs/DEVELOPMENT.md, patchy-dev-loop, the vitest template), vocabulary (new CONTEXT.md, CONTEXT-MAP.md), the ADR, the README layout.
Metadata
Metadata
Assignees
Labels
ready-for-agentFully specified, ready for an AFK agent
Activity