Commit 6d2ad959 authored by Andreas Brandl's avatar Andreas Brandl

Merge branch 'fix-ar-classes-in-versioned--migration-cop' into 'master'

Exclude non migration classes from Migration/VersionedMigration cop

See merge request gitlab-org/gitlab!69756
parents 15790615 b6fcb032
...@@ -13,7 +13,7 @@ module RuboCop ...@@ -13,7 +13,7 @@ module RuboCop
MSG_INHERIT = 'Don\'t inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[1.0] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.' MSG_INHERIT = 'Don\'t inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[1.0] 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[1.0] 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[1.0] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.'
MIGRATION_CLASS = 'Gitlab::Database::Migration' ACTIVERECORD_MIGRATION_CLASS = 'ActiveRecord::Migration'
def_node_search :includes_helpers?, <<~PATTERN def_node_search :includes_helpers?, <<~PATTERN
(send nil? :include (send nil? :include
...@@ -24,8 +24,9 @@ module RuboCop ...@@ -24,8 +24,9 @@ module RuboCop
def on_class(node) def on_class(node)
return unless relevant_migration?(node) return unless relevant_migration?(node)
return unless activerecord_migration_class?(node)
add_offense(node, location: :expression, message: MSG_INHERIT) unless gitlab_migration_class?(node) add_offense(node, location: :expression, message: MSG_INHERIT)
end end
def on_send(node) def on_send(node)
...@@ -40,8 +41,8 @@ module RuboCop ...@@ -40,8 +41,8 @@ module RuboCop
in_migration?(node) && version(node) >= ENFORCED_SINCE in_migration?(node) && version(node) >= ENFORCED_SINCE
end end
def gitlab_migration_class?(node) def activerecord_migration_class?(node)
superclass(node) == MIGRATION_CLASS superclass(node) == ACTIVERECORD_MIGRATION_CLASS
end end
def superclass(class_node) def superclass(class_node)
......
...@@ -64,6 +64,18 @@ RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass do ...@@ -64,6 +64,18 @@ RSpec.describe RuboCop::Cop::Migration::VersionedMigrationClass do
end end
RUBY RUBY
end end
it 'excludes ActiveRecord classes defined inside the migration' do
expect_no_offenses(<<~RUBY)
class TestMigration < Gitlab::Database::Migration[1.0]
class TestModel < ApplicationRecord
end
class AnotherTestModel < ActiveRecord::Base
end
end
RUBY
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment