Skip to content

feat(company-database): one Postgres database per company, its registry, pools and inventory #196

Closed

Description

@allisonmahmood

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, from template1, outside any transaction), applies settings and grants, initialises schema patchy (spec §3's inventory tables plus patchy.files from §5) and moves status to ready; an interrupted create resumes from the row. withCompany(companyId)(effect) leases a SqlClient from a scoped RcMap registry (pool max 4, min 0, idle TTL 60 s, capacity 100, PATCHY_COMPANY_DB_MAX_BACKENDS default 200; exhaustion fails with busy). withPatchLock(patchId)(effect) runs pg_advisory_xact_lock on a stable key inside the company transaction. One server, server_id = "primary"; PATCHY_COMPANY_DB_URL is the data-role template; both required at startup.
  • Inventory: read a patch's cumulative inventory, write rows, bump the schema revision. Physical namespace p_<patchId>, identifiers quoted as written.
  • The rule callers follow: the platform patch-row lock always; the company transaction and withPatchLock only 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 per sql.unsafe, codecs normalised for int8 and DATE) for the dev runtime, importing no production bootstrap.
  • pnpm dev points 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 ensureReady calls for one company create one database and both see ready; a create interrupted after CREATE DATABASE resumes and finishes; capacity exhaustion fails fast with busy.
  • 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.md lists Company database under Infrastructure; AGENTS.md and docs/DEVELOPMENT.md updated.

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

No one assigned

    Labels

    ready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions