fix(coach): move bumped conversation timestamps back to the last message

Earlier rewrites of conversation rows stamped updated_at with the run time,
so the Coach panel listed untouched conversations under Today. Migration
0375 moves updated_at back to the newest message's created_at where it is
more than a minute later. A rename or attachment shortly after the last
turn keeps its stamp, conversations without messages are left alone, and a
second run changes nothing. One set-based statement; the per-conversation
maximum reads from the (conversation_id, created_at) index.
This commit is contained in:
Marc-André Bombeck committed 2026-10-05 12:53:40 +02:00
1 parent 370fa10efa
commit 7836610358
2 files changed
+138

No files matched your search

@@ -0,0 +1,26 @@
-- Move a Coach conversation's updated_at back to its newest message.
--
-- The free-text encryption backfill (v1.39.3) and the memory-summary refresh
-- rewrote conversation rows through a Prisma update, which stamped updated_at
-- with the run time. The Coach panel orders and groups by that column, so
-- conversations nobody had touched in months showed up under "Today".
--
-- A turn stamps updated_at in the same transaction that creates its message,
-- so the newest message's created_at is the conversation's real last
-- activity. Only rows more than a minute later than that move back; a rename
-- or a document attached shortly after the last turn keeps its stamp, and a
-- conversation without messages is left alone. Running it again changes
-- nothing: a repaired row is no longer later than its newest message.
--
-- One set-based statement. The per-conversation maximum is read from
-- "coach_messages_conversation_id_created_at_idx" (conversation_id,
-- created_at), so it does not sort the message table.
UPDATE "coach_conversations" AS c
SET "updated_at" = m."last_at"
FROM (
SELECT "conversation_id", MAX("created_at") AS "last_at"
FROM "coach_messages"
GROUP BY "conversation_id"
) AS m
WHERE m."conversation_id" = c."id"
AND c."updated_at" > m."last_at" + INTERVAL '1 minute';