Files
gochat/backend/migrations/000065_backfill_conversation_activity_timestamps.up.sql
T
Rogee b78f963472 chore: 提交剩余开发改动
包含商务通消息映射、会话时间与未读状态、前端消息展示、数据库修复迁移、测试覆盖备份及 Captain 设计文档。
2026-08-06 10:10:25 +08:00

41 lines
1007 B
SQL

UPDATE conversations AS c
SET
last_activity_at = COALESCE(
c.last_activity_at,
(
SELECT EXTRACT(EPOCH FROM m.created_at)::bigint
FROM messages AS m
WHERE m.conversation_id = c.id
ORDER BY m.created_at DESC, m.id DESC
LIMIT 1
)
),
last_message_at = COALESCE(
c.last_message_at,
(
SELECT EXTRACT(EPOCH FROM m.created_at)::bigint
FROM messages AS m
WHERE m.conversation_id = c.id
ORDER BY m.created_at DESC, m.id DESC
LIMIT 1
)
),
last_non_sys_msg_at = COALESCE(
c.last_non_sys_msg_at,
(
SELECT EXTRACT(EPOCH FROM m.created_at)::bigint
FROM messages AS m
WHERE m.conversation_id = c.id
AND m.message_type <> 'activity'
ORDER BY m.created_at DESC, m.id DESC
LIMIT 1
)
)
WHERE (c.last_activity_at IS NULL
OR c.last_message_at IS NULL
OR c.last_non_sys_msg_at IS NULL)
AND EXISTS (
SELECT 1
FROM messages AS m
WHERE m.conversation_id = c.id
);