diff --git a/lib/data_update_scripts/20211115154021_nullify_invalid_tag_fields.rb b/lib/data_update_scripts/20211115154021_nullify_invalid_tag_fields.rb new file mode 100644 index 000000000..ecc08dbef --- /dev/null +++ b/lib/data_update_scripts/20211115154021_nullify_invalid_tag_fields.rb @@ -0,0 +1,34 @@ +module DataUpdateScripts + class NullifyInvalidTagFields + def run + Tag.where(bg_color_hex: "").or(Tag.where(text_color_hex: "")).find_each do |tag| + # presence here selects non-empty strings if set (avoiding unsetting valid values) + # but nullifies empty tag colors + tag.update_columns( + bg_color_hex: tag.bg_color_hex.presence, + text_color_hex: tag.text_color_hex.presence, + ) + end + + # normalize empty string to nil (alias_for? behaved correctly already), this is just cleanup + Tag.where(alias_for: "").update(alias_for: nil) + + Tag.where.not(alias_for: nil).find_each do |aliased_tag| + # this is expected to not do anything - based on this query in blazer giving 0 rows + # (there are 64 with aliases, all valid) + + # SELECT tags.alias_for, t2.name FROM tags + # LEFT JOIN tags AS t2 ON tags.alias_for = t2.name + # WHERE tags.alias_for IS NOT NULL + # AND tags.alias_for != '' + # AND t2.name IS NULL; + + aliased_tag.validate + + if aliased_tag.errors.any? { |e| e.type == "alias_for must refer to an existing tag" } + aliased_tag.update_column(:alias_for, nil) + end + end + end + end +end diff --git a/spec/lib/data_update_scripts/nullify_invalid_tag_fields_spec.rb b/spec/lib/data_update_scripts/nullify_invalid_tag_fields_spec.rb new file mode 100644 index 000000000..f28409c0c --- /dev/null +++ b/spec/lib/data_update_scripts/nullify_invalid_tag_fields_spec.rb @@ -0,0 +1,53 @@ +require "rails_helper" +require Rails.root.join( + "lib/data_update_scripts/20211115154021_nullify_invalid_tag_fields.rb", +) + +describe DataUpdateScripts::NullifyInvalidTagFields do + let(:tag) { create(:tag) } + + it "nullifies empty background color" do + tag.update_column(:bg_color_hex, "") + + described_class.new.run + + expect(tag.reload.bg_color_hex).to be_nil + expect(tag.valid?).to be true + end + + it "nullifies empty text foreground color" do + tag.update_column(:text_color_hex, "") + + described_class.new.run + + expect(tag.reload.text_color_hex).to be_nil + expect(tag.valid?).to be true + end + + it "nullifies when both colors empty" do + tag.update_columns(bg_color_hex: "", text_color_hex: "") + + described_class.new.run + + expect(tag.reload.text_color_hex).to be_nil + expect(tag.valid?).to be true + end + + it "nullifies invalid alias_for values" do + tag.update_columns(alias_for: "this-is-not-a-tag-name") + + described_class.new.run + + expect(tag.reload.alias_for).to be_nil + expect(tag.valid?).to be true + end + + it "nullifies empty string alias_for values" do + tag.update(alias_for: "") + + described_class.new.run + + expect(tag.reload.alias_for).to be_nil + expect(tag.valid?).to be true + end +end