DB naming conventions
This document defines naming conventions for StateStore tables and migration SQL, with the goal of keeping SQLite and Postgres schemas aligned and reducing drift.
Primary keys (PK)
- Tenant-scoped entities: prefer composite PKs:
(tenant_id, <entity>_id)(UUID in Postgres;TEXTin SQLite). - Surrogate/auto-increment PKs: use
<table>_id(not genericid) when the value is returned to callers or used in DAL queries. - Consistency rule: the PK column name for a table must be the same in both dialects (SQLite + Postgres).
Foreign keys (FK)
- Name FK columns after the referenced PK column:
<ref_table>_id. - For tenant-scoped references, include
tenant_idin the FK columns (and in the referenced key) to prevent cross-tenant linkage.
Timestamps
- Use
created_at/updated_atfor wall-clock timestamps (TIMESTAMPTZon Postgres;TEXTin SQLite). - Use
*_at_msfor millisecond epoch timestamps (BIGINTon Postgres;INTEGERin SQLite).
Migration checklist
When adding or modifying migrations under packages/gateway/migrations/*:
- Apply the same schema changes in both
sqlite/andpostgres/. - Keep PK/FK/timestamp column names consistent with the conventions above.
- Run the schema contract test:
pnpm test packages/gateway/tests/contract/schema-contract.test.ts.