Skip to content

A platform for community discussion. Free, open, simple.

FIX: csv import not being casing aware for tag groups (#35837)

Gabriel Grubba42a0ee088471
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 60fd0ce12fb..6a76f33d9cf 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -260,7 +260,8 @@ class TagsController < ::ApplicationController
if tag_group_name
tag_group =
- TagGroup.find_by(name: tag_group_name) || TagGroup.create!(name: tag_group_name)
+ TagGroup.find_by_name_insensitive(tag_group_name) ||
+ TagGroup.create!(name: tag_group_name)
tag.tag_groups << tag_group if tag.tag_groups.exclude?(tag_group)
end
end
diff --git a/app/models/tag_group.rb b/app/models/tag_group.rb
index d79d5270efb..07cdab6c6d7 100644
--- a/app/models/tag_group.rb
+++ b/app/models/tag_group.rb
@@ -53,6 +53,11 @@ class TagGroup < ActiveRecord::Base
nil
end
+ # Same as Tag#find_by_name
+ def self.find_by_name_insensitive(name)
+ self.find_by("lower(name) = ?", name.downcase)
+ end
+
def self.resolve_permissions(permissions)
permissions.map do |group, permission|
group_id = Group.group_id_from_param(group)
@@ -128,3 +133,7 @@ end
# parent_tag_id :integer
# one_per_topic :boolean default(FALSE)
#
+# Indexes
+#
+# index_tag_groups_on_lower_name (lower((name)::text)) UNIQUE
+#
diff --git a/db/migrate/20251107123438_add_index_to_tag_groups.rb b/db/migrate/20251107123438_add_index_to_tag_groups.rb
new file mode 100644
index 00000000000..43ad294063b
--- /dev/null
+++ b/db/migrate/20251107123438_add_index_to_tag_groups.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+class AddIndexToTagGroups < ActiveRecord::Migration[8.0]
+ def up
+ # Same from /db/migrate/20180928105835_add_index_to_tags.rb
+ execute <<~SQL
+ UPDATE tag_groups
+ SET name = name || id
+ WHERE EXISTS(SELECT * FROM tag_groups t WHERE lower(t.name) = lower(tag_groups.name) AND t.id < tag_groups.id)
+ SQL
+
+ add_index :tag_groups, "lower(name)", unique: true
+ end
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/spec/models/tag_group_spec.rb b/spec/models/tag_group_spec.rb
index 131d555d06f..c8be4eca242 100644
--- a/spec/models/tag_group_spec.rb
+++ b/spec/models/tag_group_spec.rb
@@ -118,6 +118,15 @@ RSpec.describe TagGroup do
end
end
+ describe "#find_by_name_insensitive" do
+ fab!(:tag_group) { Fabricate(:tag_group, name: "Test-Tag-Group") }
+
+ it "finds by name case insensitively" do
+ expect(TagGroup.find_by_name_insensitive("test-tag-group")).to eq(tag_group)
+ expect(TagGroup.find_by_name_insensitive("TEST-TAG-GROUP")).to eq(tag_group)
+ expect(TagGroup.find_by_name_insensitive("TeSt-TaG-GrOuP")).to eq(tag_group)
+ end
+ end
describe "tag_names=" do
let(:tag_group) { Fabricate(:tag_group) }
fab!(:tag)
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index c2a6dac6093..12fae108dc0 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -1322,6 +1322,47 @@ RSpec.describe TagsController do
expect(Tag.find_by_name("tag4").tag_groups.pluck(:name)).to contain_exactly("taggroup1")
end
+ it "does not fail if tags already exist" do
+ Fabricate(:tag, name: "TAG1")
+ Fabricate(:tag_group, name: "TAGGROUP1")
+
+ sign_in(moderator)
+
+ post "/tags/upload.json", params: { file: file, name: filename }
+
+ expect(response.status).to eq(200)
+ expect(Tag.pluck(:name)).to contain_exactly(
+ "TAG1",
+ "capitaltag2",
+ "spaced-tag",
+ "tag3",
+ "tag4",
+ )
+ expect(Tag.find_by_name("tag3").tag_groups.pluck(:name)).to contain_exactly("TAGGROUP1")
+ expect(Tag.find_by_name("tag4").tag_groups.pluck(:name)).to contain_exactly("TAGGROUP1")
+ end
+
+ describe "with `SiteSetting.force_lowercase_tags = false" do
+ before { SiteSetting.force_lowercase_tags = false }
+ it "does not fail if tags already exist" do
+ Fabricate(:tag, name: "tag1")
+ Fabricate(:tag, name: "CAPITALTAG2")
+ Fabricate(:tag, name: "tag3")
+ sign_in(moderator)
+
+ post "/tags/upload.json", params: { file: file, name: filename }
+
+ expect(response.status).to eq(200)
+ expect(Tag.pluck(:name)).to contain_exactly(
+ "tag1",
+ "CAPITALTAG2",
+ "spaced-tag",
+ "tag3",
+ "tag4",
+ )
+ end
+ end
+
it "fails gracefully with invalid input" do
sign_in(moderator)