mirror of
https://github.com/discourse/discourse.git
synced 2025-04-28 07:34:37 +08:00
FIX: correctly update replies_count on chat_threads (#24711)
The previous query would look at the existing messages, count them, and update the associated thread. But, if for some reason messages were **ALL** deleted without updating the `replies_count`, then the query wouldn't find any message, and wouldn't update any thread's `replies_count`.
This commit is contained in:
parent
707acbe696
commit
c5aa6b5e16
@ -126,17 +126,16 @@ module Chat
|
|||||||
# It is updated eventually via Jobs::Chat::PeriodicalUpdates. In
|
# It is updated eventually via Jobs::Chat::PeriodicalUpdates. In
|
||||||
# future we may want to update this more frequently.
|
# future we may want to update this more frequently.
|
||||||
updated_thread_ids = DB.query_single <<~SQL
|
updated_thread_ids = DB.query_single <<~SQL
|
||||||
UPDATE chat_threads threads
|
UPDATE chat_threads ct
|
||||||
SET replies_count = subquery.replies_count
|
SET replies_count = GREATEST(COALESCE(subquery.new_count, 0), 0)
|
||||||
FROM (
|
FROM (
|
||||||
SELECT COUNT(*) - 1 AS replies_count, thread_id
|
SELECT cm.thread_id, COUNT(cm.*) - 1 AS new_count
|
||||||
FROM chat_messages
|
FROM chat_threads
|
||||||
WHERE chat_messages.deleted_at IS NULL AND thread_id IS NOT NULL
|
LEFT JOIN chat_messages cm ON cm.thread_id = chat_threads.id AND cm.deleted_at IS NULL
|
||||||
GROUP BY thread_id
|
GROUP BY cm.thread_id
|
||||||
) subquery
|
) AS subquery
|
||||||
WHERE threads.id = subquery.thread_id
|
WHERE ct.id = subquery.thread_id AND ct.replies_count IS DISTINCT FROM GREATEST(COALESCE(subquery.new_count, 0), 0)
|
||||||
AND subquery.replies_count != threads.replies_count
|
RETURNING ct.id AS thread_id
|
||||||
RETURNING threads.id AS thread_id;
|
|
||||||
SQL
|
SQL
|
||||||
return if updated_thread_ids.empty?
|
return if updated_thread_ids.empty?
|
||||||
self.clear_caches!(updated_thread_ids)
|
self.clear_caches!(updated_thread_ids)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user