PERF: delete user and post actions vs destroy on user destroy

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.

diff --git a/app/models/user.rb b/app/models/user.rb
index 1f47eaa..e10ebba 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

GitHub sha: d7c08e21

1 Like

FIX: properly clean out post and user actions on destroy user