Skip to content

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

PERF: Avoid full `posts` table scans during anonymisation (#21081)

David Taylor93c33e02f0c3
diff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rb
index 26c3f64099d..c56ac2ecf20 100644
--- a/app/jobs/regular/update_username.rb
+++ b/app/jobs/regular/update_username.rb
@@ -46,9 +46,21 @@ module Jobs
def update_posts
updated_post_ids = Set.new
+ # Other people mentioning this user
+ Post
+ .with_deleted
+ .joins(mentioned("posts.id"))
+ .where("a.user_id = :user_id", user_id: @user_id)
+ .find_each do |post|
+ update_post(post)
+ updated_post_ids << post.id
+ end
+
+ # User mentioning self (not included in post_actions table)
Post
.with_deleted
.where("raw ILIKE ?", "%@#{@old_username}%")
+ .where("posts.user_id = :user_id", user_id: @user_id)
.find_each do |post|
update_post(post)
updated_post_ids << post.id