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

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

72 lines
2.5 KiB
Ruby
Raw Permalink Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
require_relative '../../migration_helpers'
module RuboCop
module Cop
module Migration
2022-10-11 01:57:18 +05:30
class VersionedMigrationClass < RuboCop::Cop::Base
2021-11-11 11:23:49 +05:30
include MigrationHelpers
2023-04-23 21:23:45 +05:30
ENFORCED_SINCE = 2023_01_12_00_00_00
DOC_LINK = "https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning"
2021-11-11 11:23:49 +05:30
2023-04-23 21:23:45 +05:30
MSG_INHERIT = "Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. " \
"Use Gitlab::Database::Migration[2.1] instead. See #{DOC_LINK}."
2021-11-11 11:23:49 +05:30
2023-04-23 21:23:45 +05:30
MSG_INCLUDE = "Don't include migration helper modules directly. " \
"Inherit from Gitlab::Database::Migration[2.1] instead. See #{DOC_LINK}."
GITLAB_MIGRATION_CLASS = 'Gitlab::Database::Migration'
2021-11-11 11:23:49 +05:30
ACTIVERECORD_MIGRATION_CLASS = 'ActiveRecord::Migration'
2023-04-23 21:23:45 +05:30
CURRENT_MIGRATION_VERSION = 2.1 # Should be the same value as Gitlab::Database::Migration.current_version
2021-11-11 11:23:49 +05:30
def_node_search :includes_helpers?, <<~PATTERN
(send nil? :include
(const
(const
(const nil? :Gitlab) :Database) :MigrationHelpers))
PATTERN
def on_class(node)
return unless relevant_migration?(node)
2023-04-23 21:23:45 +05:30
return unless activerecord_migration_class?(node) || old_version_migration_class?(node)
2021-11-11 11:23:49 +05:30
2022-10-11 01:57:18 +05:30
add_offense(node, message: MSG_INHERIT)
2021-11-11 11:23:49 +05:30
end
def on_send(node)
return unless relevant_migration?(node)
2022-10-11 01:57:18 +05:30
add_offense(node, message: MSG_INCLUDE) if includes_helpers?(node)
2021-11-11 11:23:49 +05:30
end
private
def relevant_migration?(node)
in_migration?(node) && version(node) >= ENFORCED_SINCE
end
def activerecord_migration_class?(node)
superclass(node) == ACTIVERECORD_MIGRATION_CLASS
end
def superclass(class_node)
_, *others = class_node.descendants
others.find { |node| node.const_type? && node&.const_name != 'Types' }&.const_name
end
2023-04-23 21:23:45 +05:30
# Returns true for any parent class of format Gitlab::Database::Migration[version] if version < current_version
def old_version_migration_class?(class_node)
parent_class_node = class_node.parent_class
return false unless parent_class_node.send_type? && parent_class_node.arguments.last.float_type?
return false unless parent_class_node.children[0].const_name == GITLAB_MIGRATION_CLASS
parent_class_node.arguments[0].value < CURRENT_MIGRATION_VERSION
end
2021-11-11 11:23:49 +05:30
end
end
end
end