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

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

require_relative '../../migration_helpers'

module RuboCop
  module Cop
    module Migration
      # Cop that enforces using text instead of the string data type
      class PreventStrings < RuboCop::Cop::Cop
        include MigrationHelpers

        MSG = 'Do not use the `string` data type, use `text` instead. ' \
          'Updating limits on strings requires downtime. This can be avoided ' \
          'by using `text` and adding a limit with `add_text_limit`'

        def_node_matcher :reverting?, <<~PATTERN
          (def :down ...)
        PATTERN

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

          # Don't enforce the rule when on down to keep consistency with existing schema
          return if reverting?(node)

          node.each_descendant(:send) do |send_node|
            next unless string_operation?(send_node)

            add_offense(send_node, location: :selector)
          end
        end

        private

        def string_operation?(node)
          # Don't complain about string arrays
          return false if array_column?(node)

          modifier = node.children[0]
          migration_method = node.children[1]

          if migration_method == :string
            modifier.type == :lvar
          elsif ADD_COLUMN_METHODS.include?(migration_method)
            modifier.nil? && string_column?(node.children[4])
          end
        end

        def string_column?(column_type)
          column_type.type == :sym && column_type.value == :string
        end
      end
    end
  end
end