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

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

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # Cop that checks if columns are added in a way that doesn't require
      # downtime.
      class AddColumnWithDefault < RuboCop::Cop::Cop
        include MigrationHelpers

        WHITELISTED_TABLES = [:application_settings].freeze

        MSG = '`add_column_with_default` without `allow_null: true` may cause prolonged lock situations and downtime, ' \
          'see https://gitlab.com/gitlab-org/gitlab/issues/38060'.freeze

        def_node_matcher :add_column_with_default?, <<~PATTERN
          (send _ :add_column_with_default $_ ... (hash $...))
        PATTERN

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

          add_column_with_default?(node) do |table, options|
            break if table_whitelisted?(table) || nulls_allowed?(options)

            add_offense(node, location: :selector)
          end
        end

        private

        def nulls_allowed?(options)
          options.find { |opt| opt.key.value == :allow_null && opt.value.true_type? }
        end

        def table_whitelisted?(symbol)
          symbol && symbol.type == :sym &&
            WHITELISTED_TABLES.include?(symbol.children[0])
        end
      end
    end
  end
end