FIX: when ensuring consistency also delete orphan badges

This commit is contained in:
Sam 2016-03-29 16:41:03 +11:00
parent 6bb97dcabb
commit 418bc4a844
2 changed files with 20 additions and 0 deletions

View File

@ -374,6 +374,13 @@ SQL
end
def self.ensure_consistency!
exec_sql <<SQL
DELETE FROM user_badges
USING user_badges ub
LEFT JOIN users u ON u.id = ub.user_id
WHERE u.id IS NULL
SQL
Badge.find_each(&:reset_grant_count!)
end

View File

@ -43,5 +43,18 @@ describe Badge do
expect(badge[:long_description]).to eq("testing it")
end
it 'can ensure consistency' do
b = Badge.first
b.grant_count = 100
b.save
UserBadge.create!(user_id: -100, badge_id: b.id, granted_at: 1.minute.ago, granted_by_id: -1)
Badge.ensure_consistency!
b.reload
expect(b.grant_count).to eq(0)
end
end