Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 审核插件 AuditResult 以及相关函数格式变更 #1426

Merged
merged 7 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 59 additions & 58 deletions sqle/driver/mysql/audit.go

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions sqle/driver/mysql/audit_executed_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ ALTER TABLE exist_db.not_exist_tb_1 ADD COLUMN b2 blob UNIQUE KEY COMMENT "unit
ALTER TABLE exist_db.not_exist_tb_1 MODIFY COLUMN b1 blob UNIQUE KEY COMMENT "unit test";
`,
newTestResult().addResult(rulepkg.DDLCheckIndexedColumnWithBlob).
add(driverV2.RuleLevelWarn, "建表DDL必须包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "建表DDL必须包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
add(driverV2.RuleLevelWarn, "", "建表DDL必须包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "", "建表DDL必须包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
newTestResult(),
newTestResult(),
newTestResult(),
Expand Down Expand Up @@ -197,8 +197,9 @@ v2 varchar(255) NOT NULL DEFAULT "unit test" COMMENT "unit test"
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
ALTER TABLE exist_db.exist_tb_1 Add primary key(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckPKWithoutAutoIncrement).add(driverV2.RuleLevelWarn, "建表DDL必须包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "建表DDL必须包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
newTestResult().addResult(rulepkg.DDLCheckPKWithoutAutoIncrement).
add(driverV2.RuleLevelWarn, "", "建表DDL必须包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "", "建表DDL必须包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
newTestResult(),
)

Expand All @@ -215,8 +216,9 @@ PRIMARY KEY (id)
)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT="unit test";
ALTER TABLE exist_db.exist_tb_1 Add primary key(v1);
`,
newTestResult().addResult(rulepkg.DDLCheckPKWithoutBigintUnsigned).add(driverV2.RuleLevelWarn, "建表DDL必须包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "建表DDL必须包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
newTestResult().addResult(rulepkg.DDLCheckPKWithoutBigintUnsigned).
add(driverV2.RuleLevelWarn, "", "建表DDL必须包含CREATE_TIME字段且默认值为CURRENT_TIMESTAMP").
add(driverV2.RuleLevelWarn, "", "建表DDL必须包含UPDATE_TIME字段且默认值为CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"),
newTestResult(),
)

Expand Down
21 changes: 13 additions & 8 deletions sqle/driver/mysql/audit_offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,8 @@ select id from exist_db.exist_tb_1 where id =1 limit 1001;
`
select id from exist_db.exist_tb_1 where id =1 limit 2, 1001;
`,
newTestResult().addResult(rulepkg.DMLCheckSelectLimit, 1000).add(driverV2.RuleLevelNotice, "使用LIMIT分页时,避免使用LIMIT M,N"),
newTestResult().addResult(rulepkg.DMLCheckSelectLimit, 1000).
add(driverV2.RuleLevelNotice, "", "使用LIMIT分页时,避免使用LIMIT M,N"),
)

runDefaultRulesInspectCase(t, "failed nil", DefaultMysqlInspectOffline(),
Expand Down Expand Up @@ -2149,7 +2150,7 @@ SELECT * FROM exist_db.exist_tb_1;
OPTIMIZE TABLE exist_db.exist_tb_1;
SELECT * FROM exist_db.exist_tb_2;
`, newTestResult().addResult(rulepkg.DMLCheckWhereIsInvalid),
newTestResult().add(driverV2.RuleLevelWarn, "语法错误或者解析器不支持,请人工确认SQL正确性"),
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"),
newTestResult().addResult(rulepkg.DMLCheckWhereIsInvalid))
}

Expand Down Expand Up @@ -2180,7 +2181,8 @@ CREATE
BEFORE INSERT ON t1 FOR EACH ROW insert into t2(id, c1) values(1, '2');
`,
} {
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspectOffline(), sql, newTestResult().add(driverV2.RuleLevelWarn, "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateTrigger))
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspectOffline(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateTrigger))
}

for _, sql := range []string{
Expand All @@ -2190,7 +2192,8 @@ CREATE
`CREATE TRIGGER BEFORE INSERT ON t1 FOR EACH ROW insert into t2(id, c1) values(1, '2');`,
`CREATE TRIGGER my_trigger BEEEFORE INSERT ON t1 FOR EACH ROW insert into t2(id, c1) values(1, '2');`,
} {
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspectOffline(), sql, newTestResult().add(driverV2.RuleLevelWarn, "语法错误或者解析器不支持,请人工确认SQL正确性"))
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspectOffline(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"))
}
}

Expand All @@ -2207,7 +2210,8 @@ CREATE
RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ',s,'!');
`,
} {
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspectOffline(), sql, newTestResult().add(driverV2.RuleLevelWarn, "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateFunction))
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspectOffline(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateFunction))
}

for _, sql := range []string{
Expand All @@ -2216,7 +2220,8 @@ CREATE
`CREATE hello_function (s CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ',s,'!');`,
`CREATE DEFINER='sqle_op'@'localhost' hello (s CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ',s,'!');`,
} {
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspectOffline(), sql, newTestResult().add(driverV2.RuleLevelWarn, "语法错误或者解析器不支持,请人工确认SQL正确性"))
runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspectOffline(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"))
}
}

Expand Down Expand Up @@ -2263,7 +2268,7 @@ select * from t1;`,
runSingleRuleInspectCase(
rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "",
DefaultMysqlInspectOffline(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "语法错误或者解析器不支持,请人工确认SQL正确性").
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").
addResult(rulepkg.DDLCheckCreateProcedure))
}

Expand Down Expand Up @@ -2298,7 +2303,7 @@ end;`,
runSingleRuleInspectCase(
rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "",
DefaultMysqlInspectOffline(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "语法错误或者解析器不支持,请人工确认SQL正确性"))
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"))
}
}

Expand Down
Loading