Skip to content

A platform for community discussion. Free, open, simple.

FIX: Add missing solved_topics_auto_close_hours custom field entries (#42255)

Martin Brennan982fba1536cd
diff --git a/db/structure.sql b/db/structure.sql
index a3ca89b1716..e6f2aa9982f 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -23075,6 +23075,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20260803015314'),
('20260731055703'),
('20260729153343'),
('20260728162521'),
diff --git a/plugins/discourse-solved/db/migrate/20260803015314_populate_missing_solved_topics_auto_close_hours.rb b/plugins/discourse-solved/db/migrate/20260803015314_populate_missing_solved_topics_auto_close_hours.rb
new file mode 100644
index 00000000000..397f2c8f68c
--- /dev/null
+++ b/plugins/discourse-solved/db/migrate/20260803015314_populate_missing_solved_topics_auto_close_hours.rb
@@ -0,0 +1,64 @@
+# frozen_string_literal: true
+
+class PopulateMissingSolvedTopicsAutoCloseHours < ActiveRecord::Migration[8.0]
+ def up
+ return if !Migration::Helpers.existing_site?
+
+ execute <<~SQL
+ UPDATE category_custom_fields auto_close
+ SET value = '0', updated_at = NOW()
+ WHERE auto_close.name = 'solved_topics_auto_close_hours'
+ AND auto_close.value IS NULL
+ AND EXISTS (
+ SELECT 1
+ FROM categories
+ WHERE categories.id = auto_close.category_id
+ AND (
+ EXISTS (
+ SELECT 1
+ FROM site_settings
+ WHERE name = 'allow_solved_on_all_topics' AND value = 't'
+ )
+ OR EXISTS (
+ SELECT 1
+ FROM category_custom_fields enabled_solved
+ WHERE enabled_solved.category_id = categories.id
+ AND enabled_solved.name = 'enable_accepted_answers'
+ AND enabled_solved.value = 'true'
+ )
+ )
+ )
+ SQL
+
+ execute <<~SQL
+ INSERT INTO category_custom_fields (category_id, name, value, created_at, updated_at)
+ SELECT categories.id, 'solved_topics_auto_close_hours', '0', NOW(), NOW()
+ FROM categories
+ WHERE
+ (
+ EXISTS (
+ SELECT 1
+ FROM site_settings
+ WHERE name = 'allow_solved_on_all_topics' AND value = 't'
+ )
+ OR EXISTS (
+ SELECT 1
+ FROM category_custom_fields enabled_solved
+ WHERE enabled_solved.category_id = categories.id
+ AND enabled_solved.name = 'enable_accepted_answers'
+ AND enabled_solved.value = 'true'
+ )
+ )
+ AND NOT EXISTS (
+ SELECT 1
+ FROM category_custom_fields existing
+ WHERE existing.category_id = categories.id
+ AND existing.name = 'solved_topics_auto_close_hours'
+ )
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-solved/spec/db/migrate/20260803015314_populate_missing_solved_topics_auto_close_hours_spec.rb b/plugins/discourse-solved/spec/db/migrate/20260803015314_populate_missing_solved_topics_auto_close_hours_spec.rb
new file mode 100644
index 00000000000..1a717db85ac
--- /dev/null
+++ b/plugins/discourse-solved/spec/db/migrate/20260803015314_populate_missing_solved_topics_auto_close_hours_spec.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+require Rails.root.join(
+ "plugins/discourse-solved/db/migrate/20260803015314_populate_missing_solved_topics_auto_close_hours.rb",
+ )
+
+RSpec.describe PopulateMissingSolvedTopicsAutoCloseHours do
+ before do
+ @original_verbose = ActiveRecord::Migration.verbose
+ ActiveRecord::Migration.verbose = false
+ end
+
+ after do
+ ActiveRecord::Migration.verbose = @original_verbose
+ Discourse.clear_site_creation_date_cache
+ end
+
+ it "does not create custom fields on fresh installs" do
+ category = Fabricate(:category)
+ category.upsert_custom_fields("enable_accepted_answers" => "true")
+ DB.exec("UPDATE schema_migration_details SET created_at = NOW()")
+ Discourse.clear_site_creation_date_cache
+
+ described_class.new.up
+
+ expect(CategoryCustomField.exists?(category:, name: "solved_topics_auto_close_hours")).to eq(
+ false,
+ )
+ end
+
+ context "when the site is an existing install" do
+ before do
+ DB.exec("UPDATE schema_migration_details SET created_at = NOW() - INTERVAL '2 hours'")
+ Discourse.clear_site_creation_date_cache
+ end
+
+ it "sets missing fields to zero only for solved categories and preserves existing values" do
+ solved_category = Fabricate(:category)
+ solved_category.upsert_custom_fields("enable_accepted_answers" => "true")
+ configured_category = Fabricate(:category)
+ configured_category.upsert_custom_fields(
+ "enable_accepted_answers" => "true",
+ "solved_topics_auto_close_hours" => "72",
+ )
+ null_category = Fabricate(:category)
+ null_category.upsert_custom_fields("enable_accepted_answers" => "true")
+ CategoryCustomField.create!(
+ category: null_category,
+ name: "solved_topics_auto_close_hours",
+ value: nil,
+ )
+ regular_category = Fabricate(:category)
+
+ 2.times { described_class.new.up }
+
+ expect(
+ CategoryCustomField.where(
+ category: [solved_category, configured_category, null_category, regular_category],
+ name: "solved_topics_auto_close_hours",
+ ).pluck(:category_id, :value),
+ ).to contain_exactly(
+ [solved_category.id, "0"],
+ [configured_category.id, "72"],
+ [null_category.id, "0"],
+ )
+ end
+
+ it "sets missing fields to zero for every category when solved is allowed globally" do
+ DB.exec(<<~SQL, data_type: SiteSettings::TypeSupervisor.types[:bool])
+ INSERT INTO site_settings (name, data_type, value, created_at, updated_at)
+ VALUES ('allow_solved_on_all_topics', :data_type, 't', NOW(), NOW())
+ ON CONFLICT (name) DO UPDATE SET value = EXCLUDED.value
+ SQL
+ first_category = Fabricate(:category)
+ second_category = Fabricate(:category)
+ second_category.upsert_custom_fields("solved_topics_auto_close_hours" => "24")
+ null_category = Fabricate(:category)
+ CategoryCustomField.create!(
+ category: null_category,
+ name: "solved_topics_auto_close_hours",
+ value: nil,
+ )
+
+ described_class.new.up
+
+ expect(
+ CategoryCustomField.where(
+ category: [first_category, second_category, null_category],
+ name: "solved_topics_auto_close_hours",
+ ).pluck(:category_id, :value),
+ ).to contain_exactly(
+ [first_category.id, "0"],
+ [second_category.id, "24"],
+ [null_category.id, "0"],
+ )
+ end
+ end
+end