Skip to content

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

FIX: Allow permanently deleting the About topic of a deleted category (#44132)

Régis Hanold60921cba576
diff --git a/app/models/category.rb b/app/models/category.rb
index 6e15bf7dca4..a777943c8d1 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -648,7 +648,11 @@ class Category < ActiveRecord::Base
end
def trash_category_definition
- topic&.trash!
+ return if topic.blank?
+
+ topic.first_post&.trash!
+ topic.trash!
+ Topic.reset_highest(topic.id)
end
def clear_related_site_settings
diff --git a/db/post_migrate/20260925155026_trash_first_post_of_trashed_topics_in_deleted_categories.rb b/db/post_migrate/20260925155026_trash_first_post_of_trashed_topics_in_deleted_categories.rb
new file mode 100644
index 00000000000..b3fb8dc8756
--- /dev/null
+++ b/db/post_migrate/20260925155026_trash_first_post_of_trashed_topics_in_deleted_categories.rb
@@ -0,0 +1,37 @@
+# frozen_string_literal: true
+class TrashFirstPostOfTrashedTopicsInDeletedCategories < ActiveRecord::Migration[8.1]
+ SMALL_ACTION_POST_TYPE = 3
+ WHISPER_POST_TYPE = 4
+
+ def up
+ execute <<~SQL
+ WITH trashed_first_posts AS (
+ UPDATE posts
+ SET deleted_at = topics.deleted_at, deleted_by_id = topics.deleted_by_id
+ FROM topics
+ WHERE posts.topic_id = topics.id
+ AND posts.post_number = 1
+ AND posts.deleted_at IS NULL
+ AND topics.deleted_at IS NOT NULL
+ AND topics.category_id IS NOT NULL
+ AND NOT EXISTS (SELECT 1 FROM categories WHERE categories.id = topics.category_id)
+ RETURNING posts.topic_id
+ )
+ UPDATE topics
+ SET posts_count = (
+ SELECT COUNT(*)
+ FROM posts
+ WHERE posts.topic_id = topics.id
+ AND posts.post_number > 1
+ AND posts.deleted_at IS NULL
+ AND posts.post_type NOT IN (#{SMALL_ACTION_POST_TYPE}, #{WHISPER_POST_TYPE})
+ )
+ FROM trashed_first_posts
+ WHERE topics.id = trashed_first_posts.topic_id
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/db/structure.sql b/db/structure.sql
index 8fb8a32ab0b..7cf750b9dcb 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -25563,6 +25563,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20261001073226'),
('20260930114733'),
('20260928103925'),
+('20260925155026'),
('20260925054715'),
('20260923141924'),
('20260923080644'),
diff --git a/plugins/discourse-post-voting/extensions/post_extension.rb b/plugins/discourse-post-voting/extensions/post_extension.rb
index 742028c2d8a..5cd1d6aa19d 100644
--- a/plugins/discourse-post-voting/extensions/post_extension.rb
+++ b/plugins/discourse-post-voting/extensions/post_extension.rb
@@ -11,7 +11,7 @@ module PostVoting
end
def is_post_voting_topic?
- topic.is_post_voting?
+ topic&.is_post_voting?
end
def post_voting_last_voted(user_id)
diff --git a/plugins/discourse-post-voting/spec/models/post_spec.rb b/plugins/discourse-post-voting/spec/models/post_spec.rb
index a99d48f1074..59dae3d63cb 100644
--- a/plugins/discourse-post-voting/spec/models/post_spec.rb
+++ b/plugins/discourse-post-voting/spec/models/post_spec.rb
@@ -24,6 +24,12 @@ describe Post do
end
end
+ it "is not a post voting topic once its topic is trashed" do
+ topic.trash!
+
+ expect(Post.find(post.id).is_post_voting_topic?).to be_falsey
+ end
+
it("ignores vote_count") { expect(Post.ignored_columns.include?("vote_count")).to eq(true) }
it "returns the last vote correctly" do
diff --git a/spec/db/post_migrate/20260925155026_trash_first_post_of_trashed_topics_in_deleted_categories_spec.rb b/spec/db/post_migrate/20260925155026_trash_first_post_of_trashed_topics_in_deleted_categories_spec.rb
new file mode 100644
index 00000000000..557b199512c
--- /dev/null
+++ b/spec/db/post_migrate/20260925155026_trash_first_post_of_trashed_topics_in_deleted_categories_spec.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+require Rails.root.join(
+ "db/post_migrate/20260925155026_trash_first_post_of_trashed_topics_in_deleted_categories.rb",
+ )
+
+RSpec.describe TrashFirstPostOfTrashedTopicsInDeletedCategories do
+ fab!(:category, :category_with_definition)
+
+ before do
+ @original_verbose = ActiveRecord::Migration.verbose
+ ActiveRecord::Migration.verbose = false
+ end
+
+ after { ActiveRecord::Migration.verbose = @original_verbose }
+
+ it "trashes the first post of trashed topics whose category no longer exists" do
+ topic = category.topic
+ Fabricate(:post, topic:)
+ Fabricate(:whisper, topic:)
+ Fabricate(:small_action, topic:)
+ trashed_reply = Fabricate(:post, topic:, deleted_at: 1.day.ago)
+ topic.trash!
+ category.delete
+
+ described_class.new.up
+
+ topic = Topic.with_deleted.find(topic.id)
+ expect(Post.only_deleted.where(topic_id: topic.id)).to contain_exactly(
+ topic.first_post_with_deleted,
+ trashed_reply,
+ )
+ expect(topic.posts_count).to eq(1)
+ end
+
+ it "leaves live orphans, trashed topics of existing categories and private messages untouched" do
+ orphan = Fabricate(:category_with_definition)
+ orphan.delete
+ category.topic.trash!
+ private_message = Fabricate(:private_message_post).topic
+ private_message.trash!
+
+ described_class.new.up
+
+ expect(
+ Post.only_deleted.where(topic_id: [orphan.topic_id, category.topic_id, private_message.id]),
+ ).to be_empty
+ expect(orphan.topic.reload.posts_count).to eq(1)
+ end
+end
diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb
index 40c40df2a70..56d769e29f4 100644
--- a/spec/models/category_spec.rb
+++ b/spec/models/category_spec.rb
@@ -803,9 +803,13 @@ RSpec.describe Category do
before { SiteSetting.shared_drafts_category = category.id.to_s }
it "is deleted correctly" do
+ Fabricate(:post, topic: category.topic)
category.destroy
expect(Category.exists?(id: category_id)).to be false
- expect(Topic.with_deleted.where.not(deleted_at: nil).exists?(id: topic_id)).to be true
+ topic = Topic.with_deleted.find(topic_id)
+ expect(topic).to be_trashed
+ expect(Post.only_deleted.where(topic_id:)).to contain_exactly(topic.first_post_with_deleted)
+ expect(topic.posts_count).to eq(1)
expect(SiteSetting.shared_drafts_category).to be_blank
end