summaryrefslogtreecommitdiff
path: root/rubocop/cop
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2018-08-16 14:28:25 +0200
committerYorick Peterse <yorickpeterse@gmail.com>2018-08-16 17:29:33 +0200
commit96ce2da74ee36e88f20cbd7ceaff2ab49f0bb223 (patch)
tree484f41cb9b2013fde409715bad4d00a602e92fb2 /rubocop/cop
parent353360ef824e10cb0197733e7b104a6fb53cf9af (diff)
downloadgitlab-ce-96ce2da74ee36e88f20cbd7ceaff2ab49f0bb223.tar.gz
Blacklist the use of "destroy_all"
This method usually has really bad performance implications, as it loads rows into memory and deletes them one by one.
Diffstat (limited to 'rubocop/cop')
-rw-r--r--rubocop/cop/destroy_all.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/rubocop/cop/destroy_all.rb b/rubocop/cop/destroy_all.rb
new file mode 100644
index 00000000000..38b6cb40f91
--- /dev/null
+++ b/rubocop/cop/destroy_all.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ # Cop that blacklists the use of `destroy_all`.
+ class DestroyAll < RuboCop::Cop::Cop
+ MSG = 'Use `delete_all` instead of `destroy_all`. ' \
+ '`destroy_all` will load the rows into memory, then execute a ' \
+ '`DELETE` for every individual row.'
+
+ def_node_matcher :destroy_all?, <<~PATTERN
+ (send {send ivar lvar const} :destroy_all ...)
+ PATTERN
+
+ def on_send(node)
+ return unless destroy_all?(node)
+
+ add_offense(node, location: :expression)
+ end
+ end
+ end
+end