summaryrefslogtreecommitdiff
path: root/rubocop/cop/migration/add_columns_to_wide_tables.rb
blob: 410563795157343d1c39f334e6ae17708686880c (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
# frozen_string_literal: true

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # Cop that prevents adding columns to wide tables.
      class AddColumnsToWideTables < RuboCop::Cop::Cop
        include MigrationHelpers

        MSG = '`%s` is a wide table with several columns, adding more should be avoided unless absolutely necessary.' \
              ' Consider storing the column in a different table or creating a new one.'

        BLACKLISTED_METHODS = %i[
          add_column
          add_reference
          add_timestamps_with_timezone
        ].freeze

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

          method_name = node.children[1]
          table_name = node.children[2]

          return unless offense?(method_name, table_name)

          add_offense(node, location: :selector, message: format(MSG, table_name.value))
        end

        private

        def offense?(method_name, table_name)
          wide_table?(table_name) &&
            BLACKLISTED_METHODS.include?(method_name)
        end

        def wide_table?(table_name)
          table_name && table_name.type == :sym &&
            WIDE_TABLES.include?(table_name.value)
        end
      end
    end
  end
end