FIX: when uploads are destroyed clear up avatar refs in user table

This also auto corrects twice daily when we ensure consistency
This commit is contained in:
Sam
2018-08-31 14:46:22 +10:00
parent 6b9aeeea73
commit e1975e293f
6 changed files with 101 additions and 0 deletions

View File

@ -1283,6 +1283,20 @@ class User < ActiveRecord::Base
true
end
def self.ensure_consistency!
DB.exec <<~SQL
UPDATE users
SET uploaded_avatar_id = NULL
WHERE uploaded_avatar_id IN (
SELECT u1.uploaded_avatar_id FROM users u1
LEFT JOIN uploads up
ON u1.uploaded_avatar_id = up.id
WHERE u1.uploaded_avatar_id IS NOT NULL AND
up.id IS NULL
)
SQL
end
end
# == Schema Information