Remove cached counters from user model and update references

This commit is contained in:
Kira 2019-06-04 06:41:43 -07:00
parent 621148b097
commit d21e36073b
6 changed files with 39 additions and 21 deletions

View File

@ -4,7 +4,7 @@ class FavoriteManager
Favorite.transaction(isolation: :serializable) do
unless force
if user.favorite_count >= user.favorite_limit
raise Favorite::Error, "You can only keep up to #{user.favorite_limit} favorites. Upgrade your account to save more."
raise Favorite::Error, "You can only keep up to #{user.favorite_limit} favorites."
end
end
@ -41,4 +41,4 @@ class FavoriteManager
end
true
end
end
end

View File

@ -66,7 +66,7 @@ module Reports
end
def users
::User.where("users.bit_prefs & ? = 0 and users.post_upload_count >= 250", ::User.flag_value_for("can_upload_free")).order("created_at desc").map {|x| Reports::UserPromotions::User.new(x)}
::User.joins(:user_status).where("users.bit_prefs & ? = 0 and user_statuses.post_count >= 250", ::User.flag_value_for("can_upload_free")).order("created_at desc").map {|x| Reports::UserPromotions::User.new(x)}
end
end
end

View File

@ -1038,7 +1038,7 @@ class Post < ApplicationRecord
def remove_from_favorites
Favorite.where(post_id: id).delete_all
user_ids = fav_string.scan(/\d+/)
User.where(:id => user_ids).update_all("favorite_count = favorite_count - 1")
UserStatus.where(:user_id => user_ids).update_all("favorite_count = favorite_count - 1")
end
end
@ -1455,7 +1455,6 @@ class Post < ApplicationRecord
end
def create_new_version
User.where(id: CurrentUser.id).update_all("post_update_count = post_update_count + 1")
PostArchive.queue(self)
end

View File

@ -654,10 +654,22 @@ class User < ApplicationRecord
user_status.wiki_edit_count
end
def post_update_count
user_status.post_update_count
end
def post_upload_count
user_status.post_count
end
def note_version_count
user_status.note_count
end
def note_update_count
note_version_count
end
def artist_version_count
user_status.artist_edit_count
end
@ -704,10 +716,10 @@ class User < ApplicationRecord
def refresh_counts!
self.class.without_timeout do
User.where(id: id).update_all(
post_upload_count: Post.for_user(id).count,
UserStatus.where(user_id: id).update_all(
post_count: Post.for_user(id).count,
post_update_count: PostArchive.for_user(id).count,
note_update_count: NoteVersion.where(updater_id: id).count
note_count: NoteVersion.where(updater_id: id).count
)
end
end
@ -750,6 +762,7 @@ class User < ApplicationRecord
def search(params)
q = super
q = q.joins(:user_status)
params = params.dup
params[:name_matches] = params.delete(:name) if params[:name].present?
@ -757,10 +770,11 @@ class User < ApplicationRecord
q = q.search_text_attribute(:name, params)
q = q.attribute_matches(:level, params[:level])
q = q.attribute_matches(:inviter_id, params[:inviter_id])
q = q.attribute_matches(:post_upload_count, params[:post_upload_count])
q = q.attribute_matches(:post_update_count, params[:post_update_count])
q = q.attribute_matches(:note_update_count, params[:note_update_count])
q = q.attribute_matches(:favorite_count, params[:favorite_count])
# TODO: Doesn't support relation filtering using this method.
# q = q.attribute_matches(:post_upload_count, params[:post_upload_count])
# q = q.attribute_matches(:post_update_count, params[:post_update_count])
# q = q.attribute_matches(:note_update_count, params[:note_update_count])
# q = q.attribute_matches(:favorite_count, params[:favorite_count])
if params[:name_matches].present?
q = q.where_ilike(:name, normalize_name(params[:name_matches]))
@ -815,11 +829,11 @@ class User < ApplicationRecord
when "name"
q = q.order("name")
when "post_upload_count"
q = q.order("post_upload_count desc")
q = q.order("user_statuses.post_count desc")
when "note_count"
q = q.order("note_update_count desc")
q = q.order("user_statuses.note_count desc")
when "post_update_count"
q = q.order("post_update_count desc")
q = q.order("user_statuses.post_update_count desc")
else
q = q.apply_default_order(params)
end

View File

@ -0,0 +1,9 @@
class RemoveExtraUserStatsFromUserTable < ActiveRecord::Migration[5.2]
def change
remove_column :users, :post_upload_count
remove_column :users, :post_update_count
remove_column :users, :note_update_count
remove_column :users, :favorite_count
remove_column :users, :set_count
end
end

View File

@ -2577,10 +2577,6 @@ CREATE TABLE public.users (
last_logged_in_at timestamp without time zone,
last_forum_read_at timestamp without time zone,
recent_tags text,
post_upload_count integer DEFAULT 0 NOT NULL,
post_update_count integer DEFAULT 0 NOT NULL,
note_update_count integer DEFAULT 0 NOT NULL,
favorite_count integer DEFAULT 0 NOT NULL,
comment_threshold integer DEFAULT '-1'::integer NOT NULL,
default_image_size character varying DEFAULT 'large'::character varying NOT NULL,
favorite_tags text,
@ -2595,7 +2591,6 @@ furry -rating:s'::text,
bit_prefs bigint DEFAULT 0 NOT NULL,
last_ip_addr inet,
unread_dmail_count integer DEFAULT 0 NOT NULL,
set_count integer DEFAULT 0 NOT NULL,
profile_about text,
profile_artinfo text,
avatar_id integer
@ -5124,6 +5119,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20190430120155'),
('20190510184237'),
('20190510184245'),
('20190602115848');
('20190602115848'),
('20190604125828');