diff --git a/app/assets/javascripts/discourse/app/components/post-language-selector.gjs b/app/assets/javascripts/discourse/app/components/post-language-selector.gjs index 537d4424f4e..749249007fa 100644 --- a/app/assets/javascripts/discourse/app/components/post-language-selector.gjs +++ b/app/assets/javascripts/discourse/app/components/post-language-selector.gjs @@ -59,7 +59,7 @@ export default class PostLanguageSelector extends Component { diff --git a/app/models/post.rb b/app/models/post.rb index ffff3be4d32..e6b503e2a4f 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -888,6 +888,8 @@ class Post < ActiveRecord::Base self.baked_at = Time.zone.now self.baked_version = BAKED_VERSION end + + self.locale = nil if locale.blank? end def advance_draft_sequence diff --git a/app/models/topic.rb b/app/models/topic.rb index 310521256ff..27eb3a5b11c 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -415,6 +415,8 @@ class Topic < ActiveRecord::Base inherit_auto_close_from_category inherit_slow_mode_from_category end + + self.locale = nil if locale.blank? end after_save do diff --git a/db/migrate/20250704072726_nullify_blank_locales.rb b/db/migrate/20250704072726_nullify_blank_locales.rb new file mode 100644 index 00000000000..475c9e735bd --- /dev/null +++ b/db/migrate/20250704072726_nullify_blank_locales.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true +class NullifyBlankLocales < ActiveRecord::Migration[7.2] + def up + loop do + result = execute(<<~SQL) + UPDATE topics + SET locale = NULL + WHERE id IN (SELECT id FROM topics WHERE locale = '' LIMIT 5000) + SQL + + break if result.cmd_tuples == 0 + end + + loop do + result = execute(<<~SQL) + UPDATE posts + SET locale = NULL + WHERE id IN (SELECT id FROM posts WHERE locale = '' LIMIT 5000) + SQL + + break if result.cmd_tuples == 0 + end + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index b035fc3a133..6216de42e8d 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -2409,4 +2409,15 @@ RSpec.describe Post do expect(post.in_user_locale?).to eq(false) end end + + describe "#before_save" do + it "replaces empty locales with nil" do + post = Fabricate(:post, locale: "en") + + post.locale = "" + post.save! + + expect(post.reload.locale).to eq(nil) + end + end end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 0b2326fc45b..2974f3d94da 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -3332,6 +3332,17 @@ describe Topic do end end + describe "#before_save" do + it "replaces empty locales with nil" do + topic = Fabricate(:topic, locale: "en") + + topic.locale = "" + topic.save! + + expect(topic.reload.locale).to eq(nil) + end + end + describe "#after_update" do fab!(:topic) { Fabricate(:topic, user: user) } fab!(:category) { Fabricate(:category_with_definition, read_restricted: true) }