users: fix incorrect count columns on users table.

Fix incorrect post_upload_count, note_update_count, and
unread_dmail_count columns on the users table.
This commit is contained in:
evazion
2022-01-09 12:06:16 -06:00
parent 5623b139aa
commit c09cd9e9fd
4 changed files with 120 additions and 20 deletions

View File

@@ -599,26 +599,6 @@ class User < ApplicationRecord
)
end
end
class_methods do
def fix_favorite_counts!
# Fix users that have a non-zero favorite_count but no favorites.
User.where("favorite_count != 0").where.not(id: Favorite.select(:user_id).distinct).update_all(favorite_count: 0)
# Fix users that have a favorite_count inconsistent with the favorites table.
User.find_by_sql(<<~SQL.squish)
UPDATE users
SET favorite_count = true_count
FROM (
SELECT user_id, COUNT(*) AS true_count
FROM favorites
GROUP BY user_id
) true_counts
WHERE users.id = user_id AND users.favorite_count != true_count
RETURNING users.*
SQL
end
end
end
module SearchMethods