mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FIX: keep topic.word_count in sync (#27065)
Whenever one creates, updates, or deletes a post, we should keep the `topic.word_count` counter in sync. Context - https://meta.discourse.org/t/-/308062
This commit is contained in:
@@ -855,14 +855,22 @@ class Topic < ActiveRecord::Base
|
||||
FROM posts
|
||||
WHERE deleted_at IS NULL AND post_type <> 4
|
||||
GROUP BY topic_id
|
||||
)
|
||||
),
|
||||
Z as (
|
||||
SELECT topic_id,
|
||||
SUM(COALESCE(posts.word_count, 0)) word_count
|
||||
FROM posts
|
||||
WHERE deleted_at IS NULL AND post_type <> 4
|
||||
GROUP BY topic_id
|
||||
)
|
||||
UPDATE topics
|
||||
SET
|
||||
highest_staff_post_number = X.highest_post_number,
|
||||
highest_post_number = Y.highest_post_number,
|
||||
last_posted_at = Y.last_posted_at,
|
||||
posts_count = Y.posts_count
|
||||
FROM X, Y
|
||||
posts_count = Y.posts_count,
|
||||
word_count = Z.word_count
|
||||
FROM X, Y, Z
|
||||
WHERE
|
||||
topics.archetype <> 'private_message' AND
|
||||
X.topic_id = topics.id AND
|
||||
@@ -870,7 +878,8 @@ class Topic < ActiveRecord::Base
|
||||
topics.highest_staff_post_number <> X.highest_post_number OR
|
||||
topics.highest_post_number <> Y.highest_post_number OR
|
||||
topics.last_posted_at <> Y.last_posted_at OR
|
||||
topics.posts_count <> Y.posts_count
|
||||
topics.posts_count <> Y.posts_count OR
|
||||
topics.word_count <> Z.word_count
|
||||
)
|
||||
SQL
|
||||
|
||||
@@ -891,14 +900,22 @@ class Topic < ActiveRecord::Base
|
||||
FROM posts
|
||||
WHERE deleted_at IS NULL AND post_type <> 3 AND post_type <> 4
|
||||
GROUP BY topic_id
|
||||
),
|
||||
Z as (
|
||||
SELECT topic_id,
|
||||
SUM(COALESCE(posts.word_count, 0)) word_count
|
||||
FROM posts
|
||||
WHERE deleted_at IS NULL AND post_type <> 3 AND post_type <> 4
|
||||
GROUP BY topic_id
|
||||
)
|
||||
UPDATE topics
|
||||
SET
|
||||
highest_staff_post_number = X.highest_post_number,
|
||||
highest_post_number = Y.highest_post_number,
|
||||
last_posted_at = Y.last_posted_at,
|
||||
posts_count = Y.posts_count
|
||||
FROM X, Y
|
||||
posts_count = Y.posts_count,
|
||||
word_count = Z.word_count
|
||||
FROM X, Y, Z
|
||||
WHERE
|
||||
topics.archetype = 'private_message' AND
|
||||
X.topic_id = topics.id AND
|
||||
@@ -906,7 +923,8 @@ class Topic < ActiveRecord::Base
|
||||
topics.highest_staff_post_number <> X.highest_post_number OR
|
||||
topics.highest_post_number <> Y.highest_post_number OR
|
||||
topics.last_posted_at <> Y.last_posted_at OR
|
||||
topics.posts_count <> Y.posts_count
|
||||
topics.posts_count <> Y.posts_count OR
|
||||
topics.word_count <> Z.word_count
|
||||
)
|
||||
SQL
|
||||
end
|
||||
@@ -941,6 +959,13 @@ class Topic < ActiveRecord::Base
|
||||
post_type <> 4
|
||||
#{post_type}
|
||||
),
|
||||
word_count = (
|
||||
SELECT SUM(COALESCE(posts.word_count, 0)) FROM posts
|
||||
WHERE topic_id = :topic_id AND
|
||||
deleted_at IS NULL AND
|
||||
post_type <> 4
|
||||
#{post_type}
|
||||
),
|
||||
last_posted_at = (
|
||||
SELECT MAX(created_at) FROM posts
|
||||
WHERE topic_id = :topic_id AND
|
||||
|
||||
Reference in New Issue
Block a user