diff --git a/backend/src/main/java/io/metersphere/api/dto/EnvironmentDTO.java b/backend/src/main/java/io/metersphere/api/dto/EnvironmentDTO.java new file mode 100644 index 0000000000..8bff389e5d --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/EnvironmentDTO.java @@ -0,0 +1,10 @@ +package io.metersphere.api.dto; + +import io.metersphere.api.dto.scenario.DatabaseConfig; +import lombok.Data; + +@Data +public class EnvironmentDTO { + private String environmentId; + private DatabaseConfig databaseConfig; +} diff --git a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java index afd82bd0bb..cb8aeb52fa 100644 --- a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java +++ b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java @@ -1,6 +1,8 @@ package io.metersphere.api.service; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import io.metersphere.api.dto.EnvironmentDTO; import io.metersphere.api.dto.SaveHistoricalDataUpgrade; import io.metersphere.api.dto.automation.ScenarioStatus; import io.metersphere.api.dto.definition.request.MsScenario; @@ -17,6 +19,7 @@ import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler; import io.metersphere.api.dto.definition.request.timer.MsConstantTimer; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.Scenario; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.request.*; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiScenarioMapper; @@ -48,6 +51,9 @@ public class HistoricalDataUpgradeService { private ExtApiScenarioMapper extApiScenarioMapper; @Resource SqlSessionFactory sqlSessionFactory; + @Resource + ApiTestEnvironmentService apiTestEnvironmentService; + private Map environmentDTOMap; private int getNextNum(String projectId) { ApiScenario apiScenario = extApiScenarioMapper.getNextNum(projectId); @@ -122,8 +128,14 @@ public class HistoricalDataUpgradeService { element.setType("DubboSampler"); } if (request instanceof SqlRequest) { - String requestJson = JSON.toJSONString(request); - element = JSON.parseObject(requestJson, MsJDBCSampler.class); + element = new MsJDBCSampler(); + SqlRequest request1 = (SqlRequest) request; + BeanUtils.copyBean(element, request1); + EnvironmentDTO dto = environmentDTOMap.get(request1.getDataSource()); + if (dto != null) { + ((MsJDBCSampler) element).setEnvironmentId(dto.getEnvironmentId()); + ((MsJDBCSampler) element).setDataSource(dto.getDatabaseConfig()); + } element.setType("JDBCSampler"); } if (request instanceof TCPRequest) { @@ -315,6 +327,9 @@ public class HistoricalDataUpgradeService { } public String upgrade(SaveHistoricalDataUpgrade saveHistoricalDataUpgrade) { + // 初始化环境,获取数据源 + getDataSource(saveHistoricalDataUpgrade.getProjectId()); + ApiTestExample example = new ApiTestExample(); example.createCriteria().andIdIn(saveHistoricalDataUpgrade.getTestIds()); List blobs = apiTestMapper.selectByExampleWithBLOBs(example); @@ -337,4 +352,23 @@ public class HistoricalDataUpgradeService { sqlSession.flushStatements(); return null; } + + private void getDataSource(String projectId) { + List environments = apiTestEnvironmentService.list(projectId); + environmentDTOMap = new HashMap<>(); + if (CollectionUtils.isNotEmpty(environments)) { + environments.forEach(environment -> { + EnvironmentConfig envConfig = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); + if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { + envConfig.getDatabaseConfigs().forEach(item -> { + EnvironmentDTO dto = new EnvironmentDTO(); + dto.setDatabaseConfig(item); + dto.setEnvironmentId(environment.getId()); + environmentDTOMap.put(item.getId(), dto); + }); + } + }); + } + } + }