From cddef4c485d18229d40e80bdfe6e2e4625b302a2 Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 4 Sep 2023 17:56:31 +0200 Subject: [PATCH] Fix language settings for users having selected the `kmr` language (#26787) --- .../20230904134623_fix_kmr_locale_settings.rb | 27 ++++++++++++++++ db/schema.rb | 2 +- lib/tasks/tests.rake | 31 ++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 db/post_migrate/20230904134623_fix_kmr_locale_settings.rb diff --git a/db/post_migrate/20230904134623_fix_kmr_locale_settings.rb b/db/post_migrate/20230904134623_fix_kmr_locale_settings.rb new file mode 100644 index 000000000..10e3f1da9 --- /dev/null +++ b/db/post_migrate/20230904134623_fix_kmr_locale_settings.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index c86106942..21eff86bf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/lib/tasks/tests.rake b/lib/tasks/tests.rake index ef4d46fe0..dbf140599 100644 --- a/lib/tasks/tests.rake +++ b/lib/tasks/tests.rake @@ -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());