debian-mirror-gitlab/rubocop/migration_helpers.rb

69 lines
2.1 KiB
Ruby
Raw Normal View History

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
2020-11-24 15:15:51 +05:30
# Tables with large number of columns (> 50 on GitLab.com as of 03/2020)
2020-04-22 19:07:51 +05:30
WIDE_TABLES = %i[
users
projects
ci_builds
].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)
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
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
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
2020-04-08 14:13:33 +05:30
def version(node)
File.basename(node.location.expression.source_buffer.name).split('_').first.to_i
end
2020-06-23 00:09:42 +05:30
# 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-12-21 20:55:43 +05:30
private
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
def dirname(node)
File.dirname(node.location.expression.source_buffer.name)
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
2016-08-24 12:49:21 +05:30
end
end