mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 20:42:43 +00:00
`medication_intake_events.idempotency_key` carried a GLOBAL unique from 0001_init. The value is client-supplied, so two people running the same client can mint the same key — and on a global unique the second one is simply refused by the constraint. Their dose is not written and nothing tells them so. The bulk drain made it worse: its replay probe read the key with no user scope, resolved the first user's row, and reported the second user's entry as a duplicate carrying a stranger's row id. Migration 0273 replaces the global unique with `(user_id, idempotency_key)`, the grain every other client-supplied identity on the table already uses. Both unscoped reads in the bulk drain now key on the composite; the other four reads were already narrowed by user. On existing data this is a relaxation, so nothing needs reconciling: the old constraint is strictly stronger than the new one, and no pair of rows can violate an index the previous one already forbade. The migration still carries a reconciliation step for a database whose global index was dropped by hand at some point. It never deletes or rewrites a dose — it clears the dedup HINT on the later row of a colliding pair and keeps every intake event, timestamp, attribution and inventory stamp intact. The new index is created before the old one is dropped, so a failure aborts with the old protection still in place. A structural guard freezes the read side: no `medicationIntakeEvent` query may filter on the key without narrowing by `userId`.
prisma/
The database layer. HealthLog runs PostgreSQL 16 via Prisma 7. This directory holds the single schema file and the forward-only migration history; the generated client is emitted elsewhere.
Contents
schema.prisma— 61 models, one file.cuid()primary keys,snake_casecolumns via@map(...), encrypted columns mostly suffixed*Encrypted(search the file; there are 24 encrypted column references). Thegenerator clientblock emits to../src/generated/prisma(imported as@/generated/prisma).migrations/— numbered, forward-only SQL migrations (0001_…upward).migration_lock.tomlpins the provider topostgresql.
Migration workflow
# author a migration from a schema edit
pnpm dlx prisma migrate dev --name <short_snake_case_name>
# regenerate the client after any schema change
pnpm dlx prisma generate
Number a new migration with the next free prefix. The production entrypoint runs prisma migrate deploy on boot, so every migration must apply cleanly forward — there is no down-migration path. Always include the migration file in the same change as the schema edit.
Conventions
- Encryption is fail-closed:
*Encryptedcolumns are written through@/lib/crypto(AES-256-GCM, versioned key ids); never store plaintext in an encrypted column. Key rotation isscripts/rotate-encryption-key.ts+docs/ops/encryption-key-rotation.md. - The generated client under
src/generated/prismais build output — do not edit it. - Schema-touching changes that affect the API surface must regenerate
docs/api/openapi.yaml(see../CLAUDE.md).
Related: ../src/lib/README.md (db.ts, rollups/), ../docs/ops/ (migrations + backup runbooks).