From c31039d51f386ba7582c8c0cf93a64b806852549 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 14 Feb 2020 12:13:17 -0500 Subject: [PATCH] FIX: tag topic counts wrong after adding synonyms After adding a tag as a synonym of another tag, both tags will have the wrong topic counts. It's corrected within 12 hours by the EnsureDbConsistency job. This fix ensures the topic counts are updated much sooner. --- lib/discourse_tagging.rb | 3 +++ spec/components/discourse_tagging_spec.rb | 2 ++ 2 files changed, 5 insertions(+) diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb index 94b9f2cf93b..af34e95a0ed 100644 --- a/lib/discourse_tagging.rb +++ b/lib/discourse_tagging.rb @@ -424,6 +424,9 @@ module DiscourseTagging end successful = existing.select { |t| !t.errors.present? } TopicTag.where(tag_id: successful.map(&:id)).update_all(tag_id: target_tag.id) + Scheduler::Defer.later "Update tag topic counts" do + Tag.ensure_consistency! + end (existing - successful).presence || true end diff --git a/spec/components/discourse_tagging_spec.rb b/spec/components/discourse_tagging_spec.rb index ee12b4ce37b..35ff67a9a9a 100644 --- a/spec/components/discourse_tagging_spec.rb +++ b/spec/components/discourse_tagging_spec.rb @@ -568,6 +568,8 @@ describe DiscourseTagging do topic = Fabricate(:topic, tags: [tag2]) expect(DiscourseTagging.add_or_create_synonyms_by_name(tag1, [tag2.name])).to eq(true) expect_same_tag_names(topic.reload.tags, [tag1]) + expect(tag1.reload.topic_count).to eq(1) + expect(tag2.reload.topic_count).to eq(0) end end end