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: add dummy advisor for mix ddl/dml (#11075)
Danny Xu059a7197e9ec
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex 2ecf350726..6a837cfcdb 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -424,6 +424,12 @@ const (// PostgreSQLStatementCheckSetRoleVariable is an advisor type for PostgreSQL to check set role variable.PostgreSQLStatementCheckSetRoleVariable Type = "bb.plugin.advisor.postgresql.statement.check-set-role-variable"+ // PostgreSQLStatementDisallowMixDML is an advisor type for PostgreSQL disallow mix DML on same table.+ PostgreSQLStatementDisallowMixDML Type = "bb.plugin.advisor.postgresql.statement.disallow-mix-dml"++ // PostgreSQLStatementDisallowMixDDLDML is an advisor type for PostgreSQL disallow mix DDL and DML.+ PostgreSQLStatementDisallowMixDDLDML Type = "bb.plugin.advisor.postgresql.statement.disallow-mix-ddl-dml"+// Oracle Advisor.// OracleSyntax is an advisor type for Oracle syntax.diff --git a/backend/plugin/advisor/generator/postgresql.template b/backend/plugin/advisor/generator/postgresql.templateindex 8ab81c41f3..7d46c2f655 100644--- a/backend/plugin/advisor/generator/postgresql.template+++ b/backend/plugin/advisor/generator/postgresql.template@@ -3,8 +3,9 @@ package pg// Framework code is generated by the generator.import (+ "github.com/pkg/errors"+"github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/db""github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"storepb "github.com/bytebase/bytebase/proto/generated-go/store")@@ -24,9 +25,9 @@ type %AdvisorName struct {// Check checks for %AdvisorCommentfunc (*%AdvisorName) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {- stmtList, errAdvice := parseStatement(statement)- if errAdvice != nil {- return errAdvice, nil+ stmtList, ok := ctx.AST.([]ast.Node)+ if !ok {+ return nil, errors.Errorf("failed to convert to Node")}level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)diff --git a/backend/plugin/advisor/pg/advisor_statement_disallow_mix_ddl_dml.go b/backend/plugin/advisor/pg/advisor_statement_disallow_mix_ddl_dml.gonew file mode 100644index 0000000000..76a2176d7b--- /dev/null+++ b/backend/plugin/advisor/pg/advisor_statement_disallow_mix_ddl_dml.go@@ -0,0 +1,71 @@+package pg++// Framework code is generated by the generator.++import (+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++var (+ _ advisor.Advisor = (*StatementDisallowMixDdlDmlAdvisor)(nil)+ _ ast.Visitor = (*statementDisallowMixDdlDmlChecker)(nil)+)++func init() {+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLStatementDisallowMixDDLDML, &StatementDisallowMixDdlDmlAdvisor{})+}++// StatementDisallowMixDdlDmlAdvisor is the advisor checking for disallow mix DDL and DML.+type StatementDisallowMixDdlDmlAdvisor struct {+}++// Check checks for disallow mix DDL and DML.+func (*StatementDisallowMixDdlDmlAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ // TODO(p0ny): implement it.+ stmtList, ok := ctx.AST.([]ast.Node)+ if !ok {+ return nil, errors.Errorf("failed to convert to Node")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &statementDisallowMixDdlDmlChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ for _, stmt := range stmtList {+ ast.Walk(checker, stmt)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type statementDisallowMixDdlDmlChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+}++// Visit implements ast.Visitor interface.+func (checker *statementDisallowMixDdlDmlChecker) Visit(_ ast.Node) ast.Visitor {+ // TODO: implement it+ // switch node := in.(type) {+ // }++ return checker+}diff --git a/backend/plugin/advisor/pg/advisor_statement_disallow_mix_dml.go b/backend/plugin/advisor/pg/advisor_statement_disallow_mix_dml.gonew file mode 100644index 0000000000..5c50ee4fe9--- /dev/null+++ b/backend/plugin/advisor/pg/advisor_statement_disallow_mix_dml.go@@ -0,0 +1,71 @@+package pg++// Framework code is generated by the generator.++import (+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++var (+ _ advisor.Advisor = (*StatementDisallowMixDmlAdvisor)(nil)+ _ ast.Visitor = (*statementDisallowMixDmlChecker)(nil)+)++func init() {+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLStatementDisallowMixDML, &StatementDisallowMixDmlAdvisor{})+}++// StatementDisallowMixDmlAdvisor is the advisor checking for disallow mix DML on same table.+type StatementDisallowMixDmlAdvisor struct {+}++// Check checks for disallow mix DML on same table.+func (*StatementDisallowMixDmlAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ // TODO(p0ny): implement it.+ stmtList, ok := ctx.AST.([]ast.Node)+ if !ok {+ return nil, errors.Errorf("failed to convert to Node")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &statementDisallowMixDmlChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ for _, stmt := range stmtList {+ ast.Walk(checker, stmt)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type statementDisallowMixDmlChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+}++// Visit implements ast.Visitor interface.+func (checker *statementDisallowMixDmlChecker) Visit(_ ast.Node) ast.Visitor {+ // TODO: implement it+ // switch node := in.(type) {+ // }++ return checker+}diff --git a/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_ddl_dml.go b/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_ddl_dml.gonew file mode 100644index 0000000000..a1c48dc967--- /dev/null+++ b/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_ddl_dml.go@@ -0,0 +1,73 @@+package tidb++import (+ "github.com/pingcap/tidb/pkg/parser/ast"+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++var (+ _ advisor.Advisor = (*StatementDisallowmixDDLDMLAdvisor)(nil)+ _ ast.Visitor = (*statementDisallowMixDDLDMLChecker)(nil)+)++func init() {+ advisor.Register(storepb.Engine_TIDB, advisor.MySQLStatementDisallowMixDDLDML, &StatementDisallowmixDDLDMLAdvisor{})+}++// StatementDisallowmixDDLDMLAdvisor is the advisor checking for no mixed DDL and DML.+type StatementDisallowmixDDLDMLAdvisor struct {+}++// Check checks for no mixed DDL and DML.+func (*StatementDisallowmixDDLDMLAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ // TODO(p0ny): implement it.+ root, ok := ctx.AST.([]ast.StmtNode)+ if !ok {+ return nil, errors.Errorf("failed to convert to StmtNode")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &statementDisallowMixDDLDMLChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }+ for _, stmtNode := range root {+ checker.text = stmtNode.Text()+ checker.line = stmtNode.OriginTextPosition()+ (stmtNode).Accept(checker)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type statementDisallowMixDDLDMLChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+}++// Enter implements the ast.Visitor interface.+func (*statementDisallowMixDDLDMLChecker) Enter(in ast.Node) (ast.Node, bool) {+ return in, false+}++// Leave implements the ast.Visitor interface.+func (*statementDisallowMixDDLDMLChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_dml.go b/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_dml.gonew file mode 100644index 0000000000..b83bc3d460--- /dev/null+++ b/backend/plugin/advisor/tidb/advisor_statement_disallow_mix_dml.go@@ -0,0 +1,73 @@+package tidb++import (+ "github.com/pingcap/tidb/pkg/parser/ast"+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++var (+ _ advisor.Advisor = (*StatementDisallowMixDMLAdvisor)(nil)+ _ ast.Visitor = (*statementDisallowMixDMLChecker)(nil)+)++func init() {+ advisor.Register(storepb.Engine_TIDB, advisor.MySQLStatementDisallowMixDML, &StatementDisallowMixDMLAdvisor{})+}++// StatementDisallowmixDDLDMLAdvisor is the advisor checking for no multiple DMLs for the same table.+type StatementDisallowMixDMLAdvisor struct {+}++// Check checks for no multiple DMLs for the same table.+func (*StatementDisallowMixDMLAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ // TODO(p0ny): implement it.+ root, ok := ctx.AST.([]ast.StmtNode)+ if !ok {+ return nil, errors.Errorf("failed to convert to StmtNode")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &statementDisallowMixDMLChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }+ for _, stmtNode := range root {+ checker.text = stmtNode.Text()+ checker.line = stmtNode.OriginTextPosition()+ (stmtNode).Accept(checker)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type statementDisallowMixDMLChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+}++// Enter implements the ast.Visitor interface.+func (*statementDisallowMixDMLChecker) Enter(in ast.Node) (ast.Node, bool) {+ return in, false+}++// Leave implements the ast.Visitor interface.+func (*statementDisallowMixDMLChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}