diff --git a/app/services/user_destroyer.rb b/app/services/user_destroyer.rb index adba7e45457..7e2f4eedbe0 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