Skip to content

Commit

Permalink
refactor(dbm-services): 模拟执行记录导入表结构过程 TencentBlueKing#6995
Browse files Browse the repository at this point in the history
ymakedaq committed Sep 20, 2024

Verified

This commit was signed with the committer’s verified signature.
al8n Al Liu
1 parent f9efdce commit ef02555
Showing 6 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion dbm-services/mysql/db-simulation/app/service/kubernets.go
Original file line number Diff line number Diff line change
@@ -483,7 +483,7 @@ func (k *DbPodSets) executeInPod(cmd, container string, extMap map[string]string
for sc.Scan() {
if !noLogger {
// 此方案打印的日志会在前端展示
xlogger.Info(sc.Text())
xlogger.Info("%s", sc.Text())
} else {
logger.Info(sc.Text())
}
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ func TestCreateClusterPod(t *testing.T) {
ps.TdbCtlImage = config.GAppConfig.Image.TdbCtlImg
ps.SpiderImage = config.GAppConfig.Image.SpiderImg
if err := ps.CreateClusterPod(); err != nil {
t.Fatalf(err.Error())
t.Fatalf("%s", err.Error())
return
}
t.Log("ending..")
43 changes: 35 additions & 8 deletions dbm-services/mysql/db-simulation/app/service/simulation_task.go
Original file line number Diff line number Diff line change
@@ -217,16 +217,14 @@ func (t *SimulationTask) SimulationRun(containerName string, xlogger *logger.Log
// 关闭协程
defer func() { ticker.Stop(); doneChan <- struct{}{} }()
model.UpdatePhase(t.TaskId, t.MySQLVersion, model.PhaseLoadSchema)
stdout, stderr, err := t.DbPodSets.executeInPod(t.getLoadSchemaSQLCmd(t.Path, t.SchemaSQLFile),
containerName,
t.getExtmap(t.SchemaSQLFile), true)
sstdout += stdout.String() + "\n"
sstderr += stderr.String() + "\n"
// Load schema SQL
sstdout, sstderr, err = t.loadSchemaSQL(containerName)
if err != nil {
logger.Error("load database schema sql failed %v", err)
return sstdout, sstderr, err
xlogger.Error("Failed to load schema SQL: %v", err)
return sstdout, sstderr, fmt.Errorf("failed to load schema SQL: %w", err)
}
xlogger.Info(stdout.String(), stderr.String())
xlogger.Info("Schema SQL loaded successfully")
xlogger.Info(sstdout, sstderr)
// load real databases
if err = t.getDbsExcludeSysDb(); err != nil {
logger.Error("getDbsExcludeSysDb faiked %v", err)
@@ -249,6 +247,35 @@ func (t *SimulationTask) SimulationRun(containerName string, xlogger *logger.Log
return sstdout, sstderr, nil
}

func (t *SimulationTask) loadSchemaSQL(containerName string) (sstdout, sstderr string,
err error) {
defer func() {
if err != nil {
errx := model.DB.Create(&model.TbSqlFileSimulationInfo{
TaskId: t.TaskId,
BillTaskId: t.Uid,
LineId: 0,
FileNameHash: fmt.Sprintf("%x", sha256.Sum256([]byte(t.SchemaSQLFile))),
FileName: t.SchemaSQLFile,
MySQLVersion: t.MySQLVersion,
Status: model.TaskFailed,
ErrMsg: "导入表结构失败," + err.Error(),
CreateTime: time.Now(),
UpdateTime: time.Now(),
}).Error
if errx != nil {
logger.Warn("create exeute schema sqlfile simulation record failed %v", errx)
}
}
}()
stdout, stderr, err := t.DbPodSets.executeInPod(t.getLoadSchemaSQLCmd(t.Path, t.SchemaSQLFile),
containerName,
t.getExtmap(t.SchemaSQLFile), true)
sstdout += stdout.String() + "\n"
sstderr += stderr.String() + "\n"
return sstdout, sstderr, err
}

func (t *SimulationTask) executeOneObject(e ExcuteSQLFileObj, containerName string, xlogger *logger.Logger) (sstdout,
sstderr string, err error) {
defer func() {
6 changes: 1 addition & 5 deletions dbm-services/mysql/db-simulation/app/syntax/syntax.go
Original file line number Diff line number Diff line change
@@ -111,10 +111,6 @@ type RiskInfo struct {
const DdlMapFileSubffix = ".tbl.map"

// Do 运行语法检查 For SQL 文件
//
// @receiver tf
// @return result
// @return err
func (tf *TmysqlParseFile) Do(dbtype string, versions []string) (result map[string]*CheckInfo, err error) {
logger.Info("doing....")
tf.result = make(map[string]*CheckInfo)
@@ -313,7 +309,7 @@ func (t *TmysqlParse) getCommand(filename, version string) (cmd string) {
outputFileName := getSQLParseResultFile(filename, version)
out = path.Join(t.tmpWorkdir, outputFileName)

cmd = fmt.Sprintf(`%s --sql-file=%s --output-path=%s --print-query-mode=2 --output-format='JSON_LINE_PER_OBJECT'`,
cmd = fmt.Sprintf(`%s --sql-file=%s --output-path=%s --print-query-mode=2 --output-format='JSON_LINE_PER_OBJECT' --sql-mode='' `,
t.TmysqlParseBinPath, in, out)

if lo.IsNotEmpty(version) {
2 changes: 1 addition & 1 deletion dbm-services/mysql/db-simulation/handler/handler.go
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ func QueryTask(c *gin.Context) {
switch task.Status {
case model.TaskFailed:
allSuccessful = false
SendResponse(c, fmt.Errorf(task.SysErrMsg), map[string]interface{}{
SendResponse(c, fmt.Errorf("%s", task.SysErrMsg), map[string]interface{}{
"simulation_version": task.MySQLVersion,
"stdout": task.Stdout,
"stderr": task.Stderr,
6 changes: 5 additions & 1 deletion dbm-services/mysql/db-simulation/handler/syntax_check.go
Original file line number Diff line number Diff line change
@@ -37,7 +37,11 @@ func init() {
}
workdir = strings.TrimSpace(viper.GetString("workdir"))
if workdir == "" {
workdir = "/tmp"
if cmutil.FileExists("/tmp") {
workdir = "/tmp"
return
}
workdir = "/"
}
}

0 comments on commit ef02555

Please sign in to comment.