A platform for community discussion. Free, open, simple.
DEV: Reset category settings that point at missing categories
Régis Hanol0e0d7b515f6c
diff --git a/app/jobs/scheduled/ensure_db_consistency.rb b/app/jobs/scheduled/ensure_db_consistency.rbindex d4da9a4ded4..7803dee8d80 100644--- a/app/jobs/scheduled/ensure_db_consistency.rb+++ b/app/jobs/scheduled/ensure_db_consistency.rb@@ -31,6 +31,7 @@ module Jobs::UserAction,::UserStat,::GroupUser,+ ::SiteSetting,].each do |klass|measure_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)diff --git a/app/models/site_setting.rb b/app/models/site_setting.rbindex b7a22d5d8f3..b1e1938c820 100644--- a/app/models/site_setting.rb+++ b/app/models/site_setting.rb@@ -145,6 +145,33 @@ class SiteSetting < ActiveRecord::Basesetup_deprecated_methodsclient_settings << :available_locales+ SEEDED_CATEGORY_SETTINGS = %i[+ general_category_id+ meta_category_id+ staff_category_id+ uncategorized_category_id+ ]++ def self.ensure_consistency!+ current.each do |name, value|+ next if !dangling_category?(name, value)++ reason = I18n.t("staff_action_logs.site_setting.missing_category_reset", category_id: value)+ set_and_log(name, defaults[name], Discourse.system_user, reason)+ if name.to_sym == :uncategorized_category_id+ set_and_log(:allow_uncategorized_topics, false, Discourse.system_user, reason)+ end+ end+ end++ def self.dangling_category?(name, value)+ type = type_supervisor.get_type(name)+ return false if type != :category && !SEEDED_CATEGORY_SETTINGS.include?(name.to_sym)++ value.to_i > 0 && !Category.exists?(value.to_i)+ end+ private_class_method :dangling_category?+def self.available_localesLocaleSiteSetting.values.to_jsonenddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 8f3c2ac6408..3b9627af2fd 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -6368,6 +6368,7 @@ en:unknown: "unknown"site_setting:update_existing_users: "update existing users? %{value}"+ missing_category_reset: "Reset to its default because the category it pointed to (id %{category_id}) no longer exists."user_merged: "%{username} was merged into this account"user_delete_self: "Deleted by self from %{url}"webhook_deactivation_reason: "Your webhook has been automatically deactivated. We received multiple '%{status}' HTTP status failure responses."diff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rbindex c09ccf54be5..53b408dc261 100644--- a/spec/models/site_setting_spec.rb+++ b/spec/models/site_setting_spec.rb@@ -665,4 +665,33 @@ RSpec.describe SiteSetting doexpect(SiteSetting.content_localization_locales).to eq(["en"])endend++ describe ".ensure_consistency!" do+ it "resets category settings whose category no longer exists" do+ SiteSetting.default_composer_category = "999999"+ SiteSetting.general_category_id = 999_999+ SiteSetting.max_topics_per_day = 999++ SiteSetting.ensure_consistency!++ expect(SiteSetting.default_composer_category).to eq("")+ expect(SiteSetting.general_category_id).to eq(-1)+ expect(SiteSetting.max_topics_per_day).to eq(999)+ expect(SiteSetting.uncategorized_category_id).not_to eq(-1)+ expect(UserHistory.where(subject: "default_composer_category").last.details).to include(+ "(id 999999) no longer exists",+ )+ end++ it "disallows uncategorized topics when the uncategorized category no longer exists" do+ SiteSetting.uncategorized_category_id = 999_999++ SiteSetting.ensure_consistency!++ expect(SiteSetting.uncategorized_category_id).to eq(+ SiteSetting.defaults[:uncategorized_category_id],+ )+ expect(SiteSetting.allow_uncategorized_topics).to eq(false)+ end+ endend