Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Xiaomi
soar
提交
50a0bc2e
S
soar
项目概览
Xiaomi
/
soar
大约 1 年 前同步成功
通知
387
Star
8512
Fork
1328
代码
文件
提交
分支
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,发现更多精彩内容 >>
提交
50a0bc2e
编写于
8月 30, 2019
作者:
martianzhang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support new heuristic rules check
COL.014 support NVARCHAR COL.013 support zero time
上级
68128b73
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
41 addition
and
6 deletion
+41
-6
advisor/heuristic.go
advisor/heuristic.go
+32
-2
advisor/heuristic_test.go
advisor/heuristic_test.go
+5
-0
advisor/rules.go
advisor/rules.go
+2
-2
doc/heuristic.md
doc/heuristic.md
+2
-2
未找到文件。
advisor/heuristic.go
浏览文件 @
50a0bc2e
...
...
@@ -27,9 +27,11 @@ import (
"github.com/XiaoMi/soar/ast"
"github.com/XiaoMi/soar/common"
"github.com/XiaoMi/soar/database"
"github.com/gedex/inflector"
"github.com/percona/go-mysql/query"
tidb
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/format"
"github.com/pingcap/parser/mysql"
"github.com/tidwall/gjson"
"vitess.io/vitess/go/vt/sqlparser"
...
...
@@ -2870,11 +2872,20 @@ func (q *Query4Audit) RuleTimestampDefault() Rule {
if
col
.
Tp
==
nil
{
continue
}
if
col
.
Tp
.
Tp
==
mysql
.
TypeTimestamp
{
switch
col
.
Tp
.
Tp
{
case
mysql
.
TypeTimestamp
,
mysql
.
TypeDate
,
mysql
.
TypeDatetime
,
mysql
.
TypeNewDate
:
hasDefault
:=
false
var
sb
strings
.
Builder
ctx
:=
format
.
NewRestoreCtx
(
format
.
DefaultRestoreFlags
,
&
sb
)
for
_
,
option
:=
range
col
.
Options
{
if
option
.
Tp
==
tidb
.
ColumnOptionDefaultValue
{
hasDefault
=
true
if
err
:=
option
.
Restore
(
ctx
);
err
==
nil
{
if
strings
.
HasPrefix
(
sb
.
String
(),
`DEFAULT '0`
)
||
strings
.
HasPrefix
(
sb
.
String
(),
`DEFAULT 0`
)
{
hasDefault
=
false
}
}
}
}
if
!
hasDefault
{
...
...
@@ -2894,11 +2905,20 @@ func (q *Query4Audit) RuleTimestampDefault() Rule {
if
col
.
Tp
==
nil
{
continue
}
if
col
.
Tp
.
Tp
==
mysql
.
TypeTimestamp
{
var
sb
strings
.
Builder
ctx
:=
format
.
NewRestoreCtx
(
format
.
DefaultRestoreFlags
,
&
sb
)
switch
col
.
Tp
.
Tp
{
case
mysql
.
TypeTimestamp
,
mysql
.
TypeDate
,
mysql
.
TypeDatetime
,
mysql
.
TypeNewDate
:
hasDefault
:=
false
for
_
,
option
:=
range
col
.
Options
{
if
option
.
Tp
==
tidb
.
ColumnOptionDefaultValue
{
hasDefault
=
true
if
err
:=
option
.
Restore
(
ctx
);
err
==
nil
{
if
strings
.
HasPrefix
(
sb
.
String
(),
`DEFAULT '0`
)
||
strings
.
HasPrefix
(
sb
.
String
(),
`DEFAULT 0`
)
{
hasDefault
=
false
}
}
}
}
if
!
hasDefault
{
...
...
@@ -2938,6 +2958,16 @@ func (q *Query4Audit) RuleAutoIncrementInitNotZero() Rule {
// RuleColumnWithCharset COL.014
func
(
q
*
Query4Audit
)
RuleColumnWithCharset
()
Rule
{
var
rule
=
q
.
RuleOK
()
tks
:=
ast
.
Tokenize
(
q
.
Query
)
for
_
,
tk
:=
range
tks
{
if
tk
.
Type
==
ast
.
TokenTypeWord
{
switch
strings
.
TrimSpace
(
strings
.
ToLower
(
tk
.
Val
))
{
case
"national"
,
"nvarchar"
,
"nchar"
,
"nvarchar("
,
"nchar("
,
"character"
:
rule
=
HeuristicRules
[
"COL.014"
]
return
rule
}
}
}
switch
q
.
Stmt
.
(
type
)
{
case
*
sqlparser
.
DDL
:
for
_
,
tiStmt
:=
range
q
.
TiStmt
{
...
...
advisor/heuristic_test.go
浏览文件 @
50a0bc2e
...
...
@@ -2769,6 +2769,9 @@ func TestRuleTimestampDefault(t *testing.T) {
{
"CREATE TABLE tbl( `id` bigint not null, `create_time` timestamp) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
,
"ALTER TABLE t1 MODIFY b timestamp NOT NULL;"
,
`ALTER TABLE t1 ADD c_time timestamp NOT NULL default "0000-00-00"`
,
`ALTER TABLE t1 ADD c_time timestamp NOT NULL default 0`
,
`ALTER TABLE t1 ADD c_time datetime NOT NULL default 0`
,
},
{
"CREATE TABLE tbl (`id` bigint not null, `update_time` timestamp default current_timestamp)"
,
...
...
@@ -2850,6 +2853,8 @@ func TestRuleColumnWithCharset(t *testing.T) {
{
"CREATE TABLE `tb2` ( `id` int(11) DEFAULT NULL, `col` char(10) CHARACTER SET utf8 DEFAULT NULL)"
,
"alter table tb2 change col col char(10) CHARACTER SET utf8 DEFAULT NULL;"
,
"CREATE TABLE tb (a nvarchar(10))"
,
"CREATE TABLE tb (a nchar(10))"
,
},
// 反面的例子
{
...
...
advisor/rules.go
浏览文件 @
50a0bc2e
...
...
@@ -523,8 +523,8 @@ func init() {
"COL.013"
:
{
Item
:
"COL.013"
,
Severity
:
"L4"
,
Summary
:
"TIMESTAMP 类型
未设置默认值
"
,
Content
:
`TIMESTAMP 类型
未设置默认值
`
,
Summary
:
"TIMESTAMP 类型
默认值检查异常
"
,
Content
:
`TIMESTAMP 类型
建议设置默认值,且不建议使用 0 或 0000-00-00 00:00:00 作为默认值。可以考虑使用 1970-08-02 01:01:01
`
,
Case
:
"CREATE TABLE tbl( `id` bigint not null, `create_time` timestamp);"
,
Func
:
(
*
Query4Audit
)
.
RuleTimestampDefault
,
},
...
...
doc/heuristic.md
浏览文件 @
50a0bc2e
...
...
@@ -472,11 +472,11 @@ select c1,c2,c3 from tbl where c4 is null or c4 <> 1
```
sql
CREATE
TABLE
`tb`
(
`c`
longblob
NOT
NULL
);
```
## TIMESTAMP 类型
未设置默认值
## TIMESTAMP 类型
默认值检查异常
*
**Item**
:COL.013
*
**Severity**
:L4
*
**Content**
:TIMESTAMP 类型
未设置默认值
*
**Content**
:TIMESTAMP 类型
建议设置默认值,且不建议使用 0 或 0000-00-00 00:00:00 作为默认值。可以考虑使用 1970-08-02 01:01:01
*
**Case**
:
```
sql
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录