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): disallow create table ... as ... statement (#2477)
h3n4l9c80ba88a5fd
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 700fec9fa6..22329da989 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -118,6 +118,9 @@ const (// MySQLTableExists is an advisor type for MySQL table existence.MySQLTableExists Type = "bb.plugin.advisor.mysql.table.exists"+ // MySQLTableDisallowCreateTableAs is an advisor type for MySQL disallow CREATE TABLE ... AS ... statement.+ MySQLTableDisallowCreateTableAs Type = "bb.plugin.advisor.mysql.table.disallow-create-table-as"+// MySQLTableCommentRequirement is an advisor for MySQL table comment requirement.MySQLTableCommentRequirement Type = "bb.plugin.advisor.mysql.table.comment-require"diff --git a/plugin/advisor/code.go b/plugin/advisor/code.goindex 33d73ac9ee..1eb8b9835a 100644--- a/plugin/advisor/code.go+++ b/plugin/advisor/code.go@@ -27,6 +27,7 @@ const (StatementNoWhere Code = 202StatementSelectAll Code = 203StatementLeadingWildcardLike Code = 204+ StatementCreateTableAs Code = 205// 301 ~ 399 naming error code// 301 table naming advisor error code.diff --git a/plugin/advisor/mysql/advisor_table_disallow_create_table_as.go b/plugin/advisor/mysql/advisor_table_disallow_create_table_as.gonew file mode 100644index 0000000000..5787535200--- /dev/null+++ b/plugin/advisor/mysql/advisor_table_disallow_create_table_as.go@@ -0,0 +1,91 @@+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/db"+)++var (+ _ advisor.Advisor = (*TableDisallowCreateTableAsAdvisor)(nil)+ _ ast.Visitor = (*tableDisallowCreateTableAsChecker)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLTableDisallowCreateTableAs, &TableDisallowCreateTableAsAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLTableDisallowCreateTableAs, &TableDisallowCreateTableAsAdvisor{})+}++// TableDisallowCreateTableAsAdvisor is the advisor checking for disallow CREATE TABLE ... AS ... statement.+type TableDisallowCreateTableAsAdvisor struct {+}++// Check checks for disallow CREATE TABLE ... AS ... statement.+func (*TableDisallowCreateTableAsAdvisor) 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 := &tableDisallowCreateTableAsChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ 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 tableDisallowCreateTableAsChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+}++// Enter implements the ast.Visitor interface.+func (v *tableDisallowCreateTableAsChecker) Enter(in ast.Node) (ast.Node, bool) {+ switch node := in.(type) {+ case *ast.CreateTableStmt:+ if node.Select != nil {+ v.adviceList = append(v.adviceList, advisor.Advice{+ Status: v.level,+ Code: advisor.StatementCreateTableAs,+ Title: v.title,+ Content: fmt.Sprintf("cannot create table `%s` by using CREATE TABLE ... [AS] SELECT ...", node.Table.Name.String()),+ Line: node.OriginTextPosition(),+ })+ }+ default:+ }++ return in, false+}++// Leave implements the ast.Visitor interface.+func (*tableDisallowCreateTableAsChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go b/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.gonew file mode 100644index 0000000000..0ed76d6e05--- /dev/null+++ b/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go@@ -0,0 +1,70 @@+package mysql++// Framework code is generated by the generator.++import (+ "testing"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestTableDisallowCreateTableAs(t *testing.T) {+ tests := []advisor.TestCase{+ {+ Statement: `CREATE TABLE t AS+ (+ SELECT * FROM tbl+ )+ `,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.StatementCreateTableAs,+ Title: "statement.create-table.no-create-table-as",+ Content: "cannot create table `t` by using CREATE TABLE ... [AS] SELECT ...",+ Line: 1,+ },+ },+ },+ {+ Statement: `CREATE TABLE t SELECT * FROM tbl`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.StatementCreateTableAs,+ Title: "statement.create-table.no-create-table-as",+ Content: "cannot create table `t` by using CREATE TABLE ... [AS] SELECT ...",+ Line: 1,+ },+ },+ },+ {+ Statement: `CREATE TABLE t LIKE tbl`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ Statement: `CREATE TABLE t (id INT PRIMARY KEY)`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ }++ advisor.RunSQLReviewRuleTests(t, tests, &TableDisallowCreateTableAsAdvisor{}, &advisor.SQLReviewRule{+ Type: advisor.SchemaRuleStatementNoCreateTableAs,+ Level: advisor.SchemaRuleLevelWarning,+ Payload: "",+ }, advisor.MockMySQLDatabase)+}diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex c3eb2ffd56..4b8e55d753 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -54,6 +54,8 @@ const (SchemaRuleStatementRequireWhere SQLReviewRuleType = "statement.where.require"// SchemaRuleStatementNoLeadingWildcardLike disallow leading '%' in LIKE, e.g. LIKE foo = '%x' is not allowed.SchemaRuleStatementNoLeadingWildcardLike SQLReviewRuleType = "statement.where.no-leading-wildcard-like"+ // SchemaRuleStatementNoCreateTableAs disallow 'CREATE TABLE ... [AS] SELECT.+ SchemaRuleStatementNoCreateTableAs SQLReviewRuleType = "statement.create-table.no-create-table-as"// SchemaRuleTableRequirePK require the table to have a primary key.SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"@@ -491,6 +493,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB:return MySQLIndexNoDuplicateColumn, nil}+ case SchemaRuleStatementNoCreateTableAs:+ switch engine {+ case db.MySQL, db.TiDB:+ return MySQLTableDisallowCreateTableAs, nil+ }}return Fake, errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)}