2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-10-03 17:21:20 +08:00

FIX: Prevent saving empty string as a locale (#33481)

Post edits need to inform the backend if the locale is changed and is
sometimes set as `""`. This PR prevents the locale from being set to
`""` in the database, and includes a migration to purge the empty
strings.
This commit is contained in:
Natalie Tay 2025-07-04 17:20:42 +08:00 committed by GitHub
parent 4c7089f817
commit 75330efab0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 55 additions and 1 deletions

View file

@ -59,7 +59,7 @@ export default class PostLanguageSelector extends Component {
<dropdown.item> <dropdown.item>
<DButton <DButton
@label="post.localizations.post_language_selector.none" @label="post.localizations.post_language_selector.none"
@action={{fn this.selectPostLanguage ""}} @action={{fn this.selectPostLanguage null}}
/> />
</dropdown.item> </dropdown.item>
</DropdownMenu> </DropdownMenu>

View file

@ -888,6 +888,8 @@ class Post < ActiveRecord::Base
self.baked_at = Time.zone.now self.baked_at = Time.zone.now
self.baked_version = BAKED_VERSION self.baked_version = BAKED_VERSION
end end

self.locale = nil if locale.blank?
end end


def advance_draft_sequence def advance_draft_sequence

View file

@ -415,6 +415,8 @@ class Topic < ActiveRecord::Base
inherit_auto_close_from_category inherit_auto_close_from_category
inherit_slow_mode_from_category inherit_slow_mode_from_category
end end

self.locale = nil if locale.blank?
end end


after_save do after_save do

View file

@ -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

View file

@ -2409,4 +2409,15 @@ RSpec.describe Post do
expect(post.in_user_locale?).to eq(false) expect(post.in_user_locale?).to eq(false)
end end
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 end

View file

@ -3332,6 +3332,17 @@ describe Topic do
end end
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 describe "#after_update" do
fab!(:topic) { Fabricate(:topic, user: user) } fab!(:topic) { Fabricate(:topic, user: user) }
fab!(:category) { Fabricate(:category_with_definition, read_restricted: true) } fab!(:category) { Fabricate(:category_with_definition, read_restricted: true) }