diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index f1376fa0f8..52692d3235 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -11,6 +11,7 @@ import io.metersphere.api.dto.EnvironmentType; import io.metersphere.api.dto.definition.request.controller.MsLoopController; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; +import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.request.BodyFile; import io.metersphere.api.service.ApiTestEnvironmentService; @@ -851,4 +852,28 @@ public class ElementUtil { } } + public static DatabaseConfig dataSource(String projectId, String dataSourceId, EnvironmentConfig envConfig) { + try { + ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); + List environment = environmentService.list(projectId); + EnvironmentConfig dataConfig = null; + List dataName = new ArrayList<>(); + List orgDataSource = environment.stream().filter(ApiTestEnvironmentWithBLOBs -> ApiTestEnvironmentWithBLOBs.getConfig().contains(dataSourceId)).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(orgDataSource)) { + dataConfig = JSONObject.parseObject(orgDataSource.get(0).getConfig(), EnvironmentConfig.class); + if (CollectionUtils.isNotEmpty(dataConfig.getDatabaseConfigs())) { + dataName = dataConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getId().equals(dataSourceId)).map(DatabaseConfig::getName).collect(Collectors.toList()); + } + } + List finalDataName = dataName; + List collect = envConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getName().equals(finalDataName.get(0))).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(collect)) { + return collect.get(0); + } + } catch (Exception e) { + LogUtil.error(e); + } + return null; + } + } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java index d865c12961..12942f4749 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java @@ -126,7 +126,12 @@ public class MsJDBCPostProcessor extends MsTestElement { } else { // 取当前环境下默认的一个数据源 if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { - this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); + DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId())); + if (dataSourceOrg != null) { + this.dataSource = dataSourceOrg; + } else { + this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); + } } } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java index 942b272bbb..ff074baad6 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java @@ -126,7 +126,12 @@ public class MsJDBCPreProcessor extends MsTestElement { } else { // 取当前环境下默认的一个数据源 if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { - this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); + DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId())); + if (dataSourceOrg != null) { + this.dataSource = dataSourceOrg; + } else { + this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); + } } } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index a0ca9831b1..8c90c640c9 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -147,7 +147,12 @@ public class MsJDBCSampler extends MsTestElement { if (config.getConfig().get(this.getProjectId()) != null) { envConfig = config.getConfig().get(this.getProjectId()); if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { - this.dataSource = envConfig.getDatabaseConfigs().get(0); + DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, envConfig); + if (dataSourceOrg != null) { + this.dataSource = dataSourceOrg; + } else { + this.dataSource = envConfig.getDatabaseConfigs().get(0); + } } } } diff --git a/frontend/src/business/components/api/definition/components/environment/ApiEnvironmentConfig.vue b/frontend/src/business/components/api/definition/components/environment/ApiEnvironmentConfig.vue index 7fa04ac852..b7c9f99058 100644 --- a/frontend/src/business/components/api/definition/components/environment/ApiEnvironmentConfig.vue +++ b/frontend/src/business/components/api/definition/components/environment/ApiEnvironmentConfig.vue @@ -35,7 +35,7 @@ import MsAsideContainer from "../../../../common/components/MsAsideContainer"; import MsMainContainer from "../../../../common/components/MsMainContainer"; import MsAsideItem from "../../../../common/components/MsAsideItem"; import EnvironmentEdit from "./EnvironmentEdit"; -import {hasPermission, listenGoBack, removeGoBackListener} from "@/common/js/utils"; +import {getUUID, hasPermission, listenGoBack, removeGoBackListener} from "@/common/js/utils"; import {Environment, parseEnvironment} from "../../model/EnvironmentModel"; export default { @@ -113,6 +113,11 @@ export default { let newEnvironment = {}; newEnvironment = new Environment(environment); newEnvironment.id = null; + newEnvironment.config.databaseConfigs.forEach(dataSource => { + if (dataSource.id) { + dataSource.id = getUUID(); + } + }) newEnvironment.name = this.getNoRepeatName(newEnvironment.name); if (!this.validateEnvironment(newEnvironment)) { return; diff --git a/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue b/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue index 43c772ffdf..5c57a4e527 100644 --- a/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue +++ b/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue @@ -37,7 +37,7 @@ import MsAsideContainer from "../../../common/components/MsAsideContainer"; import MsMainContainer from "../../../common/components/MsMainContainer"; import MsAsideItem from "../../../common/components/MsAsideItem"; import EnvironmentEdit from "./environment/EnvironmentEdit"; -import {hasPermission, listenGoBack, removeGoBackListener} from "../../../../../common/js/utils"; +import {getUUID, hasPermission, listenGoBack, removeGoBackListener} from "../../../../../common/js/utils"; import {Environment, parseEnvironment} from "../model/EnvironmentModel"; import MsDialogHeader from "@/business/components/common/components/MsDialogHeader"; @@ -145,6 +145,11 @@ export default { if (!this.validateEnvironment(newEnvironment)) { return; } + newEnvironment.config.databaseConfigs.forEach(dataSource => { + if (dataSource.id) { + dataSource.id = getUUID(); + } + }) this.$refs.environmentEdit._save(newEnvironment); this.environments.unshift(newEnvironment); this.$refs.environmentItems.itemSelected(this.environments.length - 1, newEnvironment); diff --git a/frontend/src/business/components/project/menu/EnvironmentList.vue b/frontend/src/business/components/project/menu/EnvironmentList.vue index a05eec1f59..7954f179e5 100644 --- a/frontend/src/business/components/project/menu/EnvironmentList.vue +++ b/frontend/src/business/components/project/menu/EnvironmentList.vue @@ -134,7 +134,7 @@ import EnvironmentEdit from "@/business/components/api/test/components/environme import MsAsideItem from "@/business/components/common/components/MsAsideItem"; import MsAsideContainer from "@/business/components/common/components/MsAsideContainer"; import ProjectSwitch from "@/business/components/common/head/ProjectSwitch"; -import {downloadFile, getCurrentProjectID, operationConfirm} from "@/common/js/utils"; +import {downloadFile, getCurrentProjectID, getUUID, operationConfirm} from "@/common/js/utils"; import EnvironmentImport from "@/business/components/project/menu/EnvironmentImport"; import MsMainContainer from "@/business/components/common/components/MsMainContainer"; import MsContainer from "@/business/components/common/components/MsContainer"; @@ -298,6 +298,11 @@ export default { parseEnvironment(temEnv); //parseEnvironment会改变环境对象的内部结构,从而影响前端列表的显示,所以复制一个环境对象作为代替 let newEnvironment = new Environment(temEnv); newEnvironment.id = null; + newEnvironment.config.databaseConfigs.forEach(dataSource => { + if (dataSource.id) { + dataSource.id = getUUID(); + } + }) newEnvironment.name = this.getNoRepeatName(newEnvironment.name); this.dialogVisible = true; this.currentEnvironment = newEnvironment; diff --git a/frontend/src/business/components/settings/workspace/environment/EnvironmentList.vue b/frontend/src/business/components/settings/workspace/environment/EnvironmentList.vue index cae170f69f..b54788a760 100644 --- a/frontend/src/business/components/settings/workspace/environment/EnvironmentList.vue +++ b/frontend/src/business/components/settings/workspace/environment/EnvironmentList.vue @@ -135,7 +135,7 @@ import EnvironmentEdit from "@/business/components/api/test/components/environme import MsAsideItem from "@/business/components/common/components/MsAsideItem"; import MsAsideContainer from "@/business/components/common/components/MsAsideContainer"; import ProjectSwitch from "@/business/components/common/head/ProjectSwitch"; -import {downloadFile, operationConfirm, strMapToObj} from "@/common/js/utils"; +import {downloadFile, getUUID, operationConfirm, strMapToObj} from "@/common/js/utils"; import EnvironmentImport from "@/business/components/project/menu/EnvironmentImport"; import ShowMoreBtn from "@/business/components/track/case/components/ShowMoreBtn"; import {_handleSelect, _handleSelectAll, getSelectDataCounts, setUnSelectIds} from "@/common/js/tableUtils"; @@ -332,6 +332,11 @@ export default { parseEnvironment(temEnv); //parseEnvironment会改变环境对象的内部结构,从而影响前端列表的显示,所以复制一个环境对象作为代替 let newEnvironment = new Environment(temEnv); newEnvironment.id = null; + newEnvironment.config.databaseConfigs.forEach(dataSource => { + if (dataSource.id) { + dataSource.id = getUUID(); + } + }) newEnvironment.name = this.getNoRepeatName(newEnvironment.name); this.dialogVisible = true; this.currentEnvironment = newEnvironment;