Skip to content

Commit

Permalink
support new heuristic rules check
Browse files Browse the repository at this point in the history
  COL.014 support NVARCHAR
  COL.013 support zero time
  • Loading branch information
martianzhang committed Aug 30, 2019
1 parent 68128b7 commit 50a0bc2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
34 changes: 32 additions & 2 deletions advisor/heuristic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions advisor/heuristic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down Expand Up @@ -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))",
},
// 反面的例子
{
Expand Down
4 changes: 2 additions & 2 deletions advisor/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions doc/heuristic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50a0bc2

Please sign in to comment.