2022-07-16 23:28:13 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../../migration_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Migration
|
|
|
|
class MigrationRecord < RuboCop::Cop::Cop
|
|
|
|
include MigrationHelpers
|
|
|
|
|
|
|
|
ENFORCED_SINCE = 2022_04_26_00_00_00
|
|
|
|
|
|
|
|
MSG = <<~MSG
|
2022-07-23 23:45:48 +05:30
|
|
|
Don't inherit from ActiveRecord::Base or ApplicationRecord but use MigrationRecord instead.
|
2022-07-16 23:28:13 +05:30
|
|
|
See https://docs.gitlab.com/ee/development/database/migrations_for_multiple_databases.html#example-usage-of-activerecord-classes.
|
|
|
|
MSG
|
|
|
|
|
|
|
|
def_node_search :inherits_from_active_record_base?, <<~PATTERN
|
|
|
|
(class _ (const (const _ :ActiveRecord) :Base) _)
|
|
|
|
PATTERN
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
def_node_search :inherits_from_application_record?, <<~PATTERN
|
|
|
|
(class _ (const _ :ApplicationRecord) _)
|
|
|
|
PATTERN
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def on_class(node)
|
|
|
|
return unless relevant_migration?(node)
|
2022-07-23 23:45:48 +05:30
|
|
|
return unless inherits_from_active_record_base?(node) || inherits_from_application_record?(node)
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
add_offense(node, location: :expression)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def relevant_migration?(node)
|
|
|
|
in_migration?(node) && version(node) >= ENFORCED_SINCE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|