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

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

42 lines
1.2 KiB
Ruby
Raw Normal View History

2022-07-16 23:28:13 +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 MigrationRecord < RuboCop::Cop::Base
2022-07-16 23:28:13 +05:30
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
2022-10-11 01:57:18 +05:30
add_offense(node)
2022-07-16 23:28:13 +05:30
end
private
def relevant_migration?(node)
in_migration?(node) && version(node) >= ENFORCED_SINCE
end
end
end
end
end