mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 15:28:37 +08:00
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:
@ -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
|
||||
|
Reference in New Issue
Block a user