better consistency check

This commit is contained in:
Sam 2013-04-08 13:01:58 +10:00
parent 9ad320768e
commit 73c508115a
2 changed files with 8 additions and 3 deletions

View File

@ -180,16 +180,21 @@ class TopicUser < ActiveRecord::Base
UPDATE topic_users t
SET
last_read_post_number = last_read,
seen_post_count = GREATEST(t.seen_post_count, last_read)
seen_post_count = LEAST(max_post_number,GREATEST(t.seen_post_count, last_read))
FROM (
SELECT topic_id, user_id, MAX(post_number) last_read
FROM post_timings
GROUP BY topic_id, user_id
) as X
JOIN (
SELECT p.topic_id, MAX(p.post_number) max_post_number from posts p
GROUP BY p.topic_id
) as Y on Y.topic_id = X.topic_id
WHERE X.topic_id = t.topic_id AND
X.user_id = t.user_id AND
(
last_read_post_number <> last_read
last_read_post_number <> last_read OR
seen_post_count <> LEAST(max_post_number,GREATEST(t.seen_post_count, last_read))
)
SQL
end

View File

@ -212,7 +212,7 @@ describe TopicUser do
p1 = Fabricate(:post)
p2 = Fabricate(:post, user: p1.user, topic: p1.topic, post_number: 2)
TopicUser.exec_sql("UPDATE topic_users set seen_post_count=0, last_read_post_number=0
TopicUser.exec_sql("UPDATE topic_users set seen_post_count=100, last_read_post_number=0
WHERE topic_id = :topic_id AND user_id = :user_id", topic_id: p1.topic_id, user_id: p1.user_id)
[p1,p2].each do |p|