diff options
author | Mayra Cabrera <mcabrera@gitlab.com> | 2019-08-23 21:36:12 +0000 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-08-23 21:36:12 +0000 |
commit | 4706352416005962ccb34bad1c3acc5d7479523c (patch) | |
tree | 334b873d90bf2aa98349f3812e9b1af06f89f484 /rubocop | |
parent | b8dec7ecb43d3825f994136ca68e88aada832218 (diff) | |
download | gitlab-ce-4706352416005962ccb34bad1c3acc5d7479523c.tar.gz |
Adds cop to enforce string limits on migrations
This cop will analyze migrations that add columns with string, and
report an offense if the string has no limit enforced
Related to https://gitlab.com/gitlab-org/gitlab-ce/issues/64505
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/migration/add_limit_to_string_columns.rb | 59 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 1 |
2 files changed, 60 insertions, 0 deletions
diff --git a/rubocop/cop/migration/add_limit_to_string_columns.rb b/rubocop/cop/migration/add_limit_to_string_columns.rb new file mode 100644 index 00000000000..30affcbb089 --- /dev/null +++ b/rubocop/cop/migration/add_limit_to_string_columns.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require_relative '../../migration_helpers' + +module RuboCop + module Cop + module Migration + # Cop that enforces length constraints to string columns + class AddLimitToStringColumns < RuboCop::Cop::Cop + include MigrationHelpers + + ADD_COLUMNS_METHODS = %i(add_column add_column_with_default).freeze + + MSG = 'String columns should have a limit constraint. 255 is suggested'.freeze + + def on_def(node) + return unless in_migration?(node) + + node.each_descendant(:send) do |send_node| + next unless string_operation?(send_node) + + add_offense(send_node, location: :selector) unless limit_on_string_column?(send_node) + end + end + + private + + def string_operation?(node) + modifier = node.children[0] + migration_method = node.children[1] + + if migration_method == :string + modifier.type == :lvar + elsif ADD_COLUMNS_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 + + def limit_on_string_column?(node) + migration_method = node.children[1] + + if migration_method == :string + limit_present?(node.children) + elsif ADD_COLUMNS_METHODS.include?(migration_method) + limit_present?(node) + end + end + + def limit_present?(statement) + !(statement.to_s =~ /:limit/).nil? + end + end + end + end +end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 58a7ead6f13..d1328c4eb38 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -17,6 +17,7 @@ require_relative 'cop/migration/add_column' require_relative 'cop/migration/add_concurrent_foreign_key' require_relative 'cop/migration/add_concurrent_index' require_relative 'cop/migration/add_index' +require_relative 'cop/migration/add_limit_to_string_columns' require_relative 'cop/migration/add_reference' require_relative 'cop/migration/add_timestamps' require_relative 'cop/migration/datetime' |