Class: Gitlab::Styles::Rubocop::Cop::Migration::AddColumn

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Includes:
MigrationHelpers
Defined in:
lib/gitlab/styles/rubocop/cop/migration/add_column.rb

Overview

Cop that checks if columns are added in a way that doesn’t require downtime.

Constant Summary collapse

WHITELISTED_TABLES =
[:application_settings].freeze
MSG =
'`add_column` with a default value requires downtime, ' \
'use `add_column_with_default` instead'.freeze

Instance Method Summary collapse

Methods included from MigrationHelpers

#in_migration?

Instance Method Details

#hash_key_name(pair) ⇒ Object



48
49
50
# File 'lib/gitlab/styles/rubocop/cop/migration/add_column.rb', line 48

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

#hash_key_type(pair) ⇒ Object



44
45
46
# File 'lib/gitlab/styles/rubocop/cop/migration/add_column.rb', line 44

def hash_key_type(pair)
  pair.children[0].type
end

#on_send(node) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/styles/rubocop/cop/migration/add_column.rb', line 18

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

  name = node.children[1]

  return unless name == :add_column

  # Ignore whitelisted tables.
  return if table_whitelisted?(node.children[2])

  opts = node.children.last

  return unless opts&.type == :hash

  opts.each_node(:pair) do |pair|
    if hash_key_type(pair) == :sym && hash_key_name(pair) == :default
      add_offense(node, :selector)
    end
  end
end

#table_whitelisted?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/gitlab/styles/rubocop/cop/migration/add_column.rb', line 39

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