41 lines
1007 B
SQL
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
|
|
); |