Welcome to mirror list, hosted at ThFree Co, Russian Federation.

with_lock_retries_without_ddl_transaction.rb « migration « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ebd91dd5a6ec01a3ae5a4ecdde1341e4fb5dbfe1 (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
# frozen_string_literal: true

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # Cop that prevents usage of `with_lock_retries` with `disable_ddl_transaction!`
      class WithLockRetriesWithoutDdlTransaction < RuboCop::Cop::Cop
        include MigrationHelpers

        MSG = '`with_lock_retries` cannot be used with disabled DDL transactions (`disable_ddl_transaction!`). ' \
          'Please remove the `disable_ddl_transaction!` call from your migration.'.freeze

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

        def_node_matcher :with_lock_retries?, <<~PATTERN
          (send _ :with_lock_retries)
        PATTERN

        def on_send(node)
          return unless in_migration?(node)
          return unless with_lock_retries?(node)

          node.each_ancestor(:begin) do |begin_node|
            disable_ddl_transaction_node = begin_node.children.find { |n| disable_ddl_transaction?(n) }

            add_offense(node, location: :expression) if disable_ddl_transaction_node
          end
        end
      end
    end
  end
end