debian-mirror-gitlab/rubocop/cop/migration/prevent_strings.rb

56 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

2020-05-24 23:13:21 +05:30
# 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
2022-10-11 01:57:18 +05:30
class PreventStrings < RuboCop::Cop::Base
2020-05-24 23:13:21 +05:30
include MigrationHelpers
MSG = 'Do not use the `string` data type, use `text` instead. ' \
'Updating limits on strings requires downtime. This can be avoided ' \
2021-04-29 21:17:54 +05:30
'by using `text` and adding a limit with `add_text_limit`'
2020-05-24 23:13:21 +05:30
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)
2022-10-11 01:57:18 +05:30
add_offense(send_node.loc.selector)
2020-05-24 23:13:21 +05:30
end
end
private
def string_operation?(node)
2020-06-23 00:09:42 +05:30
# Don't complain about string arrays
return false if array_column?(node)
2020-05-24 23:13:21 +05:30
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