diff --git a/dbm-services/mysql/db-simulation/app/service/kubernets.go b/dbm-services/mysql/db-simulation/app/service/kubernets.go index 5f46a54527..ad62ec1d5e 100644 --- a/dbm-services/mysql/db-simulation/app/service/kubernets.go +++ b/dbm-services/mysql/db-simulation/app/service/kubernets.go @@ -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()) } diff --git a/dbm-services/mysql/db-simulation/app/service/kubernets_test.go b/dbm-services/mysql/db-simulation/app/service/kubernets_test.go index 241ed5ef4b..3df549628e 100644 --- a/dbm-services/mysql/db-simulation/app/service/kubernets_test.go +++ b/dbm-services/mysql/db-simulation/app/service/kubernets_test.go @@ -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..") diff --git a/dbm-services/mysql/db-simulation/app/service/simulation_task.go b/dbm-services/mysql/db-simulation/app/service/simulation_task.go index 0e143d69a3..b68cf6e3a4 100644 --- a/dbm-services/mysql/db-simulation/app/service/simulation_task.go +++ b/dbm-services/mysql/db-simulation/app/service/simulation_task.go @@ -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() { diff --git a/dbm-services/mysql/db-simulation/app/syntax/syntax.go b/dbm-services/mysql/db-simulation/app/syntax/syntax.go index 66f9751b1f..2f9c76645c 100644 --- a/dbm-services/mysql/db-simulation/app/syntax/syntax.go +++ b/dbm-services/mysql/db-simulation/app/syntax/syntax.go @@ -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) { diff --git a/dbm-services/mysql/db-simulation/handler/handler.go b/dbm-services/mysql/db-simulation/handler/handler.go index d451abf7ff..813102608a 100644 --- a/dbm-services/mysql/db-simulation/handler/handler.go +++ b/dbm-services/mysql/db-simulation/handler/handler.go @@ -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, diff --git a/dbm-services/mysql/db-simulation/handler/syntax_check.go b/dbm-services/mysql/db-simulation/handler/syntax_check.go index d2198f4b97..2060aadb46 100644 --- a/dbm-services/mysql/db-simulation/handler/syntax_check.go +++ b/dbm-services/mysql/db-simulation/handler/syntax_check.go @@ -37,7 +37,11 @@ func init() { } workdir = strings.TrimSpace(viper.GetString("workdir")) if workdir == "" { - workdir = "/tmp" + if cmutil.FileExists("/tmp") { + workdir = "/tmp" + return + } + workdir = "/" } }