2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-10-03 17:21:20 +08:00
discourse/db/migrate/20250704072726_nullify_blank_locales.rb
Natalie Tay 75330efab0
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.
2025-07-04 17:20:42 +08:00

28 lines
627 B
Ruby

# 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