diff --git a/app/models/post.rb b/app/models/post.rb index bf0f2a406..2c7c00c7f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -624,10 +624,10 @@ class Post < ApplicationRecord set_tag_count(category,self.send("tag_count_#{category}") + 1) end - def set_tag_counts + def set_tag_counts(disable_cache = true) self.tag_count = 0 TagCategory.categories.each {|x| set_tag_count(x,0)} - categories = Tag.categories_for(tag_array, :disable_caching => true) + categories = Tag.categories_for(tag_array, :disable_caching => disable_cache) categories.each_value do |category| self.tag_count += 1 inc_tag_count(TagCategory.reverse_mapping[category]) @@ -1138,9 +1138,11 @@ class Post < ApplicationRecord module CountMethods def fix_post_counts(post) - post.set_tag_counts - args = Hash[TagCategory.categories.map {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count) - post.update_columns(args) + post.set_tag_counts(false) + if post.changed? + args = Hash[TagCategory.categories.map {|x| ["tag_count_#{x}",post.send("tag_count_#{x}")]}].update(:tag_count => post.tag_count) + post.update_columns(args) + end end def get_count_from_cache(tags) diff --git a/script/fixes/050_fix_post_tagcount.rb b/script/fixes/050_fix_post_tagcount.rb new file mode 100644 index 000000000..e30070f43 --- /dev/null +++ b/script/fixes/050_fix_post_tagcount.rb @@ -0,0 +1,20 @@ +#!/usr/bin/env ruby + +require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment')) + +CurrentUser.user = User.system +CurrentUser.ip_addr = "127.0.0.1" + +Post.without_timeout do + Post.where("updated_at > ? ", Date.parse("2017-11-12")).find_each do |post| + Post.fix_post_counts(post) + end +end + +Tag.without_timeout do + Tag.where(category: Tag.categories.meta).find_each do |tag| + Post.raw_tag_match(tag.name).where("true").find_each do |post| + Post.fix_post_counts(post) + end + end +end