refactor: 优化版本sql

This commit is contained in:
zhangdahai112 2022-01-23 13:10:41 +08:00 committed by 刘瑞斌
parent 6f35a6ff79
commit 24c4a44682
6 changed files with 30 additions and 63 deletions

View File

@ -65,7 +65,7 @@ public class TestCase implements Serializable {
private String refId;
private Byte latest;
private Boolean latest;
private static final long serialVersionUID = 1L;
}

View File

@ -2144,52 +2144,52 @@ public class TestCaseExample {
return (Criteria) this;
}
public Criteria andLatestEqualTo(Byte value) {
public Criteria andLatestEqualTo(Boolean value) {
addCriterion("latest =", value, "latest");
return (Criteria) this;
}
public Criteria andLatestNotEqualTo(Byte value) {
public Criteria andLatestNotEqualTo(Boolean value) {
addCriterion("latest <>", value, "latest");
return (Criteria) this;
}
public Criteria andLatestGreaterThan(Byte value) {
public Criteria andLatestGreaterThan(Boolean value) {
addCriterion("latest >", value, "latest");
return (Criteria) this;
}
public Criteria andLatestGreaterThanOrEqualTo(Byte value) {
public Criteria andLatestGreaterThanOrEqualTo(Boolean value) {
addCriterion("latest >=", value, "latest");
return (Criteria) this;
}
public Criteria andLatestLessThan(Byte value) {
public Criteria andLatestLessThan(Boolean value) {
addCriterion("latest <", value, "latest");
return (Criteria) this;
}
public Criteria andLatestLessThanOrEqualTo(Byte value) {
public Criteria andLatestLessThanOrEqualTo(Boolean value) {
addCriterion("latest <=", value, "latest");
return (Criteria) this;
}
public Criteria andLatestIn(List<Byte> values) {
public Criteria andLatestIn(List<Boolean> values) {
addCriterion("latest in", values, "latest");
return (Criteria) this;
}
public Criteria andLatestNotIn(List<Byte> values) {
public Criteria andLatestNotIn(List<Boolean> values) {
addCriterion("latest not in", values, "latest");
return (Criteria) this;
}
public Criteria andLatestBetween(Byte value1, Byte value2) {
public Criteria andLatestBetween(Boolean value1, Boolean value2) {
addCriterion("latest between", value1, value2, "latest");
return (Criteria) this;
}
public Criteria andLatestNotBetween(Byte value1, Byte value2) {
public Criteria andLatestNotBetween(Boolean value1, Boolean value2) {
addCriterion("latest not between", value1, value2, "latest");
return (Criteria) this;
}

View File

@ -32,7 +32,7 @@
<result column="case_public" jdbcType="BIT" property="casePublic" />
<result column="version_id" jdbcType="VARCHAR" property="versionId" />
<result column="ref_id" jdbcType="VARCHAR" property="refId" />
<result column="latest" jdbcType="TINYINT" property="latest" />
<result column="latest" jdbcType="BIT" property="latest" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.TestCaseWithBLOBs">
<result column="prerequisite" jdbcType="LONGVARCHAR" property="prerequisite" />
@ -181,7 +181,7 @@
#{customNum,jdbcType=VARCHAR}, #{createUser,jdbcType=VARCHAR}, #{originalStatus,jdbcType=VARCHAR},
#{deleteTime,jdbcType=BIGINT}, #{deleteUserId,jdbcType=VARCHAR}, #{order,jdbcType=BIGINT},
#{casePublic,jdbcType=BIT}, #{versionId,jdbcType=VARCHAR}, #{refId,jdbcType=VARCHAR},
#{latest,jdbcType=TINYINT}, #{prerequisite,jdbcType=LONGVARCHAR}, #{remark,jdbcType=LONGVARCHAR},
#{latest,jdbcType=BIT}, #{prerequisite,jdbcType=LONGVARCHAR}, #{remark,jdbcType=LONGVARCHAR},
#{steps,jdbcType=LONGVARCHAR}, #{stepDescription,jdbcType=LONGVARCHAR}, #{expectedResult,jdbcType=LONGVARCHAR},
#{customFields,jdbcType=LONGVARCHAR})
</insert>
@ -392,7 +392,7 @@
#{refId,jdbcType=VARCHAR},
</if>
<if test="latest != null">
#{latest,jdbcType=TINYINT},
#{latest,jdbcType=BIT},
</if>
<if test="prerequisite != null">
#{prerequisite,jdbcType=LONGVARCHAR},
@ -514,7 +514,7 @@
ref_id = #{record.refId,jdbcType=VARCHAR},
</if>
<if test="record.latest != null">
latest = #{record.latest,jdbcType=TINYINT},
latest = #{record.latest,jdbcType=BIT},
</if>
<if test="record.prerequisite != null">
prerequisite = #{record.prerequisite,jdbcType=LONGVARCHAR},
@ -571,7 +571,7 @@
case_public = #{record.casePublic,jdbcType=BIT},
version_id = #{record.versionId,jdbcType=VARCHAR},
ref_id = #{record.refId,jdbcType=VARCHAR},
latest = #{record.latest,jdbcType=TINYINT},
latest = #{record.latest,jdbcType=BIT},
prerequisite = #{record.prerequisite,jdbcType=LONGVARCHAR},
remark = #{record.remark,jdbcType=LONGVARCHAR},
steps = #{record.steps,jdbcType=LONGVARCHAR},
@ -614,7 +614,7 @@
case_public = #{record.casePublic,jdbcType=BIT},
version_id = #{record.versionId,jdbcType=VARCHAR},
ref_id = #{record.refId,jdbcType=VARCHAR},
latest = #{record.latest,jdbcType=TINYINT}
latest = #{record.latest,jdbcType=BIT}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -710,7 +710,7 @@
ref_id = #{refId,jdbcType=VARCHAR},
</if>
<if test="latest != null">
latest = #{latest,jdbcType=TINYINT},
latest = #{latest,jdbcType=BIT},
</if>
<if test="prerequisite != null">
prerequisite = #{prerequisite,jdbcType=LONGVARCHAR},
@ -764,7 +764,7 @@
case_public = #{casePublic,jdbcType=BIT},
version_id = #{versionId,jdbcType=VARCHAR},
ref_id = #{refId,jdbcType=VARCHAR},
latest = #{latest,jdbcType=TINYINT},
latest = #{latest,jdbcType=BIT},
prerequisite = #{prerequisite,jdbcType=LONGVARCHAR},
remark = #{remark,jdbcType=LONGVARCHAR},
steps = #{steps,jdbcType=LONGVARCHAR},
@ -804,7 +804,7 @@
case_public = #{casePublic,jdbcType=BIT},
version_id = #{versionId,jdbcType=VARCHAR},
ref_id = #{refId,jdbcType=VARCHAR},
latest = #{latest,jdbcType=TINYINT}
latest = #{latest,jdbcType=BIT}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>

View File

@ -1,8 +0,0 @@
package io.metersphere.commons.constants;
public class VersionConstants {
//最新版本
public static Byte LATEST = 1;
//非最新版本 可以不用设置数据库 default = 0
public static Byte NOT_LATEST = 0;
}

View File

@ -22,7 +22,6 @@ import io.metersphere.base.mapper.ext.ExtTestCaseMapper;
import io.metersphere.commons.constants.TestCaseConstants;
import io.metersphere.commons.constants.TestCaseReviewStatus;
import io.metersphere.commons.constants.UserGroupType;
import io.metersphere.commons.constants.VersionConstants;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.*;
@ -205,7 +204,7 @@ public class TestCaseService {
request.setRefId(request.getId());
}
//完全新增一条记录直接就是最新
request.setLatest(VersionConstants.LATEST);
request.setLatest(true);
testCaseMapper.insert(request);
saveFollows(request.getId(), request.getFollows());
return request;
@ -2457,7 +2456,7 @@ public class TestCaseService {
*/
private void checkAndSetLatestVersion(String refId, String versionId, String projectId) {
TestCaseExample e = new TestCaseExample();
e.createCriteria().andRefIdEqualTo(refId).andLatestEqualTo(VersionConstants.LATEST);
e.createCriteria().andRefIdEqualTo(refId).andLatestEqualTo(true);
//如果因为删除导致没有了最新的版本则按照版本创建顺序选择一个版本为最新版本
if (testCaseMapper.countByExample(e) == 0) {
extTestCaseMapper.addLatestVersion(refId);

View File

@ -201,50 +201,26 @@ ALTER TABLE project
-- 增加 latest 字段优化最新版本查询
ALTER TABLE test_case
ADD latest tinyint DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
ADD latest tinyint(1) DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
ALTER TABLE api_definition
ADD latest tinyint DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
ADD latest tinyint(1) DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
ALTER TABLE api_scenario
ADD latest tinyint DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
ADD latest tinyint(1) DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
ALTER TABLE load_test
ADD latest tinyint DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
ADD latest tinyint(1) DEFAULT 0 COMMENT '是否为最新版本 0:否1:是';
-- 设置最新版本的数据
UPDATE test_case
SET latest = 1
WHERE version_id = (
SELECT id
FROM project_version
WHERE project_version.project_id = test_case.project_id
AND project_version.latest = 1
);
SET latest = 1;
UPDATE api_definition
SET latest = 1
WHERE version_id = (
SELECT id
FROM project_version
WHERE project_version.project_id = api_definition.project_id
AND project_version.latest = 1
);
SET latest = 1;
UPDATE api_scenario
SET latest = 1
WHERE version_id = (
SELECT id
FROM project_version
WHERE project_version.project_id = api_scenario.project_id
AND project_version.latest = 1
);
SET latest = 1;
UPDATE load_test
SET latest = 1
WHERE version_id = (
SELECT id
FROM project_version
WHERE project_version.project_id = load_test.project_id
AND project_version.latest = 1
);
SET latest = 1;