discourse/lib/tasks/categories.rake
Bianca Nenciu f0ec1fad8c
FIX: Update category tag stats with new or deleted (#21531)
The old method updated only existing records, without considering that
new tags might have been created or some tags might not exist anymore.
This was usually not a problem because the stats were also updated by
other code paths.

However, the ensure consistency job should be more solid and help when
other code paths fail or after importing data.

Also, update category tag stats too should happen when updating other
category stats as well.
2023-05-18 12:46:44 +03:00

60 lines
1.6 KiB
Ruby

# frozen_string_literal: true
task "categories:move_topics", %i[from_category to_category] => [:environment] do |_, args|
from_category_id = args[:from_category]
to_category_id = args[:to_category]
if !from_category_id || !to_category_id
puts "ERROR: Expecting categories:move_topics[from_category_id,to_category_id]"
exit 1
end
from_category = Category.find(from_category_id)
to_category = Category.find(to_category_id)
if from_category.present? && to_category.present?
puts "Moving topics from #{from_category.slug} to #{to_category.slug}..."
Topic
.where(category_id: from_category.id)
.where.not(id: from_category.topic_id)
.update_all(category_id: to_category.id)
from_category.update_attribute(:topic_count, 0)
puts "Updating category stats..."
Category.update_stats
CategoryTagStat.update_topic_counts
end
puts "", "Done!", ""
end
task "categories:create_definition" => :environment do
puts "Creating category definitions"
puts
Category.where(topic_id: nil).each(&:create_category_definition)
puts "", "Done!", ""
end
def print_status(current, max)
print "\r%9d / %d (%5.1f%%)" % [current, max, ((current.to_f / max.to_f) * 100).round(1)]
end
desc "Output a list of categories"
task "categories:list" => :environment do
categories = Category.where(parent_category_id: nil).order(:slug).pluck(:id, :slug)
puts "id category-slug"
puts "-- -----------------"
categories.each do |c|
puts "#{c[0]} #{c[1]}"
Category
.where(parent_category_id: c[0])
.order(:slug)
.pluck(:id, :slug)
.each { |s| puts " #{s[0]} #{s[1]}" }
end
end