summaryrefslogtreecommitdiff
path: root/rubocop/cop/migration/safer_boolean_column.rb
blob: 0335c25d85d7698ee8972c371785ccfcfe666326 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # This cop requires a default value and disallows nulls for boolean
      # columns on small tables.
      #
      # In general, this prevents 3-state-booleans.
      # https://robots.thoughtbot.com/avoid-the-threestate-boolean-problem
      #
      # In particular, for the `application_settings` table, this ensures that
      # upgraded installations get a proper default for the new boolean setting.
      # A developer might otherwise mistakenly assume that a value in
      # `ApplicationSetting.defaults` is sufficient.
      #
      # See https://gitlab.com/gitlab-org/gitlab-ee/issues/2750 for more
      # information.
      class SaferBooleanColumn < RuboCop::Cop::Cop
        include MigrationHelpers

        DEFAULT_OFFENSE = 'Boolean columns on the `%s` table should have a default. You may wish to use `add_column_with_default`.'.freeze
        NULL_OFFENSE = 'Boolean columns on the `%s` table should disallow nulls.'.freeze
        DEFAULT_AND_NULL_OFFENSE = 'Boolean columns on the `%s` table should have a default and should disallow nulls. You may wish to use `add_column_with_default`.'.freeze

        SMALL_TABLES = %i[
          application_settings
        ].freeze

        def_node_matcher :add_column?, <<~PATTERN
          (send nil :add_column $...)
        PATTERN

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

          matched = add_column?(node)

          return unless matched

          table, _, type = matched.to_a.take(3).map(&:children).map(&:first)
          opts = matched[3]

          return unless SMALL_TABLES.include?(table) && type == :boolean

          no_default = no_default?(opts)
          nulls_allowed = nulls_allowed?(opts)

          offense = if no_default && nulls_allowed
                      DEFAULT_AND_NULL_OFFENSE
                    elsif no_default
                      DEFAULT_OFFENSE
                    elsif nulls_allowed
                      NULL_OFFENSE
                    end

          add_offense(node, :expression, format(offense, table)) if offense
        end

        def no_default?(opts)
          return true unless opts

          each_hash_node_pair(opts) do |key, value|
            return value == 'nil' if key == :default
          end
        end

        def nulls_allowed?(opts)
          return true unless opts

          each_hash_node_pair(opts) do |key, value|
            return value != 'false' if key == :null
          end
        end

        def each_hash_node_pair(hash_node, &block)
          hash_node.each_node(:pair) do |pair|
            key = hash_pair_key(pair)
            value = hash_pair_value(pair)
            yield(key, value)
          end
        end

        def hash_pair_key(pair)
          pair.children[0].children[0]
        end

        def hash_pair_value(pair)
          pair.children[1].source
        end
      end
    end
  end
end