Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(sql-review): check top level sql for dry run (#10846)

rebelice5235beb140e1
diff --git a/backend/plugin/advisor/mysql/advisor_statement_dml_dry_run.go b/backend/plugin/advisor/mysql/advisor_statement_dml_dry_run.go
index 9c0f816f8a..c4618a010d 100644
--- a/backend/plugin/advisor/mysql/advisor_statement_dml_dry_run.go
+++ b/backend/plugin/advisor/mysql/advisor_statement_dml_dry_run.go
@@ -74,39 +74,70 @@ type statementDmlDryRunChecker struct {
adviceList []advisor.Advice
level advisor.Status
title string
- text string
line int
driver *sql.DB
ctx context.Context
}
-func (checker *statementDmlDryRunChecker) EnterQuery(ctx *mysql.QueryContext) {
- checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
-}
-
// EnterUpdateStatement is called when production updateStatement is entered.
func (checker *statementDmlDryRunChecker) EnterUpdateStatement(ctx *mysql.UpdateStatementContext) {
- checker.handleStmt(ctx.GetStart().GetLine())
+ if ctx.GetParent() == nil {
+ return
+ }
+ if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ if ctx.GetParent().GetParent() == nil {
+ return
+ }
+ if _, ok := ctx.GetParent().GetParent().(*mysql.QueryContext); !ok {
+ return
+ }
+ checker.handleStmt(ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx), ctx.GetStart().GetLine())
}
// EnterDeleteStatement is called when production deleteStatement is entered.
func (checker *statementDmlDryRunChecker) EnterDeleteStatement(ctx *mysql.DeleteStatementContext) {
- checker.handleStmt(ctx.GetStart().GetLine())
+ if ctx.GetParent() == nil {
+ return
+ }
+ if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ if ctx.GetParent().GetParent() == nil {
+ return
+ }
+ if _, ok := ctx.GetParent().GetParent().(*mysql.QueryContext); !ok {
+ return
+ }
+ checker.handleStmt(ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx), ctx.GetStart().GetLine())
}
// EnterInsertStatement is called when production insertStatement is entered.
func (checker *statementDmlDryRunChecker) EnterInsertStatement(ctx *mysql.InsertStatementContext) {
- checker.handleStmt(ctx.GetStart().GetLine())
+ if ctx.GetParent() == nil {
+ return
+ }
+ if _, ok := ctx.GetParent().(*mysql.SimpleStatementContext); !ok {
+ return
+ }
+ if ctx.GetParent().GetParent() == nil {
+ return
+ }
+ if _, ok := ctx.GetParent().GetParent().(*mysql.QueryContext); !ok {
+ return
+ }
+ checker.handleStmt(ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx), ctx.GetStart().GetLine())
}
// Enter implements the ast.Visitor interface.
-func (checker *statementDmlDryRunChecker) handleStmt(lineNumber int) {
- if _, err := advisor.Query(checker.ctx, checker.driver, fmt.Sprintf("EXPLAIN %s", checker.text)); err != nil {
+func (checker *statementDmlDryRunChecker) handleStmt(text string, lineNumber int) {
+ if _, err := advisor.Query(checker.ctx, checker.driver, fmt.Sprintf("EXPLAIN %s", text)); err != nil {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
Code: advisor.StatementDMLDryRunFailed,
Title: checker.title,
- Content: fmt.Sprintf("\"%s\" dry runs failed: %s", checker.text, err.Error()),
+ Content: fmt.Sprintf("\"%s\" dry runs failed: %s", text, err.Error()),
Line: checker.line + lineNumber,
})
}
diff --git a/backend/tests/test-data/sql_review_mysql.yaml b/backend/tests/test-data/sql_review_mysql.yaml
index 166f0e448d..356fa09fb4 100644
--- a/backend/tests/test-data/sql_review_mysql.yaml
+++ b/backend/tests/test-data/sql_review_mysql.yaml
@@ -336,7 +336,7 @@
}
}
run: false
-- statement: ' DELETE FROM tech_book'
+- statement: " DELETE FROM tech_book"
result:
- |-
{
@@ -352,7 +352,7 @@
{
"status": "ERROR",
"title": "statement.dml-dry-run",
- "content": "\"DELETE FROM tech_book;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "content": "\"DELETE FROM tech_book\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
"sqlReviewReport": {
"line": 1,
"code": 208
@@ -385,7 +385,7 @@
{
"status": "ERROR",
"title": "statement.dml-dry-run",
- "content": "\"DELETE FROM tech_book WHERE name like \"%abc\";\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "content": "\"DELETE FROM tech_book WHERE name like \"%abc\"\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
"sqlReviewReport": {
"line": 1,
"code": 208
@@ -402,7 +402,7 @@
}
}
run: false
-- statement: ' INSERT INTO t_copy SELECT * FROM t'
+- statement: " INSERT INTO t_copy SELECT * FROM t"
result:
- |-
{
@@ -428,7 +428,7 @@
{
"status": "ERROR",
"title": "statement.dml-dry-run",
- "content": "\"INSERT INTO t_copy SELECT * FROM t;\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist",
+ "content": "\"INSERT INTO t_copy SELECT * FROM t\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t_copy' doesn't exist",
"sqlReviewReport": {
"line": 1,
"code": 208
@@ -455,13 +455,13 @@
}
}
run: false
-- statement: ' INSERT INTO t VALUES (1, 1, now(), 1, now())'
+- statement: " INSERT INTO t VALUES (1, 1, now(), 1, now())"
result:
- |-
{
"status": "ERROR",
"title": "statement.dml-dry-run",
- "content": "\"INSERT INTO t VALUES (1, 1, now(), 1, now());\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t' doesn't exist",
+ "content": "\"INSERT INTO t VALUES (1, 1, now(), 1, now())\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.t' doesn't exist",
"sqlReviewReport": {
"line": 1,
"code": 208
@@ -484,7 +484,7 @@
{
"status": "ERROR",
"title": "statement.dml-dry-run",
- "content": "\"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase');\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
+ "content": "\"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')\" dry runs failed: Error 1146 (42S02): Table 'testsqlreview.tech_book' doesn't exist",
"sqlReviewReport": {
"line": 1,
"code": 208
@@ -595,7 +595,7 @@
}
}
run: false
-- statement: ' ALTER TABLE user PARTITION BY HASH(id) PARTITIONS 8;'
+- statement: " ALTER TABLE user PARTITION BY HASH(id) PARTITIONS 8;"
result:
- |-
{
@@ -663,7 +663,7 @@
}
}
run: false
-- statement: ' DROP TABLE user;'
+- statement: " DROP TABLE user;"
result:
- |-
{