summaryrefslogtreecommitdiff
path: root/rubocop/cop/migration/prevent_global_enable_lock_retries_with_disable_ddl_transaction.rb
blob: f5343f973e19159ca54fb0fd131538967833726a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# frozen_string_literal: true

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # Cop that prevents usage of `enable_lock_retries!` within the `disable_ddl_transaction!` method.
      class PreventGlobalEnableLockRetriesWithDisableDdlTransaction < RuboCop::Cop::Cop
        include MigrationHelpers

        MSG = '`enable_lock_retries!` cannot be used with `disable_ddl_transaction!`. Use the `with_lock_retries` helper method to define retriable code blocks.'

        def_node_matcher :enable_lock_retries?, <<~PATTERN
          (send _ :enable_lock_retries! ...)
        PATTERN

        def_node_matcher :disable_ddl_transaction?, <<~PATTERN
          (send _ :disable_ddl_transaction! ...)
        PATTERN

        def on_begin(node)
          return unless in_migration?(node)

          has_enable_lock_retries = false
          has_disable_ddl_transaction = false

          node.each_descendant(:send) do |send_node|
            has_enable_lock_retries = true if enable_lock_retries?(send_node)
            has_disable_ddl_transaction = true if disable_ddl_transaction?(send_node)

            if has_enable_lock_retries && has_disable_ddl_transaction
              add_offense(send_node, message: MSG)
              break
            end
          end
        end
      end
    end
  end
end