FIX: If posts are deleted they should be updated in consistency jobs

This commit is contained in:
Robin Ward 2016-06-21 12:59:33 -04:00
parent 5d125b02d9
commit 94a4af6af7
2 changed files with 27 additions and 16 deletions

View File

@ -25,6 +25,7 @@ class TopicFeaturedUsers
def self.ensure_consistency!(topic_id=nil)
filter = "#{"AND t.id = #{topic_id.to_i}" if topic_id}"
filter2 = "#{"AND tt.id = #{topic_id.to_i}" if topic_id}"
sql = <<SQL
@ -53,11 +54,12 @@ cte2 as (
UPDATE topics tt
SET
featured_user1_id = featured_user1,
featured_user2_id = featured_user2,
featured_user3_id = featured_user3,
featured_user4_id = featured_user4
FROM (
featured_user1_id = x.featured_user1,
featured_user2_id = x.featured_user2,
featured_user3_id = x.featured_user3,
featured_user4_id = x.featured_user4
FROM topics AS tt2
LEFT OUTER JOIN (
SELECT
c.id,
MAX(case when c.rank = 1 then c.user_id end) featured_user1,
@ -66,14 +68,15 @@ FROM (
MAX(case when c.rank = 4 then c.user_id end) featured_user4
FROM cte2 as c
GROUP BY c.id
) x
WHERE x.id = tt.id AND
) x ON x.id = tt2.id
WHERE tt.id = tt2.id AND
(
COALESCE(featured_user1_id,-99) <> COALESCE(featured_user1,-99) OR
COALESCE(featured_user2_id,-99) <> COALESCE(featured_user2,-99) OR
COALESCE(featured_user3_id,-99) <> COALESCE(featured_user3,-99) OR
COALESCE(featured_user4_id,-99) <> COALESCE(featured_user4,-99)
COALESCE(tt.featured_user1_id,-99) <> COALESCE(x.featured_user1,-99) OR
COALESCE(tt.featured_user2_id,-99) <> COALESCE(x.featured_user2,-99) OR
COALESCE(tt.featured_user3_id,-99) <> COALESCE(x.featured_user3,-99) OR
COALESCE(tt.featured_user4_id,-99) <> COALESCE(x.featured_user4,-99)
)
#{filter2}
SQL
Topic.exec_sql(sql)

View File

@ -1,12 +1,12 @@
require 'rails_helper'
describe TopicFeaturedUsers do
it 'ensures consistenct' do
it 'ensures consistency' do
t = Fabricate(:topic)
Fabricate(:post, topic_id: t.id, user_id: t.user_id)
p2 = Fabricate(:post, topic_id: t.id)
Fabricate(:post, topic_id: t.id, user_id: p2.user_id)
p3 = Fabricate(:post, topic_id: t.id, user_id: p2.user_id)
p4 = Fabricate(:post, topic_id: t.id)
p5 = Fabricate(:post, topic_id: t.id)
@ -14,11 +14,9 @@ describe TopicFeaturedUsers do
featured_user2_id: 70,
featured_user3_id: 12,
featured_user4_id: 7,
last_post_user_id: p5.user_id
)
last_post_user_id: p5.user_id)
TopicFeaturedUsers.ensure_consistency!
t.reload
expect(t.featured_user1_id).to eq(p2.user_id)
@ -26,6 +24,16 @@ describe TopicFeaturedUsers do
expect(t.featured_user3_id).to eq(nil)
expect(t.featured_user4_id).to eq(nil)
# after removing a post
p2.update_column(:deleted_at, Time.now)
p3.update_column(:hidden, true)
TopicFeaturedUsers.ensure_consistency!
t.reload
expect(t.featured_user1_id).to eq(p4.user_id)
expect(t.featured_user2_id).to eq(nil)
expect(t.featured_user3_id).to eq(nil)
expect(t.featured_user4_id).to eq(nil)
end
end