Updating topic count in categories after bulk delete & missing topics/posts


(Bryan Underwood) #1

Thanks @pfaffman for the bulk deletion snippet you wrote using the Rails find_each method. Great work.

After bulk back-end deletion, is there a way to update the topic count in categories? Right now, I have an empty topic list which is ready for fresh import but the topic count in my categories still shows a count of thousands.And when I re-import fresh, the number only grows.

I waited overnight since I am not sure if there is a cron job or background task in Discourse that updates the counts. (I’m new to Discourse)

I feel like there is a command to refresh_topic_count or something…


(Felix Freiberger) #2

I had a similar problem.

The following code (to be run within rails console) may solve your problem, make it worse, or set your hair on fire :fire:. Use at your own risk (and with a backup).

Category.all.each { |c|
    c.topic_count = c.topics.length - 1 # -1 for about post
    c.save!
}

Alternatively, rake import:ensure_consistency from here might help, but it seems to have destructive side effects.


(Bryan Underwood) #3

Awesome, thanks @fefrei . Since I am new to Discourse and apparently did not completely read the docs I was not even aware that Sidekiq existed! :heart_eyes: Totally awesome. The solution was to simply trigger the Jobs::CategoryStats. No fire needed :fire_engine:.

[comment redacted (figured it out)]


(Bryan Underwood) #4

Now, I have a new problem which I don’t know how to feel about…

After import, I expected to have 2158 topics (or thereabout):
image

However, my new category count is around 1700. I checked the rails console to get (find_each) topics count where user not discobot and it confirms the new category topic count around 1700. Is there something I’m missing? Is there a log of “skipped topics” during import? I can’t seem to trace it out in the base.rb import include. Where does base.rb “puts” the error messages, exactly? Because it’s not in the ssh console.


(RĂ©gis Hanol) #5

What does Topic.count returns (when executed in the rails console)?


(Bryan Underwood) #6

Sorry, I meant to say my topic count not category count. Trying to say my “topic count” showing next to my “category” label. The topic count returns 1691.


(Bryan Underwood) #8

FIXED.

[I deleted my other replies in this topic, since they were the wrong path]

I happened to find this idea by @Simon_Cossar

This helped me to see that the Post.find_each method was not returning deleted posts. So when I went to re-import, my data it still existed. Now, before I re-import I run this for topics (and separately, posts):

require_relative '../discourse/config/environment'

cnt = 0
#Topic.find_each do |t| # only returns active topics
Topic.with_deleted.find_each do |t| # returns both active and deleted topics
  if t.id > 6 && t.user_id != -2 # excludes included system topics and Discobot user Greeting messages
    puts "Topic: #{t.id}, #{t.title}, #{t.user_id}"
    t.destroy
        cnt = cnt +1
  end
end
puts "Deleted #{cnt} active and deleted topics where t.id > 6 && t.user_id != -2"