Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix language settings for users having selected the kmr language #26787

Merged
merged 1 commit into from Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion db/schema.rb
Expand Up @@ -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"

Expand Down
31 changes: 30 additions & 1 deletion lib/tasks/tests.rake
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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());
Expand Down