From d7c08e217ae4befc8f8fb37dd1341a9f2b5b4c4f Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 17 Jan 2019 11:19:37 +1100 Subject: [PATCH] PERF: delete user and post actions vs destroy on user destroy Users can have 100s of thousands of post and user actions, we do not want to destroy each individually cause the tracking is enormous and the amount of queries we would need is enormous. This gives up on the `after_commit` hook on `post_actions` which ships a message to clients to synchronize a post, so some phantom post_actions may remain in the UX in the rare occasion we delete a user. The phantoms will be gone on reload. --- app/models/user.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 1f47eaacb05..e10ebba595d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -33,8 +33,14 @@ class User < ActiveRecord::Base has_many :user_api_keys, dependent: :destroy has_many :topics has_many :user_open_ids, dependent: :destroy - has_many :user_actions, dependent: :destroy - has_many :post_actions, dependent: :destroy + + # Do Not Change to user_actions/post_actions to dependent: destroy + # users can have lots of actions, bypass tracking of all destroyed objects + # this means there is a much higher likelihood that users with lots of state + # can be destroyed. + has_many :user_actions, dependent: :delete_all + has_many :post_actions, dependent: :delete_all + has_many :user_badges, -> { where('user_badges.badge_id IN (SELECT id FROM badges WHERE enabled)') }, dependent: :destroy has_many :badges, through: :user_badges has_many :email_logs, dependent: :delete_all