Skip to content

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

FIX: Ensure each flag gets a unique `name_key` (#40899)

Régis Hanol41db2f76f3a4
diff --git a/app/models/flag.rb b/app/models/flag.rb
index 28b1adffb8c..d5a1c6333e0 100644
--- a/app/models/flag.rb
+++ b/app/models/flag.rb
@@ -66,8 +66,21 @@ class Flag < ActiveRecord::Base
end
def set_name_key
+ return if name_key.present? && !will_save_change_to_name?
+
prefix = system? ? "" : "custom_"
- self.name_key = "#{prefix}#{name.squeeze(" ").gsub(" ", "_").gsub(/[^\w]/, "").downcase}"
+ slug = name.squeeze(" ").gsub(" ", "_").gsub(/[^\w]/, "").downcase
+ base = slug.present? ? "#{prefix}#{slug}" : "#{prefix}flag"
+
+ suffix = 1
+ candidate = base
+
+ while Flag.unscoped.where(name_key: candidate).where.not(id: id).exists?
+ suffix += 1
+ candidate = "#{base}_#{suffix}"
+ end
+
+ self.name_key = candidate
end
end
@@ -89,3 +102,7 @@ end
# created_at :datetime not null
# updated_at :datetime not null
#
+# Indexes
+#
+# index_flags_on_name_key (name_key) UNIQUE
+#
diff --git a/db/post_migrate/20260624140945_ensure_unique_flag_name_keys.rb b/db/post_migrate/20260624140945_ensure_unique_flag_name_keys.rb
new file mode 100644
index 00000000000..9a25a7bd927
--- /dev/null
+++ b/db/post_migrate/20260624140945_ensure_unique_flag_name_keys.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class EnsureUniqueFlagNameKeys < ActiveRecord::Migration[8.0]
+ def up
+ execute(<<~SQL)
+ UPDATE flags
+ SET name_key = name_key || '_' || id
+ WHERE name_key IS NOT NULL
+ AND id <> (
+ SELECT MIN(other.id)
+ FROM flags other
+ WHERE other.name_key = flags.name_key
+ )
+ SQL
+
+ add_index :flags, :name_key, unique: true, if_not_exists: true
+ end
+
+ def down
+ remove_index :flags, :name_key, if_exists: true
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index 1a0cf69085e..eede6c99f87 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -18786,6 +18786,13 @@ CREATE UNIQUE INDEX index_external_upload_stubs_on_key ON public.external_upload
CREATE INDEX index_external_upload_stubs_on_status ON public.external_upload_stubs USING btree (status);
+--
+-- Name: index_flags_on_name_key; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX index_flags_on_name_key ON public.flags USING btree (name_key);
+
+
--
-- Name: index_for_rebake_old; Type: INDEX; Schema: public; Owner: -
--
@@ -22057,6 +22064,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20260624140945'),
('20260617053237'),
('20260615084100'),
('20260615082047'),
diff --git a/spec/migrations/ensure_unique_flag_name_keys_spec.rb b/spec/migrations/ensure_unique_flag_name_keys_spec.rb
new file mode 100644
index 00000000000..cd5ae792bd5
--- /dev/null
+++ b/spec/migrations/ensure_unique_flag_name_keys_spec.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+require Rails.root.join("db/post_migrate/20260624140945_ensure_unique_flag_name_keys.rb")
+
+RSpec.describe EnsureUniqueFlagNameKeys do
+ subject(:migrate) { described_class.new.up }
+
+ before do
+ @verbose = ActiveRecord::Migration.verbose
+ ActiveRecord::Migration.verbose = false
+ end
+
+ after { ActiveRecord::Migration.verbose = @verbose }
+
+ it "disambiguates duplicate name_keys and enforces uniqueness" do
+ flag1 = Fabricate(:flag, name: "alpha")
+ flag2 = Fabricate(:flag, name: "beta")
+ flag3 = Fabricate(:flag, name: "gamma")
+
+ ActiveRecord::Base.connection.remove_index(:flags, :name_key, if_exists: true)
+ Flag.unscoped.where(id: [flag1.id, flag2.id, flag3.id]).update_all(name_key: "custom_")
+
+ migrate
+
+ keys = Flag.unscoped.where(id: [flag1.id, flag2.id, flag3.id]).order(:id).pluck(:name_key)
+
+ expect(keys.first).to eq("custom_")
+ expect(keys[1]).to eq("custom__#{flag2.id}")
+ expect(keys[2]).to eq("custom__#{flag3.id}")
+ expect(keys.uniq.size).to eq(3)
+
+ expect(ActiveRecord::Base.connection.index_exists?(:flags, :name_key, unique: true)).to eq(true)
+ end
+
+ it "leaves already-unique name_keys untouched" do
+ flag = Fabricate(:flag, name: "unique flag")
+ original = flag.name_key
+
+ migrate
+
+ expect(flag.reload.name_key).to eq(original)
+ end
+end
diff --git a/spec/models/flag_spec.rb b/spec/models/flag_spec.rb
index f7bc5840ed9..430b12aa7cc 100644
--- a/spec/models/flag_spec.rb
+++ b/spec/models/flag_spec.rb
@@ -29,6 +29,38 @@ RSpec.describe Flag, type: :model do
flag.destroy!
end
+ it "generates a unique, non-empty name key for non-ASCII names" do
+ flag1 = Fabricate(:flag, name: "测试举报一")
+ flag2 = Fabricate(:flag, name: "测试举报二")
+ flag3 = Fabricate(:flag, name: "测试举报三")
+
+ keys = [flag1, flag2, flag3].map(&:name_key)
+ expect(keys).to eq(%w[custom_flag custom_flag_2 custom_flag_3])
+ expect(keys.uniq.size).to eq(3)
+
+ [flag1, flag2, flag3].each(&:destroy!)
+ end
+
+ it "generates a unique name key when distinct names normalize identically" do
+ flag1 = Fabricate(:flag, name: "Spam!")
+ flag2 = Fabricate(:flag, name: "Spam?")
+
+ expect(flag1.name_key).to eq("custom_spam")
+ expect(flag2.name_key).to eq("custom_spam_2")
+
+ [flag1, flag2].each(&:destroy!)
+ end
+
+ it "keeps the name key stable when the name does not change" do
+ flag = Fabricate(:flag, name: "测试举报一")
+ expect(flag.name_key).to eq("custom_flag")
+
+ flag.update!(enabled: false)
+ expect(flag.reload.name_key).to eq("custom_flag")
+
+ flag.destroy!
+ end
+
it "updates post action types when created, modified or destroyed" do
expect(PostActionType.flag_types.keys).to eq(
%i[notify_user off_topic inappropriate spam illegal notify_moderators],