FIX: correct duplicate granting

This commit is contained in:
Sam 2014-07-05 18:32:06 +10:00
parent d99a9b6735
commit 4a25c86d61
3 changed files with 18 additions and 5 deletions

View File

@ -74,7 +74,7 @@ class BadgeGranter
LEFT JOIN ( #{badge.query} ) q
ON q.user_id = ub.user_id
#{post_clause}
WHERE ub.id = :id AND q.user_id IS NULL
WHERE ub.badge_id = :id AND q.user_id IS NULL
)"
Badge.exec_sql(sql, id: badge.id)
@ -83,9 +83,9 @@ class BadgeGranter
SELECT :id, q.user_id, q.granted_at, -1, #{post_id_field}
FROM ( #{badge.query} ) q
LEFT JOIN user_badges ub ON
ub.id = :id AND ub.user_id = q.user_id
ub.badge_id = :id AND ub.user_id = q.user_id
#{post_clause}
WHERE ub.id IS NULL"
WHERE ub.badge_id IS NULL"
Badge.exec_sql(sql, id: badge.id)

View File

@ -0,0 +1,11 @@
class IndexUserBadges < ActiveRecord::Migration
def change
execute 'DELETE FROM user_badges USING user_badges ub2
WHERE user_badges.badge_id = ub2.badge_id AND
user_badges.user_id = ub2.user_id AND
user_badges.post_id IS NOT NULL AND
user_badges.id < ub2.id
'
add_index :user_badges, [:badge_id, :user_id, :post_id], unique: true, where: 'post_id IS NOT NULL'
end
end

View File

@ -28,8 +28,10 @@ describe BadgeGranter do
it 'should grant missing badges' do
post = Fabricate(:post, like_count: 30)
BadgeGranter.backfill(Badge.find(Badge::NicePost))
BadgeGranter.backfill(Badge.find(Badge::GoodPost))
2.times {
BadgeGranter.backfill(Badge.find(Badge::NicePost))
BadgeGranter.backfill(Badge.find(Badge::GoodPost))
}
# TODO add welcome
post.user.user_badges.pluck(:badge_id).sort.should == [Badge::NicePost,Badge::GoodPost]