FEATURE: allow calling user destroyer without a transaction

FEATURE: allow calling user destroyer without a transaction

This additional option allows us to destroy a user in “pieces” in cases where database is very big and stuff tends to time out

diff --git a/app/services/user_destroyer.rb b/app/services/user_destroyer.rb
index adba7e4..7e2f4ee 100644
--- a/app/services/user_destroyer.rb
+++ b/app/services/user_destroyer.rb
@@ -18,7 +18,10 @@ class UserDestroyer
     raise PostsExistError if !opts[:delete_posts] && user.posts.count != 0
     @guardian.ensure_can_delete_user!(user)
 
-    User.transaction do
+    # default to using a transaction
+    opts[:transaction] = true if opts[:transaction] != false
+
+    optional_transaction(open_transaction: opts[:transaction]) do
 
       Draft.where(user_id: user.id).delete_all
       QueuedPost.where(user_id: user.id).delete_all
@@ -106,4 +109,14 @@ class UserDestroyer
     end
   end
 
+  protected
+
+  def optional_transaction(open_transaction: true)
+    if open_transaction
+      User.transaction { yield  }
+    else
+      yield
+    end
+  end
+
 end

GitHub sha: 6079a3e4

1 Like