Skip to content

Commit

Permalink
Merge pull request #6233 from yjieliang/feat-#6186
Browse files Browse the repository at this point in the history
feat:增加可以根据PROJECT_ID获取数据库分片信息的接口 #6186
  • Loading branch information
irwinsun authored Mar 10, 2022
2 parents d3581f6 + a2fd5e9 commit fa453a9
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_USER_ID_DEFAULT_VAL
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.common.web.annotation.BkField
import com.tencent.devops.common.web.constant.BkStyleEnum
import com.tencent.devops.project.pojo.DataBasePiecewiseInfo
import com.tencent.devops.project.pojo.DataSource
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
Expand All @@ -46,6 +47,7 @@ import javax.ws.rs.PUT
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces
import javax.ws.rs.QueryParam
import javax.ws.rs.core.MediaType

@Api(tags = ["OP_DATA_SOURCE"], description = "OP-数据源")
Expand Down Expand Up @@ -107,4 +109,19 @@ interface OPDataSourceResource {
@BkField(patternStyle = BkStyleEnum.ID_STYLE)
id: String
): Result<Boolean>

@ApiOperation("根据PROJECT_ID获取数据库分片信息")
@GET
@Path("/piecewise/get")
fun getDataBasePiecewiseById(
@ApiParam("项目ID", required = true)
@QueryParam("projectId")
projectId: String,
@ApiParam("微服务code", required = true)
@QueryParam("moduleCode")
moduleCode: String,
@ApiParam("集群名称", required = true)
@QueryParam("clusterName")
clusterName: String
): Result<DataBasePiecewiseInfo?>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.project.pojo

import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty

