2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
module RuboCop
|
|
|
|
# Module containing helper methods for writing migration cops.
|
|
|
|
module MigrationHelpers
|
2020-11-24 15:15:51 +05:30
|
|
|
# Tables with permanently small number of records
|
|
|
|
SMALL_TABLES = %i[
|
2020-04-22 19:07:51 +05:30
|
|
|
application_settings
|
|
|
|
plan_limits
|
|
|
|
].freeze
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
# Tables with large number of columns (> 50 on GitLab.com as of 01/2021)
|
2020-04-22 19:07:51 +05:30
|
|
|
WIDE_TABLES = %i[
|
|
|
|
ci_builds
|
2021-03-11 19:13:27 +05:30
|
|
|
namespaces
|
|
|
|
projects
|
|
|
|
users
|
2020-04-22 19:07:51 +05:30
|
|
|
].freeze
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
# List of helpers that add new columns, either directly (ADD_COLUMN_METHODS)
|
|
|
|
# or through a create/alter table (TABLE_METHODS)
|
2023-03-04 22:38:38 +05:30
|
|
|
ADD_COLUMN_METHODS = %i(add_column change_column_type_concurrently).freeze
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
TABLE_METHODS = %i(create_table create_table_if_not_exists change_table create_table_with_constraints).freeze
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def high_traffic_tables
|
2021-01-03 14:25:43 +05:30
|
|
|
@high_traffic_tables ||= rubocop_migrations_config.dig('Migration/UpdateLargeTable', 'HighTrafficTables')
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
# Returns true if the given node originated from the db/migrate directory.
|
|
|
|
def in_migration?(node)
|
2020-06-23 00:09:42 +05:30
|
|
|
in_deployment_migration?(node) || in_post_deployment_migration?(node)
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def in_background_migration?(node)
|
|
|
|
filepath(node).include?('/lib/gitlab/background_migration/') ||
|
2022-11-25 23:54:43 +05:30
|
|
|
in_ee_background_migration?(node)
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_ee_background_migration?(node)
|
|
|
|
filepath(node).include?('/ee/lib/ee/gitlab/background_migration/')
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def in_deployment_migration?(node)
|
|
|
|
dirname(node).end_with?('db/migrate', 'db/geo/migrate')
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
def in_post_deployment_migration?(node)
|
2019-12-21 20:55:43 +05:30
|
|
|
dirname(node).end_with?('db/post_migrate', 'db/geo/post_migrate')
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
# Returns true if we've defined an 'EnforcedSince' variable in rubocop.yml and the migration version
|
|
|
|
# is greater.
|
|
|
|
def time_enforced?(node)
|
|
|
|
return false unless enforced_since
|
|
|
|
|
|
|
|
version(node) > enforced_since
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def version(node)
|
|
|
|
File.basename(node.location.expression.source_buffer.name).split('_').first.to_i
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
# Returns true if a column definition is for an array, like { array: true }
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# add_column :table, :ids, :integer, array: true, default: []
|
|
|
|
#
|
2020-06-23 00:09:42 +05:30
|
|
|
# rubocop:disable Lint/BooleanSymbol
|
|
|
|
def array_column?(node)
|
|
|
|
node.each_descendant(:pair).any? do |pair_node|
|
2023-04-23 21:23:45 +05:30
|
|
|
pair_node.child_nodes[0].sym_type? && # Searching for a RuboCop::AST::SymbolNode
|
|
|
|
pair_node.child_nodes[0].value == :array && # Searching for a (pair (sym :array) (true)) node
|
2023-03-04 22:38:38 +05:30
|
|
|
pair_node.child_nodes[1].type == :true # RuboCop::AST::Node uses symbols for types, even when that is a :true
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop:enable Lint/BooleanSymbol
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
private
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def filepath(node)
|
|
|
|
node.location.expression.source_buffer.name
|
|
|
|
end
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
def dirname(node)
|
2022-07-16 23:28:13 +05:30
|
|
|
File.dirname(filepath(node))
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def rubocop_migrations_config
|
|
|
|
@rubocop_migrations_config ||= YAML.load_file(File.join(rubocop_path, 'rubocop-migrations.yml'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def rubocop_path
|
|
|
|
File.expand_path(__dir__)
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
def enforced_since
|
|
|
|
@enforced_since ||= config.for_cop(name)['EnforcedSince']
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
end
|