태그/태그 그룹 CSV 가져오기는 대소문자를 구별해야 하지만 실제로는 구별하지 않음

Reproduction:

  • Have a tag called ABC
  • Create a CSV with tag abc
  • Go to /tags and upload CSV
  • See it fail with “Sorry, there was an error uploading foo.csv. Please try again.”

This is happening because tags are case aware but they must be unique ignoring the case.

If you have a file with 1000 tags it is quite a job to figure out where it goes wrong.

The code does this

tag = Tag.find_by_name(tag_name) || Tag.create!(name: tag_name)
...
tag_group = TagGroup.find_by(name: tag_group_name) || TagGroup.create!(name: tag_group_name)

which is a bad pattern, since find_by_name is case sensitive and create! is not [1] so it errors out with ActiveRecord::RecordInvalid : Validation failed: Name has already been taken

This should be something like

tag = Tag.where('name ILIKE ?', tag_name).first || Tag.create!(name: tag_name)


  1. because validates :name, presence: true, uniqueness: { case_sensitive: false } ↩︎

4개의 좋아요

Thanks for reporting, put a pr-welcome on this, I guess we need to change that find_by_name to do a case insensitive lookup.

Given we have:

The fix would be:

tag = Tag.where('lower(name) = ?', tag_name.downcase).first

TagGroup is missing the index, so we would need to add it and then do the same there.

안녕하세요 @RGJ

일반 태그로는 해당 문제를 재현할 수 없었지만, 태그 그룹에서는 말씀하신 오류가 발생했습니다.

제 샘플 CSV는 다음과 같습니다:

ABC,TAGGROUP

ABC는 정상적으로 작동하지만, tag_group을 사용하면 오류가 발생합니다. 샘플 CSV를 공유해 주시면 기꺼이 도움을 드리겠습니다!

태그 그룹 문제를 수정하는 PR을 만들었습니다. 또한, 포럼에서 SiteSetting.force_lowercase_tags가 활성화되어 있나요?

1개의 좋아요

아니요, 체크가 해제되어 있습니다. 죄송합니다, 이제 그것이 중요하다는 것을 알겠습니다.

어떻게 재현하려는 것인지 확실하지 않지만, CSV에는 소문자 abc가 포함되어야 하고 force_lowercase_tags는 체크 해제 상태여야 합니다.

1개의 좋아요

걱정 마세요. 제가 코드를 파헤치다 발견한 사이트 설정이었는데, 중요할 수 있거든요!

그 CSV는 태그 그룹이 없는 abc만 있는 건가요?

우리의 테스트 예시는 다음과 같습니다(대문자 capitaltag2를 전부 대문자로 설정하여 로컬에서 테스트해 보았지만, 예상대로 잘 작동했습니다):

실제로 태그만으로는 재현할 수 없습니다.

OP에서 작은 실수를 했습니다:

Tag.find_by_name는 대소문자를 구분하지 않습니다(좋습니다). 반면 Tag.find_by(name: )는 대소문자를 구분합니다(나쁩니다).
이것은 David가 7년 전 find_by_name 메서드를 오버라이드하여 태그에 대해 이미 수정했기 때문입니다.

TagGroup의 경우 이 수정이 적용된 적이 없습니다.

2개의 좋아요