From 02c94988600c35598f18795919b01c46f6fd519b Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 20 Jan 2022 00:09:52 -0600 Subject: [PATCH] artists: normalize group names. Normalize artist group names following the same rules as artist other names. This means artist group names now use underscores instead of spaces. It also means extra space characters at the beginning and end of names is stripped, and Unicode characters are normalized. Fixes #4647, which was caused by users accidentally replacing group names with a single space character when trying to remove a group. --- app/models/artist.rb | 1 + .../fixes/092_normalize_artist_group_names.rb | 17 +++++++++++++++++ test/unit/artist_test.rb | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100755 script/fixes/092_normalize_artist_group_names.rb diff --git a/app/models/artist.rb b/app/models/artist.rb index d11cf46d4..ef6e6e2f1 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -9,6 +9,7 @@ class Artist < ApplicationRecord deletable normalize :name, :normalize_name + normalize :group_name, :normalize_other_name normalize :other_names, :normalize_other_names array_attribute :other_names # XXX must come after `normalize :other_names` diff --git a/script/fixes/092_normalize_artist_group_names.rb b/script/fixes/092_normalize_artist_group_names.rb new file mode 100755 index 000000000..13868a4a8 --- /dev/null +++ b/script/fixes/092_normalize_artist_group_names.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +require_relative "base" + +with_confirmation do + CurrentUser.scoped(User.system) do + Artist.where.not(group_name: "").find_each do |artist| + artist.update!(group_name: artist.group_name) # forces normalization + + if artist.saved_changes? + puts "id=#{artist.id} name=#{artist.name} oldgroup=`#{artist.group_name_before_last_save}` newgroup=`#{artist.group_name}`" + end + rescue ActiveRecord::RecordInvalid + puts "id=#{artist.id} name=#{artist.name} error=#{artist.errors.full_messages.join}" + end + end +end diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 1b0d91b17..398667aa9 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -412,6 +412,22 @@ class ArtistTest < ActiveSupport::TestCase should normalize_attribute(:other_names).from("_foo_ Bar").to(["_foo_", "Bar"]) end + context "group name" do + should normalize_attribute(:group_name).from(" ").to("") + should normalize_attribute(:group_name).from(" foo").to("foo") + should normalize_attribute(:group_name).from("foo ").to("foo") + should normalize_attribute(:group_name).from("___foo").to("___foo") + should normalize_attribute(:group_name).from("foo___").to("foo___") + should normalize_attribute(:group_name).from("foo\n").to("foo") + should normalize_attribute(:group_name).from("foo bar").to("foo_bar") + should normalize_attribute(:group_name).from("foo bar").to("foo_bar") + should normalize_attribute(:group_name).from("foo___bar").to("foo___bar") + should normalize_attribute(:group_name).from(" _Foo Bar_ ").to("_Foo_Bar_") + should normalize_attribute(:group_name).from("_foo_ Bar").to("_foo__Bar") + should normalize_attribute(:group_name).from("pokémon".unicode_normalize(:nfd)).to("pokémon".unicode_normalize(:nfkc)) + should normalize_attribute(:group_name).from("🏳️‍🌈").to("🏳️‍🌈") + end + should "search on its name should return results" do artist = FactoryBot.create(:artist, :name => "artist")