Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(sql-review): implement MySQL table-not-exists rule (#2515)
rebelice6fea4fc84424
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 24c35096fa..8f60db4161 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -130,6 +130,9 @@ const (// MySQLTableDisallowCreateTableAs is an advisor type for MySQL disallow CREATE TABLE ... AS ... statement.MySQLTableDisallowCreateTableAs Type = "bb.plugin.advisor.mysql.table.disallow-create-table-as"+ // MySQLTableNotExists is an advisor for MySQL table name conflict check.+ MySQLTableNotExists Type = "bb.plugin.advisor.mysql.table.not-exists"+// MySQLDatabaseAllowDropIfEmpty is an advisor type for MySQL only allow drop empty database.MySQLDatabaseAllowDropIfEmpty Type = "bb.plugin.advisor.mysql.database.drop-empty-database"diff --git a/plugin/advisor/code.go b/plugin/advisor/code.goindex 4c8b1aa53e..084a5af0d5 100644--- a/plugin/advisor/code.go+++ b/plugin/advisor/code.go@@ -66,6 +66,7 @@ const (TableNotExists Code = 604NoTableComment Code = 605TableCommentTooLong Code = 606+ TableExists Code = 607// 701 ~ 799 database advisor error code.DatabaseNotEmpty Code = 701diff --git a/plugin/advisor/mysql/advisor_table_not_exists.go b/plugin/advisor/mysql/advisor_table_not_exists.gonew file mode 100644index 0000000000..e642c95187--- /dev/null+++ b/plugin/advisor/mysql/advisor_table_not_exists.go@@ -0,0 +1,96 @@+package mysql++// Framework code is generated by the generator.++import (+ "fmt"++ "github.com/pingcap/tidb/parser/ast"++ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/advisor/catalog"+ "github.com/bytebase/bytebase/plugin/advisor/db"+)++var (+ _ advisor.Advisor = (*TableNotExistsAdvisor)(nil)+ _ ast.Visitor = (*tableNotExistsChecker)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLTableNotExists, &TableNotExistsAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLTableNotExists, &TableNotExistsAdvisor{})+}++// TableNotExistsAdvisor is the advisor checking for table name conflict check.+type TableNotExistsAdvisor struct {+}++// Check checks for table name conflict check.+func (*TableNotExistsAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ stmtList, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &tableNotExistsChecker{+ level: level,+ title: string(ctx.Rule.Type),+ database: ctx.Database,+ }++ for _, stmt := range stmtList {+ checker.text = stmt.Text()+ checker.line = stmt.OriginTextPosition()+ (stmt).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 tableNotExistsChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+ database *catalog.Database+}++// Enter implements the ast.Visitor interface.+func (checker *tableNotExistsChecker) Enter(in ast.Node) (ast.Node, bool) {+ if node, ok := in.(*ast.CreateTableStmt); ok {+ if !node.IfNotExists && checker.tableExists(node.Table.Name.O) {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.TableExists,+ Title: checker.title,+ Content: fmt.Sprintf("Table `%s` exists", node.Table.Name.O),+ Line: node.OriginTextPosition(),+ })+ }+ }++ return in, false+}++// Leave implements the ast.Visitor interface.+func (*tableNotExistsChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}++func (checker *tableNotExistsChecker) tableExists(table string) bool {+ return checker.database.FindTable(&catalog.TableFind{TableName: table}) != nil+}diff --git a/plugin/advisor/mysql/advisor_table_not_exists_test.go b/plugin/advisor/mysql/advisor_table_not_exists_test.gonew file mode 100644index 0000000000..1127777e7e--- /dev/null+++ b/plugin/advisor/mysql/advisor_table_not_exists_test.go@@ -0,0 +1,54 @@+package mysql++// Framework code is generated by the generator.++import (+ "testing"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestTableNotExists(t *testing.T) {+ tests := []advisor.TestCase{+ {+ Statement: `CREATE TABLE t(a int)`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ Statement: `CREATE TABLE tech_book(a int)`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.TableExists,+ Title: "table.not-exists",+ Content: "Table `tech_book` exists",+ Line: 1,+ },+ },+ },+ {+ Statement: `CREATE TABLE IF NOT EXISTS tech_book(a int)`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ }++ advisor.RunSQLReviewRuleTests(t, tests, &TableNotExistsAdvisor{}, &advisor.SQLReviewRule{+ Type: advisor.SchemaRuleTableNotExists,+ Level: advisor.SchemaRuleLevelWarning,+ Payload: "",+ }, advisor.MockMySQLDatabase)+}diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 2c6b9fbd4c..50b524d761 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -67,6 +67,8 @@ const (SchemaRuleTableExists SQLReviewRuleType = "table.exists"// SchemaRuleTableCommentConvention enforce the table comment convention.SchemaRuleTableCommentConvention SQLReviewRuleType = "table.comment"+ // SchemaRuleTableNotExists check the table name conflict.+ SchemaRuleTableNotExists SQLReviewRuleType = "table.not-exists"// SchemaRuleRequiredColumn enforce the required columns in each table.SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"