Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
容器镜像库
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
f7b456da
提交
f7b456da
编辑于
3 years ago
作者:
Andreas Brandl
浏览文件
操作
下载
补丁
差异文件
Cop for database migration class
上级
137f716c
No related branches found
分支 包含提交
No related tags found
标签 包含提交
2 合并请求
!106
Draft: This MR is specified for doc sync check
,
!105
Draft:This MR is specified for docsynccheck, will not be merged
变更
2
隐藏空白变更内容
行内
左右并排
显示
2 个更改的文件
rubocop/cop/migration/versioned_migration_class.rb
+55
-0
55 个添加, 0 个删除
rubocop/cop/migration/versioned_migration_class.rb
spec/rubocop/cop/migration/versioned_migration_class_spec.rb
+69
-0
69 个添加, 0 个删除
spec/rubocop/cop/migration/versioned_migration_class_spec.rb
有
124 个添加
和
0 个删除
rubocop/cop/migration/versioned_migration_class.rb
0 → 100644
+
55
−
0
浏览文件 @
f7b456da
# frozen_string_literal: true
require_relative
'../../migration_helpers'
module
RuboCop
module
Cop
module
Migration
class
VersionedMigrationClass
<
RuboCop
::
Cop
::
Cop
include
MigrationHelpers
ENFORCED_SINCE
=
2021_08_30_00_00_00
MSG_INHERIT
=
'Don\'t inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[1.0] instead.'
MSG_INCLUDE
=
'Don\'t include migration helper modules directly. Inherit from Gitlab::Database::Migration[1.0] instead.'
MIGRATION_CLASS
=
'Gitlab::Database::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
)
add_offense
(
node
,
location: :expression
,
message:
MSG_INHERIT
)
unless
gitlab_migration_class?
(
node
)
end
def
on_send
(
node
)
return
unless
relevant_migration?
(
node
)
add_offense
(
node
,
location: :expression
,
message:
MSG_INCLUDE
)
if
includes_helpers?
(
node
)
end
private
def
relevant_migration?
(
node
)
in_migration?
(
node
)
&&
version
(
node
)
>=
ENFORCED_SINCE
end
def
gitlab_migration_class?
(
node
)
superclass
(
node
)
==
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
此差异已折叠。
点击以展开。
spec/rubocop/cop/migration/versioned_migration_class_spec.rb
0 → 100644
+
69
−
0
浏览文件 @
f7b456da
# frozen_string_literal: true
require
'fast_spec_helper'
require_relative
'../../../../rubocop/cop/migration/versioned_migration_class'
RSpec
.
describe
RuboCop
::
Cop
::
Migration
::
VersionedMigrationClass
do
subject
(
:cop
)
{
described_class
.
new
}
let
(
:migration
)
do
<<~
SOURCE
class TestMigration < Gitlab::Database::Migration[1.0]
def up
execute 'select 1'
end
def down
execute 'select 1'
end
end
SOURCE
end
shared_examples
'a disabled cop'
do
it
'does not register any offenses'
do
expect_no_offenses
(
migration
)
end
end
context
'outside of a migration'
do
it_behaves_like
'a disabled cop'
end
context
'in migration'
do
before
do
allow
(
cop
).
to
receive
(
:in_migration?
).
and_return
(
true
)
end
context
'in an old migration'
do
before
do
allow
(
cop
).
to
receive
(
:version
).
and_return
(
described_class
::
ENFORCED_SINCE
-
5
)
end
it_behaves_like
'a disabled cop'
end
context
'that is recent'
do
before
do
allow
(
cop
).
to
receive
(
:version
).
and_return
(
described_class
::
ENFORCED_SINCE
+
5
)
end
it
'adds an offence if inheriting from ActiveRecord::Migration'
do
expect_offense
(
<<~
RUBY
)
class MyMigration < ActiveRecord::Migration[6.1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't inherit from ActiveRecord::Migration but use Gitlab::Database::Migration[1.0] instead.
end
RUBY
end
it
'adds an offence if including Gitlab::Database::MigrationHelpers directly'
do
expect_offense
(
<<~
RUBY
)
class MyMigration < Gitlab::Database::Migration[1.0]
include Gitlab::Database::MigrationHelpers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't include migration helper modules directly. Inherit from Gitlab::Database::Migration[1.0] instead.
end
RUBY
end
end
end
end
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录