From 09eca87c7643ea1da102d18295bfbbc46be9af16 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Tue, 17 Oct 2023 10:53:02 +1100 Subject: [PATCH] FIX: synonym tags are not considered as unused (#23950) Currently, `Tag.unused` scope is used to delete unused tags on `/tags` and by CleanUpTags job. Synonym tags, should not be included and treated as unused. Synonyms are only deleted when main tag is deleted: https://github.com/discourse/discourse/blob/main/app/models/tag.rb#L57 --- app/models/tag.rb | 2 +- spec/models/tag_spec.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index a8205d0e88b..f33b6a47ba1 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -30,7 +30,7 @@ class Tag < ActiveRecord::Base # tags that have never been used and don't belong to a tag group scope :unused, -> { - where(staff_topic_count: 0, pm_topic_count: 0).joins( + where(staff_topic_count: 0, pm_topic_count: 0, target_tag_id: nil).joins( "LEFT JOIN tag_group_memberships tgm ON tags.id = tgm.tag_id", ).where("tgm.tag_id IS NULL") } diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index c1100d725e4..4437771c7b9 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -299,6 +299,7 @@ RSpec.describe Tag do ) end let!(:tag_group) { Fabricate(:tag_group, tag_names: [tag_in_group.name]) } + let!(:synonym_tag) { Fabricate(:tag, target_tag: tags.first) } it "returns the correct tags" do expect(Tag.unused.pluck(:name)).to contain_exactly("unused1", "unused2")