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
ENFORCED_SINCE = 2021_09_02_00_00_00
2023-03-04 22:38:38 +05:30
CURRENT_DATABASE_MIGRATION_CLASS = 'Gitlab::Database::Migration[2.1]'
2021-11-11 11:23:49 +05:30
2023-03-04 22:38:38 +05:30
MSG_INHERIT = 'Don\'t inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.'
MSG_INCLUDE = 'Don\'t include migration helper modules directly. Inherit from Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.'
2021-11-11 11:23:49 +05:30
ACTIVERECORD_MIGRATION_CLASS = 'ActiveRecord::Migration'
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 )
return unless activerecord_migration_class? ( node )
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
end
end
end
end