Skip to content

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

feat(catalog): disallow CREATE TABLE AS in walk through (#2952)

rebelice3caf76862e63
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.go
index c035171c30..9a972fbb07 100644
--- a/plugin/advisor/catalog/walk_through.go
+++ b/plugin/advisor/catalog/walk_through.go
@@ -48,6 +48,8 @@ const (
ErrorTypeTableExists = 301
// ErrorTypeTableNotExists is the error that table not exists.
ErrorTypeTableNotExists = 302
+ // ErrorTypeUseCreateTableAs is the error that using CREATE TABLE AS statements.
+ ErrorTypeUseCreateTableAs = 303
// 401 ~ 499 column error type.
@@ -971,6 +973,13 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
}
}
+ if node.Select != nil {
+ return &WalkThroughError{
+ Type: ErrorTypeUseCreateTableAs,
+ Content: fmt.Sprintf("Disallow the CREATE TABLE AS statement but \"%s\" uses", node.Text()),
+ }
+ }
+
if node.ReferTable != nil {
return d.copyTable(node)
}
diff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.go
index ad41430a7e..2dacb55972 100644
--- a/plugin/advisor/catalog/walk_through_test.go
+++ b/plugin/advisor/catalog/walk_through_test.go
@@ -29,6 +29,21 @@ func TestWalkThrough(t *testing.T) {
DbType: db.MySQL,
},
statement: `
+ CREATE TABLE t as select * from t1;
+ `,
+ err: &WalkThroughError{
+ Type: ErrorTypeUseCreateTableAs,
+ Content: "Disallow the CREATE TABLE AS statement but \"CREATE TABLE t as select * from t1;\" uses",
+ Line: 2,
+ },
+ },
+
+ {
+ origin: &Database{
+ Name: "test",
+ DbType: db.MySQL,
+ },
+ statement: `
CREATE TABLE t(a int NOT NULL);
INSERT INTO t(a) values (1), (NULL);
`,
diff --git a/plugin/advisor/mysql/advisor_table_disallow_create_table_as.go b/plugin/advisor/mysql/advisor_table_disallow_create_table_as.go
deleted file mode 100644
index 5787535200..0000000000
--- a/plugin/advisor/mysql/advisor_table_disallow_create_table_as.go
+++ /dev/null
@@ -1,91 +0,0 @@
-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.go
deleted file mode 100644
index f271249b32..0000000000
--- a/plugin/advisor/mysql/advisor_table_disallow_create_table_as_test.go
+++ /dev/null
@@ -1,70 +0,0 @@
-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 tech_book
- )
- `,
- 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: 4,
- },
- },
- },
- {
- 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 tech_book`,
- 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)
-}