Skip to content

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

FIX: UserDestroyer should unclaim any claimed reviewables. (#36277)

Gary Pendergastced8c2c1274c
diff --git a/app/services/user_destroyer.rb b/app/services/user_destroyer.rb
index acfec664534..403bfffd484 100644
--- a/app/services/user_destroyer.rb
+++ b/app/services/user_destroyer.rb
@@ -30,6 +30,7 @@ class UserDestroyer
Bookmark.where(user_id: user.id).delete_all
Draft.where(user_id: user.id).delete_all
Reviewable.where(created_by_id: user.id).delete_all
+ ReviewableClaimedTopic.where(user_id: user.id).delete_all
category_topic_ids = Category.where.not(topic_id: nil).pluck(:topic_id)
diff --git a/db/post_migrate/20251127001422_remove_orphaned_reviewable_claimed_topics.rb b/db/post_migrate/20251127001422_remove_orphaned_reviewable_claimed_topics.rb
new file mode 100644
index 00000000000..124536e8830
--- /dev/null
+++ b/db/post_migrate/20251127001422_remove_orphaned_reviewable_claimed_topics.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class RemoveOrphanedReviewableClaimedTopics < ActiveRecord::Migration[8.0]
+ def up
+ DB.exec(<<~SQL)
+ DELETE FROM reviewable_claimed_topics
+ WHERE user_id NOT IN (SELECT id FROM users)
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/spec/services/user_destroyer_spec.rb b/spec/services/user_destroyer_spec.rb
index 866a6fb603f..cc39e95769a 100644
--- a/spec/services/user_destroyer_spec.rb
+++ b/spec/services/user_destroyer_spec.rb
@@ -419,6 +419,16 @@ RSpec.describe UserDestroyer do
end
end
+ describe "Destroying a user with a reviewable claimed topic" do
+ let!(:topic) { Fabricate(:topic) }
+ let!(:claimed_topic) { Fabricate(:reviewable_claimed_topic, user: user, topic: topic) }
+
+ it "removes the reviewable claimed topic" do
+ UserDestroyer.new(admin).destroy(user)
+ expect(ReviewableClaimedTopic.exists?(user_id: user.id)).to be false
+ end
+ end
+
context "when user liked things" do
before do
@topic = Fabricate(:topic, user: Fabricate(:user))