FIX: when ensuring consistency also delete orphan badges
This commit is contained in:
parent
6bb97dcabb
commit
418bc4a844
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue