Skip to content

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

refactor: move mysql/tidb compatible (#8527)

Danny Xue719c76a0abf
diff --git a/backend/plugin/db/mysql/rollback.go b/backend/plugin/db/mysql/rollback.go
index 04809d40d8..47322274d7 100644
--- a/backend/plugin/db/mysql/rollback.go
+++ b/backend/plugin/db/mysql/rollback.go
@@ -17,7 +17,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pkg/errors"
- bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
"github.com/bytebase/bytebase/backend/resources/mysqlutil"
)
@@ -150,7 +150,7 @@ func (driver *Driver) GenerateRollbackSQL(ctx context.Context, binlogSizeLimit i
// GetTableColumns parses the schema to get the table columns map.
// This is used to generate rollback SQL from the binlog events.
func GetTableColumns(schema string) (map[string][]string, error) {
- _, supportStmts, err := bbparser.ExtractTiDBUnsupportedStmts(schema)
+ _, supportStmts, err := mysqlparser.ExtractTiDBUnsupportedStmts(schema)
if err != nil {
return nil, errors.Wrapf(err, "failed to extract TiDB unsupported statements from old statements %q", schema)
}
diff --git a/backend/plugin/parser/mysql/compatible.go b/backend/plugin/parser/mysql/compatible.go
new file mode 100644
index 0000000000..faa10b4e1b
--- /dev/null
+++ b/backend/plugin/parser/mysql/compatible.go
@@ -0,0 +1,70 @@
+package mysql
+
+import (
+ "bytes"
+ "fmt"
+ "regexp"
+
+ "github.com/pkg/errors"
+
+ tidbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb"
+)
+
+// ExtractTiDBUnsupportedStmts returns a list of unsupported statements in TiDB extracted from the `stmts`,
+// and returns the remaining statements supported by TiDB from `stmts`.
+func ExtractTiDBUnsupportedStmts(stmts string) ([]string, string, error) {
+ var unsupportStmts []string
+ var supportedStmts bytes.Buffer
+ // We use our bb tokenizer to help us split the multi-statements into statement list.
+ singleSQLs, err := SplitMySQL(stmts)
+ if err != nil {
+ return nil, "", errors.Wrapf(err, "cannot split multi sql %q via bytebase parser", stmts)
+ }
+ for _, sql := range singleSQLs {
+ content := sql.Text
+ if isTiDBUnsupportStmt(content) {
+ unsupportStmts = append(unsupportStmts, content)
+ continue
+ }
+ _, _ = supportedStmts.WriteString(content)
+ _, _ = supportedStmts.WriteString("\n")
+ }
+ return unsupportStmts, supportedStmts.String(), nil
+}
+
+// isTiDBUnsupportStmt returns true if this statement is unsupported in TiDB.
+func isTiDBUnsupportStmt(stmt string) bool {
+ if _, err := tidbparser.ParseTiDB(stmt, "", ""); err != nil {
+ return true
+ }
+ return false
+}
+
+// IsTiDBUnsupportDDLStmt checks whether the `stmt` is unsupported DDL statement in TiDB, the following statements are unsupported:
+// 1. `CREATE TRIGGER`
+// 2. `CREATE EVENT`
+// 3. `CREATE FUNCTION`
+// 4. `CREATE PROCEDURE`.
+func IsTiDBUnsupportDDLStmt(stmt string) bool {
+ objects := []string{
+ "TRIGGER",
+ "EVENT",
+ "FUNCTION",
+ "PROCEDURE",
+ }
+ createRegexFmt := "(?i)^\\s*CREATE\\s+(DEFINER=(`(.)+`|(.)+)@(`(.)+`|(.)+)(\\s)+)?%s\\s+"
+ dropRegexFmt := "(?i)^\\s*DROP\\s+%s\\s+"
+ for _, obj := range objects {
+ createRegexp := fmt.Sprintf(createRegexFmt, obj)
+ re := regexp.MustCompile(createRegexp)
+ if re.MatchString(stmt) {
+ return true
+ }
+ dropRegexp := fmt.Sprintf(dropRegexFmt, obj)
+ re = regexp.MustCompile(dropRegexp)
+ if re.MatchString(stmt) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/backend/plugin/parser/sql/utils_test.go b/backend/plugin/parser/mysql/compatible_test.go
similarity index 99%
rename from backend/plugin/parser/sql/utils_test.go
rename to backend/plugin/parser/mysql/compatible_test.go
index 04ea2ee279..28b897a3af 100644
--- a/backend/plugin/parser/sql/utils_test.go
+++ b/backend/plugin/parser/mysql/compatible_test.go
@@ -1,4 +1,4 @@
-package parser
+package mysql
import (
"testing"
diff --git a/backend/plugin/parser/sql/differ/mysql/differ.go b/backend/plugin/parser/sql/differ/mysql/differ.go
index e0994aae63..298549d631 100644
--- a/backend/plugin/parser/sql/differ/mysql/differ.go
+++ b/backend/plugin/parser/sql/differ/mysql/differ.go
@@ -594,7 +594,7 @@ func (*SchemaDiffer) SchemaDiff(oldStmt, newStmt string, ignoreCaseSensitive boo
}
func classifyStatement(statement string) ([]string, string, error) {
- unsupported, supported, err := bbparser.ExtractTiDBUnsupportedStmts(statement)
+ unsupported, supported, err := mysqlparser.ExtractTiDBUnsupportedStmts(statement)
if err != nil {
return nil, "", errors.Wrapf(err, "failed to extract TiDB unsupported statements from statements %q", statement)
}
diff --git a/backend/plugin/parser/sql/transform/mysql/transform.go b/backend/plugin/parser/sql/transform/mysql/transform.go
index d5afbc725f..001ee4e4c0 100644
--- a/backend/plugin/parser/sql/transform/mysql/transform.go
+++ b/backend/plugin/parser/sql/transform/mysql/transform.go
@@ -114,7 +114,7 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
remainingStatement = append(remainingStatement, stmt.Text)
continue
}
- if bbparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
+ if mysqlparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
remainingStatement = append(remainingStatement, stmt.Text)
continue
}
@@ -147,7 +147,7 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
}
for _, stmt := range standardList {
- if bbparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
+ if mysqlparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
// TODO(rebelice): consider the unsupported DDL.
continue
}
@@ -184,7 +184,7 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
// 3. missing indexes for existed table are below of this table and as the origin order.
// 4. missing tables are below of existed tables and as the origin order.
for _, stmt := range standardList {
- if bbparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
+ if mysqlparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
// TODO(rebelice): consider the unsupported DDL.
continue
}
@@ -366,7 +366,7 @@ func (*SchemaTransformer) Check(schema string) (int, error) {
// So we need to skip the statement if the delimiter is not `;`.
continue
}
- if bbparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
+ if mysqlparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
continue
}
nodeList, _, err := parser.New().Parse(stmt.Text, "", "")
@@ -460,7 +460,7 @@ func (*SchemaTransformer) Transform(schema string) (string, error) {
result = append(result, stmt.Text+"\n\n")
continue
}
- if bbparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
+ if mysqlparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
result = append(result, stmt.Text+"\n\n")
continue
}
diff --git a/backend/plugin/parser/sql/utils.go b/backend/plugin/parser/sql/utils.go
index 85ac4716a8..8819bf563e 100644
--- a/backend/plugin/parser/sql/utils.go
+++ b/backend/plugin/parser/sql/utils.go
@@ -1,10 +1,7 @@
package parser
import (
- "bytes"
- "fmt"
"io"
- "regexp"
"github.com/pkg/errors"
@@ -95,65 +92,6 @@ func SplitMultiSQLStream(engineType EngineType, src io.Reader, f func(string) er
}
}
-// ExtractTiDBUnsupportedStmts returns a list of unsupported statements in TiDB extracted from the `stmts`,
-// and returns the remaining statements supported by TiDB from `stmts`.
-func ExtractTiDBUnsupportedStmts(stmts string) ([]string, string, error) {
- var unsupportStmts []string
- var supportedStmts bytes.Buffer
- // We use our bb tokenizer to help us split the multi-statements into statement list.
- singleSQLs, err := SplitMultiSQL(MySQL, stmts)
- if err != nil {
- return nil, "", errors.Wrapf(err, "cannot split multi sql %q via bytebase parser", stmts)
- }
- for _, sql := range singleSQLs {
- content := sql.Text
- if isTiDBUnsupportStmt(content) {
- unsupportStmts = append(unsupportStmts, content)
- continue
- }
- _, _ = supportedStmts.WriteString(content)
- _, _ = supportedStmts.WriteString("\n")
- }
- return unsupportStmts, supportedStmts.String(), nil
-}
-
-// isTiDBUnsupportStmt returns true if this statement is unsupported in TiDB.
-func isTiDBUnsupportStmt(stmt string) bool {
- if _, err := tidbparser.ParseTiDB(stmt, "", ""); err != nil {
- return true
- }
- return false
-}
-
-// IsTiDBUnsupportDDLStmt checks whether the `stmt` is unsupported DDL statement in TiDB, the following statements are unsupported:
-// 1. `CREATE TRIGGER`
-// 2. `CREATE EVENT`
-// 3. `CREATE FUNCTION`
-// 4. `CREATE PROCEDURE`.
-func IsTiDBUnsupportDDLStmt(stmt string) bool {
- objects := []string{
- "TRIGGER",
- "EVENT",
- "FUNCTION",
- "PROCEDURE",
- }
- createRegexFmt := "(?i)^\\s*CREATE\\s+(DEFINER=(`(.)+`|(.)+)@(`(.)+`|(.)+)(\\s)+)?%s\\s+"
- dropRegexFmt := "(?i)^\\s*DROP\\s+%s\\s+"
- for _, obj := range objects {
- createRegexp := fmt.Sprintf(createRegexFmt, obj)
- re := regexp.MustCompile(createRegexp)
- if re.MatchString(stmt) {
- return true
- }
- dropRegexp := fmt.Sprintf(dropRegexFmt, obj)
- re = regexp.MustCompile(dropRegexp)
- if re.MatchString(stmt) {
- return true
- }
- }
- return false
-}
-
// ExtractDatabaseList extracts all databases from statement.
func ExtractDatabaseList(engineType EngineType, statement string, fallbackNormalizedDatabaseName string) ([]string, error) {
switch engineType {
diff --git a/backend/runner/plancheck/statement_report_executor.go b/backend/runner/plancheck/statement_report_executor.go
index 0ccc46b028..5c110b401c 100644
--- a/backend/runner/plancheck/statement_report_executor.go
+++ b/backend/runner/plancheck/statement_report_executor.go
@@ -20,6 +20,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
"github.com/bytebase/bytebase/backend/store"
@@ -421,7 +422,7 @@ func reportForMySQL(ctx context.Context, sqlDB *sql.DB, dbType db.Type, database
if stmt.Empty {
continue
}
- if parser.IsTiDBUnsupportDDLStmt(stmt.Text) {
+ if mysqlparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
continue
}
root, _, err := p.Parse(stmt.Text, charset, collation)
diff --git a/backend/runner/plancheck/statement_type_executor.go b/backend/runner/plancheck/statement_type_executor.go
index 2809b2ef79..b9d9b2c34a 100644
--- a/backend/runner/plancheck/statement_type_executor.go
+++ b/backend/runner/plancheck/statement_type_executor.go
@@ -13,6 +13,7 @@ import (
api "github.com/bytebase/bytebase/backend/legacyapi"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/db"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
"github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
runnerutils "github.com/bytebase/bytebase/backend/runner/utils"
@@ -347,7 +348,7 @@ func (e *StatementTypeExecutor) mysqlSDLTypeCheck(ctx context.Context, newSchema
var results []*storepb.PlanCheckRunResult_Result
for _, stmt := range list {
- if parser.IsTiDBUnsupportDDLStmt(stmt.Text) {
+ if mysqlparser.IsTiDBUnsupportDDLStmt(stmt.Text) {
continue
}
nodeList, _, err := tidbparser.New().Parse(stmt.Text, "", "")
@@ -482,7 +483,7 @@ func mysqlCreateAndDropDatabaseCheck(nodeList []tidbast.StmtNode) []*storepb.Pla
func mysqlStatementTypeCheck(statement string, charset string, collation string, changeType storepb.PlanCheckRunConfig_ChangeDatabaseType) ([]*storepb.PlanCheckRunResult_Result, error) {
// Due to the limitation of TiDB parser, we should split the multi-statement into single statements, and extract
// the TiDB unsupported statements, otherwise, the parser will panic or return the error.
- unsupportStmt, supportStmt, err := parser.ExtractTiDBUnsupportedStmts(statement)
+ unsupportStmt, supportStmt, err := mysqlparser.ExtractTiDBUnsupportedStmts(statement)
if err != nil {
// nolint:nilerr
return []*storepb.PlanCheckRunResult_Result{