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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
1.2 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
require_relative '../../migration_helpers'
module RuboCop
module Cop
module Migration
# Cop that prevents adding columns to wide tables.
2022-10-11 01:57:18 +05:30
class AddColumnsToWideTables < RuboCop::Cop::Base
2020-04-22 19:07:51 +05:30
include MigrationHelpers
2021-04-17 20:07:23 +05:30
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.'
2020-04-22 19:07:51 +05:30
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)
2022-10-11 01:57:18 +05:30
add_offense(node.loc.selector, message: format(MSG, table_name.value))
2020-04-22 19:07:51 +05:30
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