fix(接口自动化): 修复数据迁移缺陷

This commit is contained in:
fit2-zhao 2021-01-06 18:55:25 +08:00
parent 4dfaa1c463
commit 540aba788c
2 changed files with 46 additions and 2 deletions

View File

@ -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;
}

View File

@ -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<String, EnvironmentDTO> 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<ApiTest> blobs = apiTestMapper.selectByExampleWithBLOBs(example);
@ -337,4 +352,23 @@ public class HistoricalDataUpgradeService {
sqlSession.flushStatements();
return null;
}
private void getDataSource(String projectId) {
List<ApiTestEnvironmentWithBLOBs> 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);
});
}
});
}
}
}