Users are reporting missing avatars after importing from vBulletin or restoring a Discourse database, despite the avatar URLs and database records appearing correct. pfaffman notes avatars show as generic placeholders and confirms the files are missing from uploads or tombstone, even though the upload record exists. fhe suggests checking a related thread, but pfaffman finds it unhelpful. Overgrow experiences the same issue after a DB restore and migration, noting that extension: "unknown" and missing dimensions may be the cause. Some avatars regenerate automatically, but slowly. Overgrow shares a Ruby script to fix avatar uploads by correcting extension, dimensions, and regenerating OptimizedImage thumbnails (version 2), which resolved the issue for them. The root problem appears to be corrupted or incomplete Upload records after migration or import, particularly missing metadata like extension and dimensions, which prevents thumbnail generation.
I did a fresh import and all of the avatars are missing. Sidekiq still has 13M things to process, but I don’t see how that could be the problem. I did another import on another site with what I believe to be the same import script and the avatars are fine there.
Somehow my database after backup restore and migration from 1.6 to 2.5 Discourse was missing width, height and extension values for Upload table (file pointers were correct and files on its place).
I have ended up creating a script that goes through all custom uploaded avatars and corrects extension, dimensions and re-generates all OptimizedImages with version 2 thumbnails which I was missing.
For anyone interested, here it is:
fix-avatars.rb
require File.expand_path("../../config/environment", __FILE__)
generated_count = 0
scope = UserAvatar.where("custom_upload_id > 0")
scope.find_each do |ua|
upload_id = ua.custom_upload_id
upload = Upload.find_by(id: upload_id)
unless upload
puts "\nno upload found for this user avatar"
next
end
# fix unknown
if upload.extension === 'unknown'
upload.extension = nil
upload.save!
puts "\rupload_id #{upload.id} extension unknown -> nil"
end
if upload.extension.nil?
puts "\rupload_id #{upload.id} fixing extension"
upload.fix_image_extension
if upload.height == 0 || upload.width == 0
puts "\rupload_id #{upload.id} fixing dimensions"
upload.fix_dimensions!
end
# check optimized version
correct_optimized = OptimizedImage.where(upload_id: upload_id)
.where(version: 2)
if correct_optimized.count < 1
puts "\rupload_id #{upload.id} doesn't have version 2 thumbnails"
# destroy old optimized
puts "\rupload_id #{upload.id} deleting avatars"
optimized = OptimizedImage.where(upload_id: upload_id)
if optimized
optimized.destroy_all
end
#generate optimized
puts "\rupload_id #{upload.id} creating avatars"
Discourse.avatar_sizes.each {|size| OptimizedImage.create_for(upload, size, size, allow_animation: SiteSetting.allow_animated_avatars)}
generated_count += 1
puts "\rGenerated #{generated_count} total avatars"
end
end
end