2016-06-24 11:26:28 -04:00
|
|
|
module RuboCop
|
|
|
|
# Module containing helper methods for writing migration cops.
|
|
|
|
module MigrationHelpers
|
2020-03-18 14:09:35 -04:00
|
|
|
WHITELISTED_TABLES = %i[
|
|
|
|
application_settings
|
|
|
|
plan_limits
|
|
|
|
].freeze
|
|
|
|
|
2020-03-30 11:07:51 -04:00
|
|
|
# Blacklisted tables due to:
|
|
|
|
# - number of columns (> 50 on GitLab.com as of 03/2020)
|
|
|
|
# - number of records
|
|
|
|
WIDE_TABLES = %i[
|
|
|
|
users
|
|
|
|
projects
|
|
|
|
ci_builds
|
|
|
|
].freeze
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
# List of helpers that add new columns, either directly (ADD_COLUMN_METHODS)
|
|
|
|
# or through a create/alter table (TABLE_METHODS)
|
|
|
|
ADD_COLUMN_METHODS = %i(add_column add_column_with_default change_column_type_concurrently).freeze
|
|
|
|
|
|
|
|
TABLE_METHODS = %i(create_table create_table_if_not_exists change_table).freeze
|
|
|
|
|
2016-06-24 11:26:28 -04:00
|
|
|
# Returns true if the given node originated from the db/migrate directory.
|
|
|
|
def in_migration?(node)
|
2019-09-18 20:06:11 -04:00
|
|
|
dirname(node).end_with?('db/migrate', 'db/geo/migrate') || in_post_deployment_migration?(node)
|
2016-06-24 11:26:28 -04:00
|
|
|
end
|
2017-12-11 11:34:51 -05:00
|
|
|
|
|
|
|
def in_post_deployment_migration?(node)
|
2019-09-18 20:06:11 -04:00
|
|
|
dirname(node).end_with?('db/post_migrate', 'db/geo/post_migrate')
|
|
|
|
end
|
|
|
|
|
2020-02-19 13:09:10 -05:00
|
|
|
def version(node)
|
|
|
|
File.basename(node.location.expression.source_buffer.name).split('_').first.to_i
|
|
|
|
end
|
|
|
|
|
2020-06-10 17:09:29 -04:00
|
|
|
# Returns true if a column definition is for an array
|
|
|
|
# rubocop:disable Lint/BooleanSymbol
|
|
|
|
def array_column?(node)
|
|
|
|
node.each_descendant(:pair).any? do |pair_node|
|
|
|
|
pair_node.child_nodes[0].value == :array && # Searching for a (pair (sym :array) (true)) node
|
|
|
|
pair_node.child_nodes[1].type == :true # RuboCop::AST::Node uses symbols for types, even when that is a :true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop:enable Lint/BooleanSymbol
|
|
|
|
|
2019-09-18 20:06:11 -04:00
|
|
|
private
|
2017-12-11 11:34:51 -05:00
|
|
|
|
2019-09-18 20:06:11 -04:00
|
|
|
def dirname(node)
|
|
|
|
File.dirname(node.location.expression.source_buffer.name)
|
2017-12-11 11:34:51 -05:00
|
|
|
end
|
2016-06-24 11:26:28 -04:00
|
|
|
end
|
|
|
|
end
|