Fix language settings for users having selected the `kmr` language (#26787)

This commit is contained in:
Claire 2023-09-04 17:56:31 +02:00 committed by GitHub
parent ece1ff77d6
commit cddef4c485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 2 deletions

View File

@ -0,0 +1,27 @@
# frozen_string_literal: true
class FixKmrLocaleSettings < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
class MigrationUser < ApplicationRecord
self.table_name = :users
end
def up
MigrationUser.reset_column_information
MigrationUser.where.not(settings: [nil, '{}']).find_each do |user|
user_settings = Oj.load(user.settings)
next unless user_settings['default_language'] == 'kmr'
user_settings['default_language'] = 'ku'
user.update!(settings: Oj.dump(user_settings))
end
MigrationUser.where.not(chosen_languages: nil).where('chosen_languages && ?', '{kmr}').find_each do |user|
user.update!(chosen_languages: user.chosen_languages.map { |lang| lang == 'kmr' ? 'ku' : lang }.uniq)
end
end
def down; end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2023_08_22_081029) do
ActiveRecord::Schema[7.0].define(version: 2023_09_04_134623) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

View File

@ -68,10 +68,24 @@ namespace :tests do
puts 'Preview cards not deduplicated as expected'
exit(1)
end
unless Account.find_local('kmruser').user.chosen_languages == %w(en ku ckb)
puts 'Chosen languages not migrated as expected for kmr users'
exit(1)
end
unless Account.find_local('kmruser').user.settings['default_language'] == 'ku'
puts 'Default posting language not migrated as expected for kmr users'
exit(1)
end
end
desc 'Populate the database with test data for 2.4.3'
task populate_v2_4_3: :environment do # rubocop:disable Naming/VariableNumber
user_key = OpenSSL::PKey::RSA.new(2048)
user_private_key = ActiveRecord::Base.connection.quote(user_key.to_pem)
user_public_key = ActiveRecord::Base.connection.quote(user_key.public_key.to_pem)
ActiveRecord::Base.connection.execute(<<~SQL)
INSERT INTO "custom_filters"
(id, account_id, phrase, context, whole_word, irreversible, created_at, updated_at)
@ -118,6 +132,21 @@ namespace :tests do
(id, thing_type, thing_id, var, value, created_at, updated_at)
VALUES
(3, 'User', 1, 'notification_emails', E'--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\nfollow: false\nreblog: true\nfavourite: true\nmention: false\nfollow_request: true\ndigest: true\nreport: true\npending_account: false\ntrending_tag: true\nappeal: true\n', now(), now());
INSERT INTO "accounts"
(id, username, domain, private_key, public_key, created_at, updated_at)
VALUES
(10, 'kmruser', NULL, #{user_private_key}, #{user_public_key}, now(), now());
INSERT INTO "users"
(id, account_id, email, created_at, updated_at, admin, locale, chosen_languages)
VALUES
(4, 10, 'kmruser@localhost', now(), now(), false, 'ku', '{en,kmr,ku,ckb}');
INSERT INTO "settings"
(id, thing_type, thing_id, var, value, created_at, updated_at)
VALUES
(4, 'User', 4, 'default_language', E'--- kmr\n', now(), now());
SQL
end
@ -197,7 +226,7 @@ namespace :tests do
INSERT INTO "users"
(id, account_id, email, created_at, updated_at, admin, locale)
VALUES
(3, 7, 'ptuser@localhost', now(), now(), false, 'pt');
(3, 8, 'ptuser@localhost', now(), now(), false, 'pt');
-- conversations
INSERT INTO "conversations" (id, created_at, updated_at) VALUES (1, now(), now());