Merge branch 'fix/encrypt-free-text-and-local-ai' into release/v1.39.3

# Conflicts:
#	src/app/api/import/csv/route.ts
This commit is contained in:
Marc-André Bombeck committed 2026-09-26 21:50:05 +02:00
commit 66b1aef40c
69 files changed
+3625 -367

No files matched your search

@@ -0,0 +1,32 @@
-- Two free-text columns were still stored readable: the title of a Coach
-- conversation (the first 80 characters of the opening message, or the name a
-- person gave the thread) and the note on a custom-metric reading. Both move
-- to AES-256-GCM at rest, in the same Bytes codec every other note uses.
--
-- SQL cannot encrypt, so this migration only adds the ciphertext columns and
-- the discovery indexes. The boot-time `free-text-encryption-backfill` job
-- encrypts the existing rows one at a time and nulls the readable value in the
-- same transaction. Readers prefer the ciphertext and fall back to the old
-- column until the backfill has reached a row. The old columns are dropped in
-- a later release, once the backfill reports nothing left on every instance.
--
-- Additive only: no table rewrite, no data change. The title loses its NOT
-- NULL because every new title is written to the ciphertext column and the
-- readable one stays empty.
ALTER TABLE "coach_conversations" ADD COLUMN "title_encrypted" BYTEA;
ALTER TABLE "coach_conversations" ALTER COLUMN "title" DROP NOT NULL;
ALTER TABLE "custom_metric_entries" ADD COLUMN "note_encrypted" BYTEA;
-- Partial indexes on exactly the backfill's discovery predicate, so the boot
-- scan is index-only and shrinks to nothing as the backfill converges (the
-- 0243 precedent). Non-concurrent: Prisma Migrate runs the file in a
-- transaction, and the build lands in the pre-traffic window at boot.
CREATE INDEX IF NOT EXISTS "coach_conversations_title_backfill_idx"
ON "coach_conversations" ("user_id")
WHERE "title" IS NOT NULL;
CREATE INDEX IF NOT EXISTS "custom_metric_entries_note_backfill_idx"
ON "custom_metric_entries" ("user_id")
WHERE "note" IS NOT NULL;
+25 -1
View File
@@ -5846,7 +5846,15 @@ model CustomMetricEntry {
/// Unit snapshot from the metric at write time.
unit String
measuredAt DateTime @map("measured_at")
/// @internal: legacy plaintext note, read only as a fallback for rows the
/// free-text encryption backfill has not reached yet (v1.39.3). Every write
/// since then goes to `noteEncrypted` and leaves this NULL; the backfill
/// nulls it on the old rows. DROP this column in a later release, once the
/// backfill reports zero remaining rows on every instance.
note String?
/// v1.39.3 — AES-256-GCM ciphertext of the note. Same Bytes codec as the
/// measurement and mood notes.
noteEncrypted Bytes? @map("note_encrypted")
createdAt DateTime @default(now()) @map("created_at")
/// v1.37.20 — soft-delete tombstone, peer parity with Measurement /
/// MoodEntry (every other entry surface deletes recoverably with an undo
@@ -5857,6 +5865,10 @@ model CustomMetricEntry {
metric CustomMetric @relation(fields: [customMetricId], references: [id], onDelete: Cascade)
@@index([userId, customMetricId, measuredAt])
// `custom_metric_entries_note_backfill_idx` is a partial index over
// `(user_id) WHERE note IS NOT NULL` — see
// migration 0357 for the free-text encryption boot-discovery scan. Raw SQL
// only (partial predicate not expressible in Prisma).
@@map("custom_metric_entries")
}
@@ -6107,7 +6119,15 @@ model RecommendationFeedback {
model CoachConversation {
id String @id @default(cuid())
userId String @map("user_id")
title String
/// @internal: legacy plaintext title, read only as a fallback for rows the
/// free-text encryption backfill has not reached yet (v1.39.3). Every write
/// since then goes to `titleEncrypted` and leaves this NULL; the backfill
/// nulls it on the old rows. DROP this column in a later release, once the
/// backfill reports zero remaining rows on every instance.
title String?
/// v1.39.3 — AES-256-GCM ciphertext of the title (the first 80 characters of
/// the opening Coach message, or the rename). Same Bytes codec as the notes.
titleEncrypted Bytes? @map("title_encrypted")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@ -6141,6 +6161,10 @@ model CoachConversation {
// and the tool-route send path filters on the flag. Replaces the retired
// [userId, documentId, updatedAt] composite.
@@index([userId, documentScoped, updatedAt])
// `coach_conversations_title_backfill_idx` is a partial index over
// `(user_id) WHERE title IS NOT NULL` — see
// migration 0357 for the free-text encryption boot-discovery scan. Raw SQL
// only (partial predicate not expressible in Prisma).
@@map("coach_conversations")
}