DEV: search more carefully for missing uploads

rake uploads:analyze_missing_s3 was not looking at all places, amend it so
it looks in all the places where uploads could exist.
This commit is contained in:
Sam Saffron
2020-08-26 17:48:42 +10:00
parent 3cc761ac44
commit b60c39fdf0

View File

@ -1007,7 +1007,9 @@ def analyze_missing_s3
lookup = {} lookup = {}
other = [] other = []
all = []
DB.query(sql).each do |r| DB.query(sql).each do |r|
all << r
if r.post_id if r.post_id
lookup[r.post_id] ||= [] lookup[r.post_id] ||= []
lookup[r.post_id] << [r.url, r.sha1, r.extension] lookup[r.post_id] << [r.url, r.sha1, r.extension]
@ -1029,26 +1031,35 @@ def analyze_missing_s3
puts "Total missing uploads: #{Upload.where(verified: false).count}" puts "Total missing uploads: #{Upload.where(verified: false).count}"
puts "Total problem posts: #{lookup.keys.count} with #{lookup.values.sum { |a| a.length } } missing uploads" puts "Total problem posts: #{lookup.keys.count} with #{lookup.values.sum { |a| a.length } } missing uploads"
puts "Other missing uploads count: #{other.count}" puts "Other missing uploads count: #{other.count}"
if other.count > 0
ids = other.map { |r| r.id } if all.count > 0
count = DB.query_single(<<~SQL, ids: ids).first ids = all.map { |r| r.id }
SELECT COUNT(*) FROM users WHERE uploaded_avatar_id IN (:ids)
SQL lookups = [
if count > 0 [:post_uploads, :upload_id],
puts "Found #{count} uploaded avatars" [:users, :uploaded_avatar_id],
end [:user_avatars, :gravatar_upload_id],
count = DB.query_single(<<~SQL, ids: ids).first [:user_avatars, :custom_upload_id],
SELECT COUNT(*) FROM user_avatars WHERE gravatar_upload_id IN (:ids) [:site_settings, ["NULLIF(value, '')::integer", "data_type = #{SiteSettings::TypeSupervisor.types[:upload].to_i}"]],
SQL [:user_profiles, :profile_background_upload_id],
if count > 0 [:user_profiles, :card_background_upload_id],
puts "Found #{count} gravatars" [:categories, :uploaded_logo_id],
end [:categories, :uploaded_background_id],
count = DB.query_single(<<~SQL, ids: ids).first [:custom_emojis, :upload_id],
SELECT COUNT(*) FROM user_avatars WHERE custom_upload_id IN (:ids) [:theme_fields, :upload_id],
SQL [:user_exports, :upload_id],
if count > 0 [:groups, :flair_upload_id],
puts "Found #{count} custom uploaded avatars" ]
lookups.each do |table, (column, where)|
count = DB.query_single(<<~SQL, ids: ids).first
SELECT COUNT(*) FROM #{table} WHERE #{column} IN (:ids) #{"AND #{where}" if where}
SQL
if count > 0
puts "Found #{count} missing row#{"s" if count > 1} in #{table}(#{column})"
end
end end
end end
end end