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 756fd299c0..02225aedf8 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 @@ -12,6 +12,7 @@ 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.mockconfig.MockConfigStaticData; +import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.service.ApiTestEnvironmentService; @@ -785,4 +786,29 @@ public class ElementUtil { return evlValue; } } + + 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 6efd761420..e9da251fa8 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 1dd387d527..90a20ff10e 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 7a1f63dd0f..db41a5c1e5 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 @@ -146,7 +146,10 @@ public class MsJDBCSampler extends MsTestElement { if (config.isEffective(this.getProjectId())) { if (config.getConfig().get(this.getProjectId()) != null) { envConfig = config.getConfig().get(this.getProjectId()); - if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { + 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 2513d092e7..f9549376cc 100644 --- a/frontend/src/business/components/api/definition/components/environment/ApiEnvironmentConfig.vue +++ b/frontend/src/business/components/api/definition/components/environment/ApiEnvironmentConfig.vue @@ -91,6 +91,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 c23229abb1..cbb1c6e173 100644 --- a/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue +++ b/frontend/src/business/components/api/test/components/ApiEnvironmentConfig.vue @@ -116,6 +116,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/project/menu/EnvironmentList.vue b/frontend/src/business/components/project/menu/EnvironmentList.vue index 83924cc86a..89aedeee18 100644 --- a/frontend/src/business/components/project/menu/EnvironmentList.vue +++ b/frontend/src/business/components/project/menu/EnvironmentList.vue @@ -32,7 +32,9 @@ @@ -94,12 +96,14 @@ {{ row.conditionType ? "-" : getDetails(row) }} - + - +