A platform for community discussion. Free, open, simple.
FIX: delete synonym tags if other synonyms are already exist. (#21885)
Vinoth Kannan6409794e0f04
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rbindex 03567f44bb4..6aac1518881 100644--- a/lib/discourse_tagging.rb+++ b/lib/discourse_tagging.rb@@ -773,6 +773,16 @@ module DiscourseTaggingsuccessful = existing.select { |t| !t.errors.present? }synonyms_ids = successful.map(&:id)TopicTag.where(topic_id: target_tag.topics.with_deleted, tag_id: synonyms_ids).delete_all+ TopicTag.joins(DB.sql_fragment(<<~SQL, synonyms_ids: synonyms_ids)).delete_all+ INNER JOIN (+ SELECT MIN(id) AS id, topic_id+ FROM topic_tags+ WHERE tag_id IN (:synonyms_ids)+ GROUP BY topic_id+ ) AS tt ON tt.id < topic_tags.id+ AND tt.topic_id = topic_tags.topic_id+ AND topic_tags.tag_id IN (:synonyms_ids)+ SQLTopicTag.where(tag_id: synonyms_ids).update_all(tag_id: target_tag.id)Scheduler::Defer.later "Update tag topic counts" doTag.ensure_consistency!diff --git a/spec/lib/discourse_tagging_spec.rb b/spec/lib/discourse_tagging_spec.rbindex c767e03a1eb..75f3779c825 100644--- a/spec/lib/discourse_tagging_spec.rb+++ b/spec/lib/discourse_tagging_spec.rb@@ -1552,6 +1552,19 @@ RSpec.describe DiscourseTagging doexpect(tag2.reload.target_tag).to eq(tag1)end+ it "replaces topic synonym tags with target tag" do+ tag4, tag5 = 2.times.collect { Fabricate(:tag) }+ topic = Fabricate(:topic, tags: [tag2, tag3, tag4])+ expect {+ expect(DiscourseTagging.add_or_create_synonyms_by_name(tag1, [tag2.name, tag3.name])).to eq(+ true,+ )+ }.to_not change { Tag.count }+ expect_same_tag_names(tag1.reload.synonyms, [tag2, tag3])+ expect_same_tag_names(topic.reload.tags, [tag1, tag4])+ expect(tag5.reload).to be_present+ end+it "can create new tags" doexpect {expect(DiscourseTagging.add_or_create_synonyms_by_name(tag1, ["synonym1"])).to eq(true)