Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
古剑诛仙
soar
提交
fcba5e74
S
soar
项目概览
古剑诛仙
/
soar
与 Fork 源项目一致
Fork自
Xiaomi / soar
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
soar
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fcba5e74
编写于
12月 06, 2018
作者:
L
liipx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
COL.007 reward
上级
421a866d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
106 addition
and
4 deletion
+106
-4
advisor/heuristic.go
advisor/heuristic.go
+51
-0
advisor/heuristic_test.go
advisor/heuristic_test.go
+46
-0
advisor/index.go
advisor/index.go
+9
-4
未找到文件。
advisor/heuristic.go
浏览文件 @
fcba5e74
...
...
@@ -3272,6 +3272,57 @@ func (q *Query4Audit) RuleMaxTextColsCount() Rule {
return
rule
}
// RuleMaxTextColsCount COL.007 checking for existed table
func
(
idxAdv
*
IndexAdvisor
)
RuleMaxTextColsCount
()
Rule
{
rule
:=
HeuristicRules
[
"OK"
]
// 未开启测试环境不进行检查
if
common
.
Config
.
TestDSN
.
Disable
{
return
rule
}
sqlparser
.
Walk
(
func
(
node
sqlparser
.
SQLNode
)
(
kontinue
bool
,
err
error
)
{
switch
stmt
:=
node
.
(
type
)
{
case
*
sqlparser
.
DDL
:
if
stmt
.
Action
!=
"alter"
{
return
true
,
nil
}
tb
:=
stmt
.
Table
// 此处的检查需要在测试环境中的临时数据库中进行检查
// 需要将测试环境 DSN 的数据库暂时指向临时数据库
// 为了防止影响切换数据库环境会影响接下来的测试,需要在检查完后将原配置还原
dbTmp
:=
idxAdv
.
vEnv
.
Database
idxAdv
.
vEnv
.
Database
=
idxAdv
.
vEnv
.
DBRef
[
idxAdv
.
vEnv
.
Database
]
defer
func
()
{
idxAdv
.
vEnv
.
Database
=
dbTmp
}()
// 添加字段的语句会在初始化环境的时候被执行
// 只需要获取该标的 CREAET 语句,后再对该语句进行检查即可
ddl
,
err
:=
idxAdv
.
vEnv
.
ShowCreateTable
(
tb
.
Name
.
String
())
if
err
!=
nil
{
common
.
Log
.
Error
(
"RuleMaxTextColsCount create statement got failed: %s"
,
err
.
Error
())
return
false
,
err
}
q
,
err
:=
NewQuery4Audit
(
ddl
)
if
err
!=
nil
{
return
false
,
err
}
r
:=
q
.
RuleMaxTextColsCount
()
if
r
.
Item
!=
"OK"
{
rule
=
r
return
false
,
nil
}
}
return
true
,
nil
},
idxAdv
.
Ast
)
return
rule
}
// RuleAllowEngine TBL.002
func
(
q
*
Query4Audit
)
RuleAllowEngine
()
Rule
{
var
rule
=
q
.
RuleOK
()
...
...
advisor/heuristic_test.go
浏览文件 @
fcba5e74
...
...
@@ -23,7 +23,10 @@ import (
"github.com/XiaoMi/soar/common"
"github.com/XiaoMi/soar/env"
"github.com/kr/pretty"
"strings"
"vitess.io/vitess/go/vt/sqlparser"
)
// ALI.001
...
...
@@ -3113,6 +3116,49 @@ func TestRuleMaxTextColsCount(t *testing.T) {
common
.
Log
.
Debug
(
"Exiting function: %s"
,
common
.
GetFunctionName
())
}
// COL.007
func
TestRuleMaxTextColsCountWithEnv
(
t
*
testing
.
T
)
{
common
.
Log
.
Debug
(
"Entering function: %s"
,
common
.
GetFunctionName
())
dsn
:=
common
.
Config
.
OnlineDSN
common
.
Config
.
OnlineDSN
=
common
.
Config
.
TestDSN
vEnv
,
rEnv
:=
env
.
BuildEnv
()
defer
vEnv
.
CleanUp
()
initSQLs
:=
[]
string
{
`CREATE TABLE t1 (id int, title text, content blob);`
,
"alter table t1 add column other text;"
,
}
for
_
,
sql
:=
range
initSQLs
{
vEnv
.
BuildVirtualEnv
(
rEnv
,
sql
)
if
!
strings
.
HasPrefix
(
strings
.
ToLower
(
sql
),
"alter"
)
{
continue
}
stmt
,
syntaxErr
:=
sqlparser
.
Parse
(
sql
)
if
syntaxErr
!=
nil
{
common
.
Log
.
Critical
(
"Syntax Error: %v, SQL: %s"
,
syntaxErr
,
sql
)
}
q
:=
&
Query4Audit
{
Query
:
sql
,
Stmt
:
stmt
}
idxAdvisor
,
err
:=
NewAdvisor
(
vEnv
,
*
rEnv
,
*
q
)
if
err
!=
nil
{
t
.
Error
(
"NewAdvisor Error: "
,
err
,
"SQL: "
,
sql
)
}
if
idxAdvisor
!=
nil
{
rule
:=
idxAdvisor
.
RuleMaxTextColsCount
()
if
rule
.
Item
!=
"COL.007"
{
t
.
Error
(
"Rule not match:"
,
rule
,
"Expect : COL.007, SQL:"
,
sql
)
}
}
}
common
.
Log
.
Debug
(
"Exiting function: %s"
,
common
.
GetFunctionName
())
common
.
Config
.
OnlineDSN
=
dsn
}
// TBL.002
func
TestRuleAllowEngine
(
t
*
testing
.
T
)
{
common
.
Log
.
Debug
(
"Entering function: %s"
,
common
.
GetFunctionName
())
...
...
advisor/index.go
浏览文件 @
fcba5e74
...
...
@@ -111,7 +111,11 @@ func NewAdvisor(env *env.VirtualEnv, rEnv database.Connector, q Query4Audit) (*I
}
}
return
nil
,
nil
return
&
IndexAdvisor
{
vEnv
:
env
,
rEnv
:
rEnv
,
Ast
:
q
.
Stmt
,
},
nil
case
*
sqlparser
.
DBDDL
:
// 忽略建库语句
...
...
@@ -1011,11 +1015,12 @@ func (idxAdv *IndexAdvisor) HeuristicCheck(q Query4Audit) map[string]Rule {
}
ruleFuncs
:=
[]
func
(
*
IndexAdvisor
)
Rule
{
(
*
IndexAdvisor
)
.
RuleMaxTextColsCount
,
// COL.007
(
*
IndexAdvisor
)
.
RuleImplicitConversion
,
// ARG.003
(
*
IndexAdvisor
)
.
RuleGroupByConst
,
// CLA.004
(
*
IndexAdvisor
)
.
RuleOrderByConst
,
// CLA.005
(
*
IndexAdvisor
)
.
RuleUpdatePrimaryKey
,
// CLA.016
// (*IndexAdvisor).RuleImpossibleOuterJoin, // TODO: JOI.003, JOI.004
(
*
IndexAdvisor
)
.
RuleGroupByConst
,
// CLA.004
(
*
IndexAdvisor
)
.
RuleOrderByConst
,
// CLA.005
(
*
IndexAdvisor
)
.
RuleUpdatePrimaryKey
,
// CLA.016
}
for
_
,
f
:=
range
ruleFuncs
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录