Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: remove disallow mixing ddl dml check in statement type check (#11136)
p0ny15a9f8fcc562
diff --git a/backend/runner/plancheck/statement_type_executor.go b/backend/runner/plancheck/statement_type_executor.goindex c4647191ef..9466bdc0fb 100644--- a/backend/runner/plancheck/statement_type_executor.go+++ b/backend/runner/plancheck/statement_type_executor.go@@ -121,13 +121,13 @@ func (e *StatementTypeExecutor) runForDatabaseTarget(ctx context.Context, configvar results []*storepb.PlanCheckRunResult_Resultswitch instance.Engine {case storepb.Engine_POSTGRES, storepb.Engine_RISINGWAVE:- checkResults, err := postgresqlStatementTypeCheck(renderedStatement, changeType)+ checkResults, err := postgresqlStatementTypeCheck(renderedStatement)if err != nil {return nil, err}results = append(results, checkResults...)case storepb.Engine_TIDB:- checkResults, err := tidbStatementTypeCheck(renderedStatement, dbSchema.GetMetadata().CharacterSet, dbSchema.GetMetadata().Collation, changeType)+ checkResults, err := tidbStatementTypeCheck(renderedStatement, dbSchema.GetMetadata().CharacterSet, dbSchema.GetMetadata().Collation)if err != nil {return nil, err}@@ -140,7 +140,7 @@ func (e *StatementTypeExecutor) runForDatabaseTarget(ctx context.Context, configresults = append(results, sdlAdvice...)}case storepb.Engine_MYSQL, storepb.Engine_OCEANBASE:- checkResults, err := mysqlStatementTypeCheck(renderedStatement, changeType)+ checkResults, err := mysqlStatementTypeCheck(renderedStatement)if err != nil {return nil, err}@@ -296,13 +296,13 @@ func (e *StatementTypeExecutor) runForDatabaseGroupTarget(ctx context.Context, cvar results []*storepb.PlanCheckRunResult_Resultswitch instance.Engine {case storepb.Engine_POSTGRES, storepb.Engine_RISINGWAVE:- checkResults, err := postgresqlStatementTypeCheck(renderedStatement, changeType)+ checkResults, err := postgresqlStatementTypeCheck(renderedStatement)if err != nil {return nil, err}results = append(results, checkResults...)case storepb.Engine_TIDB:- checkResults, err := tidbStatementTypeCheck(renderedStatement, dbSchema.GetMetadata().CharacterSet, dbSchema.GetMetadata().Collation, changeType)+ checkResults, err := tidbStatementTypeCheck(renderedStatement, dbSchema.GetMetadata().CharacterSet, dbSchema.GetMetadata().Collation)if err != nil {return nil, err}@@ -315,7 +315,7 @@ func (e *StatementTypeExecutor) runForDatabaseGroupTarget(ctx context.Context, cresults = append(results, sdlAdvice...)}case storepb.Engine_MYSQL, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:- checkResults, err := mysqlStatementTypeCheck(renderedStatement, changeType)+ checkResults, err := mysqlStatementTypeCheck(renderedStatement)if err != nil {return nil, err}@@ -508,7 +508,7 @@ func tidbCreateAndDropDatabaseCheck(nodeList []tidbast.StmtNode) []*storepb.Planreturn results}-func mysqlStatementTypeCheck(statement string, changeType storepb.PlanCheckRunConfig_ChangeDatabaseType) ([]*storepb.PlanCheckRunResult_Result, error) {+func mysqlStatementTypeCheck(statement string) ([]*storepb.PlanCheckRunResult_Result, error) {stmts, err := mysqlparser.ParseMySQL(statement)if err != nil {// nolint:nilerr@@ -534,41 +534,6 @@ func mysqlStatementTypeCheck(statement string, changeType storepb.PlanCheckRunCo// Disallow CREATE/DROP DATABASE statements.results = append(results, mysqlCreateAndDropDatabaseCheck(stmts)...)- switch changeType {- case storepb.PlanCheckRunConfig_DML:- for _, node := range stmts {- checker := &mysqlparser.StatementTypeChecker{}- antlr.ParseTreeWalkerDefault.Walk(checker, node.Tree)- // We only want to disallow DDL statements in CHANGE DATA.- // We need to run some common statements, e.g. COMMIT.- if checker.IsDDL {- results = append(results, &storepb.PlanCheckRunResult_Result{- Status: storepb.PlanCheckRunResult_Result_WARNING,- Title: "Data change can only run DML",- Content: fmt.Sprintf("\"%s\" is not DML", checker.Text),- Code: common.TaskTypeNotDML.Int32(),- Report: nil,- })- }- }- case storepb.PlanCheckRunConfig_DDL, storepb.PlanCheckRunConfig_SDL:- for _, node := range stmts {- checker := &mysqlparser.StatementTypeChecker{}- antlr.ParseTreeWalkerDefault.Walk(checker, node.Tree)- if checker.IsDML || checker.IsExplain {- results = append(results, &storepb.PlanCheckRunResult_Result{- Status: storepb.PlanCheckRunResult_Result_WARNING,- Title: "Alter schema can only run DDL",- Content: fmt.Sprintf("\"%s\" is not DDL", checker.Text),- Code: common.TaskTypeNotDDL.Int32(),- Report: nil,- })- }- }- default:- return nil, common.Errorf(common.Invalid, "invalid check statement type task type: %s", changeType)- }-return results, nil}@@ -610,7 +575,7 @@ func (e *StatementTypeExecutor) mysqlSDLTypeCheck(ctx context.Context, newSchemareturn results, nil}-func tidbStatementTypeCheck(statement string, charset string, collation string, changeType storepb.PlanCheckRunConfig_ChangeDatabaseType) ([]*storepb.PlanCheckRunResult_Result, error) {+func tidbStatementTypeCheck(statement string, charset string, collation string) ([]*storepb.PlanCheckRunResult_Result, error) {p := tidbp.New()// To support MySQL8 window function syntax.// See https://github.com/bytebase/bytebase/issues/175.@@ -641,40 +606,6 @@ func tidbStatementTypeCheck(statement string, charset string, collation string,// Disallow CREATE/DROP DATABASE statements.results = append(results, tidbCreateAndDropDatabaseCheck(stmts)...)- switch changeType {- case storepb.PlanCheckRunConfig_DML:- for _, node := range stmts {- _, isDDL := node.(tidbast.DDLNode)- // We only want to disallow DDL statements in CHANGE DATA.- // We need to run some common statements, e.g. COMMIT.- if isDDL {- results = append(results, &storepb.PlanCheckRunResult_Result{- Status: storepb.PlanCheckRunResult_Result_WARNING,- Title: "Data change can only run DML",- Content: fmt.Sprintf("\"%s\" is not DML", node.Text()),- Code: common.TaskTypeNotDML.Int32(),- Report: nil,- })- }- }- case storepb.PlanCheckRunConfig_DDL, storepb.PlanCheckRunConfig_SDL:- for _, node := range stmts {- _, isDML := node.(tidbast.DMLNode)- _, isExplain := node.(*tidbast.ExplainStmt)- if isDML || isExplain {- results = append(results, &storepb.PlanCheckRunResult_Result{- Status: storepb.PlanCheckRunResult_Result_WARNING,- Title: "Alter schema can only run DDL",- Content: fmt.Sprintf("\"%s\" is not DDL", node.Text()),- Code: common.TaskTypeNotDDL.Int32(),- Report: nil,- })- }- }- default:- return nil, common.Errorf(common.Invalid, "invalid check statement type task type: %s", changeType)- }-return results, nil}@@ -703,7 +634,7 @@ func postgresqlCreateAndDropDatabaseCheck(nodeList []ast.Node) []*storepb.PlanChreturn result}-func postgresqlStatementTypeCheck(statement string, changeType storepb.PlanCheckRunConfig_ChangeDatabaseType) ([]*storepb.PlanCheckRunResult_Result, error) {+func postgresqlStatementTypeCheck(statement string) ([]*storepb.PlanCheckRunResult_Result, error) {stmts, err := pgrawparser.Parse(pgrawparser.ParseContext{}, statement)if err != nil {// nolint:nilerr@@ -729,40 +660,5 @@ func postgresqlStatementTypeCheck(statement string, changeType storepb.PlanCheck// Disallow CREATE/DROP DATABASE statements.results = append(results, postgresqlCreateAndDropDatabaseCheck(stmts)...)- switch changeType {- case storepb.PlanCheckRunConfig_DML:- for _, node := range stmts {- _, isDDL := node.(ast.DDLNode)- // We only want to disallow DDL statements in CHANGE DATA.- // We need to run some common statements, e.g. COMMIT.- if isDDL {- results = append(results, &storepb.PlanCheckRunResult_Result{- Status: storepb.PlanCheckRunResult_Result_WARNING,- Title: "Data change can only run DML",- Content: fmt.Sprintf("\"%s\" is not DML", node.Text()),- Code: common.TaskTypeNotDML.Int32(),- Report: nil,- })- }- }- case storepb.PlanCheckRunConfig_DDL, storepb.PlanCheckRunConfig_SDL:- for _, node := range stmts {- _, isDML := node.(ast.DMLNode)- _, isSelect := node.(*ast.SelectStmt)- _, isExplain := node.(*ast.ExplainStmt)- if isDML || isSelect || isExplain {- results = append(results, &storepb.PlanCheckRunResult_Result{- Status: storepb.PlanCheckRunResult_Result_WARNING,- Title: "Alter schema can only run DDL",- Content: fmt.Sprintf("\"%s\" is not DDL", node.Text()),- Code: common.TaskTypeNotDDL.Int32(),- Report: nil,- })- }- }- default:- return nil, common.Errorf(common.Invalid, "invalid check statement type task type: %s", changeType)- }-return results, nil}diff --git a/backend/runner/plancheck/statement_type_executor_test.go b/backend/runner/plancheck/statement_type_executor_test.goindex 62fb2ea5df..3e0e2878e7 100644--- a/backend/runner/plancheck/statement_type_executor_test.go+++ b/backend/runner/plancheck/statement_type_executor_test.go@@ -50,7 +50,7 @@ func runStatementTypeCheck(t *testing.T, file string, engineType storepb.Engine,var err errorswitch engineType {case storepb.Engine_MYSQL:- checkResults, err = mysqlStatementTypeCheck(test.Statement, test.ChangeType)+ checkResults, err = mysqlStatementTypeCheck(test.Statement)require.NoError(t, err)default:}diff --git a/backend/runner/plancheck/test/mysql_statement_type_check.yaml b/backend/runner/plancheck/test/mysql_statement_type_check.yamlindex 9a0dde42ee..7bc3638fee 100644--- a/backend/runner/plancheck/test/mysql_statement_type_check.yaml+++ b/backend/runner/plancheck/test/mysql_statement_type_check.yaml@@ -22,27 +22,13 @@SELECT tbl_temp1.fld_order_idFROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;changetype: 1- want:- - |-- {- "status": "WARNING",- "title": "Alter schema can only run DDL",- "content": "\"INSERT INTO tbl_temp2 (fld_id)\nSELECT tbl_temp1.fld_order_id\nFROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;\" is not DDL",- "code": 402- }+ want: []- statement: IMPORT TABLE FROM '/tmp/mysql-files/*.sdi';changetype: 1want: []- statement: TABLE a EXCEPT TABLE b;changetype: 1- want:- - |-- {- "status": "WARNING",- "title": "Alter schema can only run DDL",- "content": "\"TABLE a EXCEPT TABLE b;\" is not DDL",- "code": 402- }+ want: []- statement: EXPLAIN SELECT c->>"$.name" AS name FROM jempn WHERE g > 2;changetype: 1want: []@@ -110,28 +96,14 @@want: []- statement: ALTER FUNCTION hello COMMENT 'hello function';changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"ALTER FUNCTION hello COMMENT 'hello function';\" is not DML",- "code": 401- }+ want: []- statement: |-CREATE DEFINER = 'admin'@'localhost' PROCEDURE account_count()BEGINSELECT 'Number of accounts:', COUNT(*) FROM mysql.user;END;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"CREATE DEFINER = 'admin'@'localhost' PROCEDURE account_count()\nBEGIN\n SELECT 'Number of accounts:', COUNT(*) FROM mysql.user;\nEND;\" is not DML",- "code": 401- }+ want: []- statement: ALTER PROCEDURE account_count COMMENT 'this account_count';changetype: 1want: []@@ -142,14 +114,7 @@SELECT hello('world');changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"CREATE FUNCTION hello (s CHAR(20))\nRETURNS CHAR(50) DETERMINISTIC\nRETURN CONCAT('Hello, ',s,'!');\" is not DML",- "code": 401- }+ want: []- statement: |-delimiter //CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)@@ -159,27 +124,13 @@END//delimiter ;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"CREATE PROCEDURE citycount (IN country CHAR(3), OUT cities INT)\n BEGIN\n SELECT COUNT(*) INTO cities FROM world.city\n WHERE CountryCode = country;\n END;\" is not DML",- "code": 401- }+ want: []- statement: CALL citycount('JPN', @cities); -- cities in Japanchangetype: 2want: []- statement: ALTER EVENT no_such_event ON SCHEDULE EVERY '2:3' DAY_HOUR;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"ALTER EVENT no_such_event ON SCHEDULE EVERY '2:3' DAY_HOUR;\" is not DML",- "code": 401- }+ want: []- statement: |-CREATE EVENT myeventON SCHEDULE@@ -188,147 +139,63 @@DOUPDATE myschema.mytable SET mycol = mycol + 1;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"CREATE EVENT myevent\nON SCHEDULE\n EVERY 6 HOUR\nCOMMENT 'A sample comment.'\nDO\n UPDATE myschema.mytable SET mycol = mycol + 1;\" is not DML",- "code": 401- }+ want: []- statement: ALTER DATABASE bbdev READ ONLY = DEFAULT;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"ALTER DATABASE bbdev READ ONLY = DEFAULT;\" is not DML",- "code": 401- }+ want: []- statement: ALTER SCHEMA bbdev READ ONLY = DEFAULT;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"ALTER SCHEMA bbdev READ ONLY = DEFAULT;\" is not DML",- "code": 401- }+ want: []- statement: CREATE TABLE t1(a INT);changetype: 1want: []- statement: DELETE FROM t1;changetype: 1- want:- - |-- {- "status": "WARNING",- "title": "Alter schema can only run DDL",- "content": "\"DELETE FROM t1;\" is not DDL",- "code": 402- }+ want: []- statement: SELECT 1;changetype: 2want: []- statement: DROP TABLE t1;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"DROP TABLE t1;\" is not DML",- "code": 401- }+ want: []- statement: CREATE DATABASE bbdev;changetype: 1want:- |-{- "status": "ERROR",- "title": "Cannot create database",- "content": "The statement \"CREATE DATABASE bbdev;\" creates database",- "code": 404+ "status": "ERROR",+ "title": "Cannot create database",+ "content": "The statement \"CREATE DATABASE bbdev;\" creates database",+ "code": 404}- statement: DROP DATABASE bbdev;changetype: 2want:- |-{- "status": "ERROR",- "title": "Cannot drop database",- "content": "The statement \"DROP DATABASE bbdev;\" drops database",- "code": 403- }- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"DROP DATABASE bbdev;\" is not DML",- "code": 401+ "status": "ERROR",+ "title": "Cannot drop database",+ "content": "The statement \"DROP DATABASE bbdev;\" drops database",+ "code": 403}- statement: RENAME TABLE t1 To t2;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"RENAME TABLE t1 To t2;\" is not DML",- "code": 401- }+ want: []- statement: TRUNCATE TABLE t1;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"TRUNCATE TABLE t1;\" is not DML",- "code": 401- }+ want: []- statement: ALTER TABLE t1 ADD COLUMN col1 INT;changetype: 2- want:- - |-- {- "status": "WARNING",- "title": "Data change can only run DML",- "content": "\"ALTER TABLE t1 ADD COLUMN col1 INT;\" is not DML",- "code": 401- }+ want: []- statement: DELETE FROM t1 WHERE id = 1;changetype: 1- want:- - |-- {- "status": "WARNING",- "title": "Alter schema can only run DDL",- "content": "\"DELETE FROM t1 WHERE id = 1;\" is not DDL",- "code": 402- }+ want: []- statement: INSERT INTO t1(id) VALUE(1);changetype: 1- want:- - |-- {- "status": "WARNING",- "title": "Alter schema can only run DDL",- "content": "\"INSERT INTO t1(id) VALUE(1);\" is not DDL",- "code": 402- }+ want: []- statement: UPDATE t1 SET name = 'zp' WHERE id = 1;changetype: 1- want:- - |-- {- "status": "WARNING",- "title": "Alter schema can only run DDL",- "content": "\"UPDATE t1 SET name = 'zp' WHERE id = 1;\" is not DDL",- "code": 402- }+ want: []- statement: |-update t_ci_job_artifact_config cinner join (@@ -346,14 +213,7 @@set c.structure_id = tmp.structure_idwhere c.structure_id is null;changetype: 1- want:- - |-- {- "status": "WARNING",- "title": "Alter schema can only run DDL",- "content": "\"update t_ci_job_artifact_config c\ninner join (\nselect\nstructure.id as structure_id,\nstructure.job_id as job_id\nfrom\nt_ci_job_stage_group_structure structure,\nt_ci_stage_template_group stg\nwhere\nstructure.stage_template_group_id = stg.id\nand stg.template_type = 3\nand stg.artifact_type in (4,5,6,7)\n) tmp on c.job_id = tmp.job_id and c.type in (4,5,6,7)\nset c.structure_id = tmp.structure_id\nwhere c.structure_id is null;\" is not DDL",- "code": 402- }+ want: []- statement: |-update t_ci_job_artifact_config cinner join (