discourse/lib/tasks/topic_localizations.rake
Natalie Tay 675a5e5be3
DEV: Add rake task to backfill localization excerpts which were empty (#36901)
This is a continuation of
https://github.com/discourse/discourse/pull/36885.

The rake task here backfills `topic_localization.excerpt` which may have
been empty (both `nil` and `""` are valid as checked on meta).

The reason why this can't be a migration is because we use
`Post.excerpt` which taps on a method in the post model which also uses
`PrettyText`.
2025-12-30 17:04:36 +08:00

29 lines
877 B
Ruby

# frozen_string_literal: true
desc "Backfill empty excerpts for topic localizations from post localizations"
task "topic_localizations:backfill_excerpts" => :environment do
updated = 0
scope =
TopicLocalization
.where(excerpt: [nil, ""])
.joins(:topic)
.where.not(topics: { excerpt: "" })
.includes(topic: { first_post: :localizations })
total = scope.count
puts "Found #{total} topic localizations to process"
next if total == 0
scope.find_each do |topic_localization|
post_localization =
topic_localization.topic.first_post&.localizations&.find_by(locale: topic_localization.locale)
if post_localization
topic_localization.update_excerpt(cooked: post_localization.cooked)
updated += 1
end
print "\r#{updated}/#{total} processed"
end
puts "\nDone! Updated #{updated} topic localizations."
end