diff options
Diffstat (limited to 'rubocop/cop/performance')
-rw-r--r-- | rubocop/cop/performance/active_record_subtransaction_methods.rb | 29 | ||||
-rw-r--r-- | rubocop/cop/performance/active_record_subtransactions.rb | 30 |
2 files changed, 59 insertions, 0 deletions
diff --git a/rubocop/cop/performance/active_record_subtransaction_methods.rb b/rubocop/cop/performance/active_record_subtransaction_methods.rb new file mode 100644 index 00000000000..3b89d3ab858 --- /dev/null +++ b/rubocop/cop/performance/active_record_subtransaction_methods.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Performance + # Cop that disallows certain methods that rely on subtransactions in their implementation. + # Companion to Performance/ActiveRecordSubtransactions, which bans direct usage of subtransactions. + class ActiveRecordSubtransactionMethods < RuboCop::Cop::Cop + MSG = 'Methods that rely on subtransactions should not be used. ' \ + 'For more information see: https://gitlab.com/gitlab-org/gitlab/-/issues/338346' + + DISALLOWED_METHODS = %i[ + safe_ensure_unique + safe_find_or_create_by + safe_find_or_create_by! + with_fast_read_statement_timeout + create_or_find_by + create_or_find_by! + ].to_set.freeze + + def on_send(node) + return unless DISALLOWED_METHODS.include?(node.method_name) + + add_offense(node, location: :selector) + end + end + end + end +end diff --git a/rubocop/cop/performance/active_record_subtransactions.rb b/rubocop/cop/performance/active_record_subtransactions.rb new file mode 100644 index 00000000000..a550b558e52 --- /dev/null +++ b/rubocop/cop/performance/active_record_subtransactions.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Performance + class ActiveRecordSubtransactions < RuboCop::Cop::Cop + MSG = 'Subtransactions should not be used. ' \ + 'For more information see: https://gitlab.com/gitlab-org/gitlab/-/issues/338346' + + def_node_matcher :match_transaction_with_options, <<~PATTERN + (send _ :transaction (hash $...)) + PATTERN + + def_node_matcher :subtransaction_option?, <<~PATTERN + (pair (:sym :requires_new) (true)) + PATTERN + + def on_send(node) + match_transaction_with_options(node) do |option_nodes| + option_nodes.each do |option_node| + next unless subtransaction_option?(option_node) + + add_offense(option_node) + end + end + end + end + end + end +end |