Skip to content
代码片段 群组 项目
提交 ecb48a9a 编辑于 作者: Eulyeon Ko's avatar Eulyeon Ko 提交者: Gosia Ksionek
浏览文件

Update versioned migration cop

The migration helper's current version is v2.2.
上级 6768f04a
No related branches found
No related tags found
无相关合并请求
...@@ -8,18 +8,19 @@ module Migration ...@@ -8,18 +8,19 @@ module Migration
class VersionedMigrationClass < RuboCop::Cop::Base class VersionedMigrationClass < RuboCop::Cop::Base
include MigrationHelpers include MigrationHelpers
ENFORCED_SINCE = 2023_01_12_00_00_00 ENFORCED_SINCE = 2023_11_01_02_15_00
CURRENT_MIGRATION_VERSION = 2.2 # Should be the same value as Gitlab::Database::Migration.current_version
DOC_LINK = "https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning" DOC_LINK = "https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning"
MSG_INHERIT = "Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. " \ 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}.".freeze "Use Gitlab::Database::Migration[#{CURRENT_MIGRATION_VERSION}] instead. See #{DOC_LINK}.".freeze
MSG_INCLUDE = "Don't include migration helper modules directly. " \ MSG_INCLUDE = "Don't include migration helper modules directly. " \
"Inherit from Gitlab::Database::Migration[2.1] instead. See #{DOC_LINK}.".freeze "Inherit from Gitlab::Database::Migration[#{CURRENT_MIGRATION_VERSION}] instead. See #{DOC_LINK}."
.freeze
GITLAB_MIGRATION_CLASS = 'Gitlab::Database::Migration' GITLAB_MIGRATION_CLASS = 'Gitlab::Database::Migration'
ACTIVERECORD_MIGRATION_CLASS = 'ActiveRecord::Migration' ACTIVERECORD_MIGRATION_CLASS = 'ActiveRecord::Migration'
CURRENT_MIGRATION_VERSION = 2.1 # Should be the same value as Gitlab::Database::Migration.current_version
def_node_search :includes_helpers?, <<~PATTERN def_node_search :includes_helpers?, <<~PATTERN
(send nil? :include (send nil? :include
......
...@@ -34,6 +34,12 @@ ...@@ -34,6 +34,12 @@
# untouched. # untouched.
expect(described_class[described_class.current_version]).to be < ActiveRecord::Migration::Current expect(described_class[described_class.current_version]).to be < ActiveRecord::Migration::Current
end end
it 'matches the version used by Rubocop' do
require 'rubocop'
load 'rubocop/cop/migration/versioned_migration_class.rb'
expect(described_class.current_version).to eq(RuboCop::Cop::Migration::VersionedMigrationClass::CURRENT_MIGRATION_VERSION)
end
end end
describe Gitlab::Database::Migration::LockRetriesConcern do describe Gitlab::Database::Migration::LockRetriesConcern do
......
...@@ -49,7 +49,7 @@ def down ...@@ -49,7 +49,7 @@ def down
it 'adds an offence if inheriting from ActiveRecord::Migration' do it 'adds an offence if inheriting from ActiveRecord::Migration' do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class MyMigration < ActiveRecord::Migration[6.1] class MyMigration < ActiveRecord::Migration[6.1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. Use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. Use Gitlab::Database::Migration[#{described_class::CURRENT_MIGRATION_VERSION}] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.
end end
RUBY RUBY
end end
...@@ -57,23 +57,23 @@ class MyMigration < ActiveRecord::Migration[6.1] ...@@ -57,23 +57,23 @@ class MyMigration < ActiveRecord::Migration[6.1]
it 'adds an offence if inheriting from old version of Gitlab::Database::Migration' do it 'adds an offence if inheriting from old version of Gitlab::Database::Migration' do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class MyMigration < Gitlab::Database::Migration[2.0] class MyMigration < Gitlab::Database::Migration[2.0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. Use Gitlab::Database::Migration[2.1] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration or old versions of Gitlab::Database::Migration. Use Gitlab::Database::Migration[#{described_class::CURRENT_MIGRATION_VERSION}] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.
end end
RUBY RUBY
end end
it 'adds an offence if including Gitlab::Database::MigrationHelpers directly' do it 'adds an offence if including Gitlab::Database::MigrationHelpers directly' do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class MyMigration < Gitlab::Database::Migration[2.1] class MyMigration < Gitlab::Database::Migration[#{described_class::CURRENT_MIGRATION_VERSION}]
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't include migration helper modules directly. Inherit from Gitlab::Database::Migration[#{described_class::CURRENT_MIGRATION_VERSION}] instead. See https://docs.gitlab.com/ee/development/migration_style_guide.html#migration-helpers-and-versioning.
end end
RUBY RUBY
end end
it 'excludes ActiveRecord classes defined inside the migration' do it 'excludes ActiveRecord classes defined inside the migration' do
expect_no_offenses(<<~RUBY) expect_no_offenses(<<~RUBY)
class TestMigration < Gitlab::Database::Migration[2.1] class TestMigration < Gitlab::Database::Migration[#{described_class::CURRENT_MIGRATION_VERSION}]
class TestModel < ApplicationRecord class TestModel < ApplicationRecord
end end
...@@ -85,7 +85,7 @@ class AnotherTestModel < ActiveRecord::Base ...@@ -85,7 +85,7 @@ class AnotherTestModel < ActiveRecord::Base
it 'excludes parentless classes defined inside the migration' do it 'excludes parentless classes defined inside the migration' do
expect_no_offenses(<<~RUBY) expect_no_offenses(<<~RUBY)
class TestMigration < Gitlab::Database::Migration[2.1] class TestMigration < Gitlab::Database::Migration[#{described_class::CURRENT_MIGRATION_VERSION}]
class TestClass class TestClass
end end
end end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册