Skip to content

feat(dbm-services): 库表分析识别子查询 #10151 #10197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dbm-services/mysql/db-simulation/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ func IsEmptyTdbctlPodResourceConfig() bool {
func loadConfig() (err error) {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("$HOME/conf")
viper.AddConfigPath("./conf")
viper.AddConfigPath("$HOME/conf")
if err = viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
logger.Error("config file not found,maybe read by env")
Expand Down
50 changes: 25 additions & 25 deletions dbm-services/mysql/db-simulation/app/syntax/parse_relation_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (tf *TmysqlParseFile) DoParseRelationDbs(version string) (createDbs, relati
}
logger.Info("all sqlfiles download ok ~")
alreadExecutedSqlfileChan := make(chan string, len(tf.Param.FileNames))

go func() {
if err = tf.Execute(alreadExecutedSqlfileChan, version); err != nil {
logger.Error("failed to execute tmysqlparse: %s", err.Error())
Expand Down Expand Up @@ -168,40 +167,41 @@ func (t *TmysqlParse) analyzeRelationDbs(inputfileName, mysqlVersion string) (
logger.Error("json unmasrshal line:%s failed %s", string(line), err.Error())
return nil, nil, nil, false, err
}
// 判断是否有语法错误
// ErrorCode !=0 就是语法错误
if res.ErrorCode != 0 {
return nil, nil, nil, false, fmt.Errorf("%s", res.ErrorMsg)
}
if lo.IsNotEmpty(res.Command) {
if res.Command == SQLTypeCreateTable {
var c CreateTableResult
if err = json.Unmarshal(line, &c); err != nil {
logger.Error("json unmasrshal line:%s failed %s", string(line), err.Error())
return nil, nil, nil, false, err
}
// 需要排除create table like
if c.IsCreateTableLike || c.IsCreateTableSelect {
return nil, nil, allCommandType, true, nil
}
} else {
allCommandType = append(allCommandType, res.Command)
}
}
if slices.Contains([]string{SQLTypeCreateProcedure, SQLTypeCreateFunction, SQLTypeCreateView, SQLTypeCreateTrigger,
SQLTypeInsertSelect, SQLTypeRelaceSelect},
res.Command) {
return nil, nil, allCommandType, true, nil
}
if lo.IsEmpty(res.DbName) {
if lo.IsEmpty(res.Command) {
continue
}
if lo.IsNotEmpty(res.DbName) {
relationDbs = append(relationDbs, res.DbName)
}
// create db not need dump db
if slices.Contains([]string{SQLTypeCreateDb}, res.Command) {
createDbs = append(createDbs, res.DbName)
continue
}
relationDbs = append(relationDbs, res.DbName)

if res.HasSubQuery {
return nil, relationDbs, allCommandType, true, nil
}
if slices.Contains([]string{SQLTypeCreateProcedure, SQLTypeCreateFunction, SQLTypeCreateView, SQLTypeCreateTrigger,
SQLTypeInsertSelect, SQLTypeRelaceSelect},
res.Command) {
return nil, relationDbs, allCommandType, true, nil
}
if res.Command == SQLTypeCreateTable {
var c CreateTableResult
if err = json.Unmarshal(line, &c); err != nil {
logger.Error("json unmasrshal line:%s failed %s", string(line), err.Error())
return nil, nil, nil, false, err
}
// 需要排除create table like
if c.IsCreateTableLike || c.IsCreateTableSelect {
return nil, relationDbs, allCommandType, true, nil
}
}
allCommandType = append(allCommandType, res.Command)
}
return createDbs, relationDbs, allCommandType, false, nil
}
Expand Down
3 changes: 2 additions & 1 deletion dbm-services/mysql/db-simulation/app/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ func (t *TmysqlParse) Init() (err error) {
return nil
}

// DelTempDir TODO
// DelTempDir del tempDir
func (t *TmysqlParse) DelTempDir() {
logger.Info("start to remove tempDir:%s", t.tmpWorkdir)
if err := os.RemoveAll(t.tmpWorkdir); err != nil {
logger.Warn("remove tempDir:" + t.tmpWorkdir + ".error info:" + err.Error())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ type ParseLineQueryBase struct {
ErrorMsg string `json:"error_msg,omitempty"`
MinMySQLVersion int `json:"min_mysql_version"`
MaxMySQLVersion int `json:"max_my_sql_version"`
HasSubQuery bool `json:"has_subquery,omitempty"`
}

// IsSysDb sql modify target db is sys db
Expand Down
10 changes: 5 additions & 5 deletions dbm-services/mysql/db-simulation/handler/syntax_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ func (s SyntaxHandler) ParseSQLFileRelationDb(r *gin.Context) {
FileNames: param.Files,
},
}
defer p.DelTempDir()
createDbs, dbs, allCommands, dumpall, err := p.DoParseRelationDbs("")
if err != nil {
s.SendResponse(r, err, nil)
Expand All @@ -270,7 +271,7 @@ func (s SyntaxHandler) ParseSQLFileRelationDb(r *gin.Context) {
defer p.DelTempDir()
// 如果所有的命令都是alter table, dump指定库表
logger.Debug("debug: %v,%d", allCommands, len(allCommands))
if isAllOperateTable(allCommands) {
if isAllOperateTable(allCommands) && !dumpall {
relationTbls, err := p.ParseSpecialTbls("")
if err != nil {
s.SendResponse(r, err, nil)
Expand Down Expand Up @@ -320,7 +321,7 @@ func isAllOperateTable(allCommands []string) bool {
return lo.Every([]string{
syntax.SQLTypeAlterTable, syntax.SQLTypeUseDb,
syntax.SQLTypeCreateIndex, syntax.SQLTypeDropTable,
syntax.SQLTypeInsert, syntax.SQLTypeDelete,
syntax.SQLTypeInsert, syntax.SQLTypeDelete, syntax.SQLTypeUpdate,
syntax.SQLTypeCreateTable, syntax.SQLTypeReplace,
}, allCommands)
}
Expand Down Expand Up @@ -358,15 +359,15 @@ func (s *SyntaxHandler) ParseSQLRelationDb(r *gin.Context) {
FileNames: []string{fileName},
},
}
// defer p.DelTempDir()
defer p.DelTempDir()
createDbs, dbs, allCommands, dumpall, err := p.DoParseRelationDbs("")
if err != nil {
s.SendResponse(r, err, nil)
return
}
// 如果所有的命令都是alter table, dump指定库表
logger.Info("all command types: %v,%d", allCommands, len(allCommands))
if isAllOperateTable(allCommands) {
if isAllOperateTable(allCommands) && !dumpall {
relationTbls, err := p.ParseSpecialTbls("")
if err != nil {
s.SendResponse(r, err, nil)
Expand All @@ -382,7 +383,6 @@ func (s *SyntaxHandler) ParseSQLRelationDb(r *gin.Context) {
})
return
}

s.SendResponse(r, nil, gin.H{
"create_dbs": createDbs,
"dbs": dbs,
Expand Down