@ApiModel("数据库分片信息")
data class DataBasePiecewiseInfo(
@ApiModelProperty("项目ID/项目CODE")
val projectId: String,
@ApiModelProperty("集群名称")
val clusterName: String,
@ApiModelProperty("微服务模块名称")
val moduleCode: String,
@ApiModelProperty("数据源名称")
val dataSourceName: String,
@ApiModelProperty("数据源URL")
val dsUrl: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ data class DataSource(
val dataSourceName: String,
@ApiModelProperty("容量是否满标识")
@field:BkField(patternStyle = BkStyleEnum.BOOLEAN_STYLE)
val fullFlag: Boolean = false
val fullFlag: Boolean = false,
@ApiModelProperty("数据源URL")
val dsUrl: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ package com.tencent.devops.project.dao

import com.tencent.devops.common.api.util.UUIDUtil
import com.tencent.devops.model.project.tables.TDataSource
import com.tencent.devops.model.project.tables.TShardingRoutingRule
import com.tencent.devops.model.project.tables.records.TDataSourceRecord
import com.tencent.devops.project.pojo.DataSource
import org.jooq.Condition
import org.jooq.DSLContext
import org.jooq.Result
import org.jooq.Record1
import org.springframework.stereotype.Repository
import java.time.LocalDateTime

Expand All @@ -49,6 +51,7 @@ class DataSourceDao {
MODULE_CODE,
DATA_SOURCE_NAME,
FULL_FLAG,
DS_URL,
CREATOR,
MODIFIER
)
Expand All @@ -58,6 +61,7 @@ class DataSourceDao {
dataSource.moduleCode.name,
dataSource.dataSourceName,
dataSource.fullFlag,
dataSource.dsUrl,
userId,
userId
).onDuplicateKeyUpdate()
Expand Down Expand Up @@ -118,9 +122,33 @@ class DataSourceDao {
.set(MODULE_CODE, dataSource.moduleCode.name)
.set(DATA_SOURCE_NAME, dataSource.dataSourceName)
.set(FULL_FLAG, dataSource.fullFlag)
.set(DS_URL, dataSource.dsUrl)
.set(UPDATE_TIME, LocalDateTime.now())
.where(ID.eq(id))
.execute()
}
}

fun getRoutingRule(dslContext: DSLContext, projectId: String): Record1<String>? {
val tr = TShardingRoutingRule.T_SHARDING_ROUTING_RULE
return dslContext
.select(tr.ROUTING_RULE)
.from(tr)
.where(tr.ROUTING_NAME.eq(projectId))
.fetchOne()
}

fun getDataBasePiecewiseById(
dslContext: DSLContext,
moduleCode: String,
clusterName: String,
routingRule: String
): TDataSourceRecord? {
with(TDataSource.T_DATA_SOURCE) {
return dslContext.selectFrom(this)
.where(MODULE_CODE.eq(moduleCode))
.and(DATA_SOURCE_NAME.eq(routingRule))
.and(CLUSTER_NAME.eq(clusterName)).fetchOne()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.tencent.devops.project.resources
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.common.web.RestResource
import com.tencent.devops.project.api.op.OPDataSourceResource
import com.tencent.devops.project.pojo.DataBasePiecewiseInfo
import com.tencent.devops.project.pojo.DataSource
import com.tencent.devops.project.service.DataSourceService
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -53,4 +54,13 @@ class OPDataSourceResourceImpl @Autowired constructor(
override fun deleteDataSourceById(userId: String, id: String): Result<Boolean> {
return Result(dataSourceService.deleteDataSource(userId, id))
}

override fun getDataBasePiecewiseById(
projectId: String,
moduleCode: String,
clusterName: String
): Result<DataBasePiecewiseInfo?> {
val result = dataSourceService.getDataBasePiecewiseById(projectId, moduleCode, clusterName)
return if (result != null) Result(result) else Result(0, null, result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

package com.tencent.devops.project.service

import com.tencent.devops.project.pojo.DataBasePiecewiseInfo
import com.tencent.devops.project.pojo.DataSource

interface DataSourceService {
Expand All @@ -38,4 +39,10 @@ interface DataSourceService {
fun updateDataSource(userId: String, id: String, dataSource: DataSource): Boolean

fun getDataSourceById(id: String): DataSource?

fun getDataBasePiecewiseById(
projectId: String,
moduleCode: String,
clusterName: String
): DataBasePiecewiseInfo?
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ import com.tencent.devops.common.api.constant.CommonMessageCode
import com.tencent.devops.common.api.enums.SystemModuleEnum
import com.tencent.devops.common.api.exception.ErrorCodeException
import com.tencent.devops.project.dao.DataSourceDao
import com.tencent.devops.project.pojo.DataBasePiecewiseInfo
import com.tencent.devops.project.pojo.DataSource
import com.tencent.devops.project.service.DataSourceService
import org.jooq.DSLContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils

@Service
class DataSourceServiceImpl @Autowired constructor(
Expand Down Expand Up @@ -101,10 +103,35 @@ class DataSourceServiceImpl @Autowired constructor(
clusterName = record.clusterName,
moduleCode = SystemModuleEnum.valueOf(record.moduleCode),
dataSourceName = record.dataSourceName,
fullFlag = record.fullFlag
fullFlag = record.fullFlag,
dsUrl = record.dsUrl
)
} else {
null
}
}

override fun getDataBasePiecewiseById(
projectId: String,
moduleCode: String,
clusterName: String
): DataBasePiecewiseInfo? {
val routingRule = dataSourceDao.getRoutingRule(dslContext, projectId)?.get(0) as String
if (!StringUtils.isEmpty(routingRule)) {
val dataSource = dataSourceDao.getDataBasePiecewiseById(
dslContext = dslContext,
moduleCode = moduleCode,
clusterName = clusterName,
routingRule = routingRule
) ?: return null
return DataBasePiecewiseInfo(
projectId = projectId,
moduleCode = moduleCode,
clusterName = dataSource.clusterName,
dataSourceName = dataSource.dataSourceName,
dsUrl = dataSource.dsUrl
)
}
return null
}
}
1 change: 1 addition & 0 deletions support-files/sql/1001_ci_project_ddl_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ CREATE TABLE IF NOT EXISTS `T_DATA_SOURCE` (
`FULL_FLAG` bit(1) DEFAULT b'0' COMMENT '容量是否满标识 true:是,false:否',
`CREATOR` varchar(50) NOT NULL DEFAULT 'system' COMMENT '创建者',
`MODIFIER` varchar(50) NOT NULL DEFAULT 'system' COMMENT '修改者',
`DS_URL` varchar(1024) NULL COMMENT '数据源URL地址',
`UPDATE_TIME` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '修改时间',
`CREATE_TIME` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
PRIMARY KEY (`ID`),
Expand Down
27 changes: 27 additions & 0 deletions support-files/sql/2007_ci_project-update_v1.9_mysql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
USE devops_ci_project;
SET NAMES utf8mb4;

DROP PROCEDURE IF EXISTS ci_project_schema_update;

DELIMITER <CI_UBF>

CREATE PROCEDURE ci_project_schema_update()
BEGIN
DECLARE db VARCHAR(100);
SET AUTOCOMMIT = 0;
SELECT DATABASE() INTO db;

IF NOT EXISTS(SELECT 1
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = db
AND TABLE_NAME = 'T_DATA_SOURCE'
AND COLUMN_NAME = 'DS_URL') THEN
ALTER TABLE T_DATA_SOURCE
ADD DS_URL varchar(1024) NULL COMMENT '数据源URL地址' ;
END IF;

COMMIT;
END <CI_UBF>
DELIMITER ;
COMMIT;
CALL ci_project_schema_update();

0 comments on commit fa453a9

Please sign in to comment.