diff options
author | Aleksandra Bozhinoska <aleksandra.bozhinoska@sonarsource.com> | 2025-02-25 09:21:09 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2025-02-25 20:03:22 +0000 |
commit | f1823e256196f2d03219c3f4109c36409c388f6a (patch) | |
tree | 979e9c6a6b805697f16fa7658c7005001a814feb | |
parent | c5b73557ee84d1c9af425d025bf039ce5da586f9 (diff) | |
download | sonarqube-f1823e256196f2d03219c3f4109c36409c388f6a.tar.gz sonarqube-f1823e256196f2d03219c3f4109c36409c388f6a.zip |
SONAR-24320 Cleanup when incremental migration is disabled
13 files changed, 268 insertions, 40 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index d7e24c7aa54..f5798a9a2cd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -63,6 +63,10 @@ public class BranchDao implements Dao { return mapper(dbSession).updateExcludeFromPurge(branchUuid, excludeFromPurge, now); } + public void updateMeasuresMigratedToFalse(DbSession dbSession) { + mapper(dbSession).updateMeasuresMigratedToFalse(); + } + public Optional<BranchDto> selectByBranchKey(DbSession dbSession, String projectUuid, String key) { return selectByKey(dbSession, projectUuid, key, BranchType.BRANCH); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java index 95adbb62244..43dc1485d63 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java @@ -36,6 +36,8 @@ public interface BranchMapper { int updateExcludeFromPurge(@Param("uuid") String uuid, @Param("excludeFromPurge") boolean excludeFromPurge, @Param("now") long now); + void updateMeasuresMigratedToFalse(); + BranchDto selectByKey(@Param("projectUuid") String projectUuid, @Param("key") String key, @Param("branchType") BranchType branchType); List<BranchDto> selectByKeys(@Param("projectUuid") String projectUuid, @Param("keys") Set<String> branchKeys); @@ -68,7 +70,7 @@ public interface BranchMapper { long updateAllNeedIssueSyncForProject(@Param("projectUuid") String projectUuid, @Param("now") long now); - long updateNeedIssueSync(@Param("uuid") String uuid, @Param("needIssueSync")boolean needIssueSync,@Param("now") long now); + long updateNeedIssueSync(@Param("uuid") String uuid, @Param("needIssueSync") boolean needIssueSync, @Param("now") long now); short doAnyOfComponentsNeedIssueSync(@Param("componentKeys") List<String> components); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java index 0a6863b6fe6..793a7e4f985 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioDao.java @@ -267,6 +267,10 @@ public class PortfolioDao implements Dao { return mapper(session).countByMeasuresMigratedFalse(); } + public void updateMeasuresMigratedToFalse(DbSession dbSession) { + mapper(dbSession).updateMeasuresMigratedToFalse(); + } + @VisibleForTesting boolean isMeasuresMigrated(DbSession dbSession, String uuid) { return mapper(dbSession).isMeasuresMigrated(uuid); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java index 1dbb66749bb..3209b133158 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/portfolio/PortfolioMapper.java @@ -114,6 +114,8 @@ public interface PortfolioMapper { int updateMeasuresMigrated(@Param("uuid") String uuid, @Param("measuresMigrated") boolean measuresMigrated, @Param("now") long now); + void updateMeasuresMigratedToFalse(); + List<String> selectUuidsWithMeasuresMigratedFalse(int limit); int countByMeasuresMigratedFalse(); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml index 238da88d1bd..c28a7229d09 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml @@ -297,4 +297,9 @@ pb.measures_migrated = ${_false} </select> + <update id="updateMeasuresMigratedToFalse"> + update project_branches + set measures_migrated = ${_false} + </update> + </mapper> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml index db973bc0b50..fc08501f0e1 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/portfolio/PortfolioMapper.xml @@ -547,6 +547,11 @@ root_uuid = #{uuid, jdbcType=VARCHAR} </update> + <update id="updateMeasuresMigratedToFalse"> + update portfolios + set measures_migrated = ${_false} + </update> + <select id="selectUuidsWithMeasuresMigratedFalse" resultType="java.lang.String" parameterType="int"> SELECT p.uuid FROM portfolios p diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java index 5c374541c90..fc3565e94f5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/BranchDaoTest.java @@ -133,7 +133,7 @@ public class BranchDaoTest { @DataProvider public static Object[][] nullOrEmpty() { - return new Object[][] { + return new Object[][]{ {null}, {""} }; @@ -143,7 +143,7 @@ public class BranchDaoTest { public static Object[][] oldAndNewValuesCombinations() { String value1 = randomAlphabetic(10); String value2 = randomAlphabetic(20); - return new Object[][] { + return new Object[][]{ {null, value1}, {"", value1}, {value1, null}, @@ -398,7 +398,7 @@ public class BranchDaoTest { BranchDto branch1 = db.components().insertProjectBranch(project1, b -> b.setKey("branch1")); BranchDto branch2 = db.components().insertProjectBranch(project2, b -> b.setKey("branch2")); - BranchDto branch3 = db.components().insertProjectBranch(project3, b -> b.setKey("branch3")); + db.components().insertProjectBranch(project3, b -> b.setKey("branch3")); Map<String, String> branchKeysByProjectUuid = new HashMap<>(); branchKeysByProjectUuid.put(project1.getUuid(), "branch1"); @@ -541,7 +541,7 @@ public class BranchDaoTest { public void selectByUuid() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto branch1 = db.components().insertProjectBranch(project); - ComponentDto branch2 = db.components().insertProjectBranch(project); + db.components().insertProjectBranch(project); assertThat(underTest.selectByUuid(db.getSession(), branch1.uuid()).get()) .extracting(BranchDto::getUuid) @@ -573,7 +573,8 @@ public class BranchDaoTest { db.measures().insertLiveMeasure(project3, unanalyzedC); assertThat(underTest.countPrBranchAnalyzedLanguageByProjectUuid(db.getSession())) - .extracting(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, PrBranchAnalyzedLanguageCountByProjectDto::getBranch, PrBranchAnalyzedLanguageCountByProjectDto::getPullRequest) + .extracting(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, PrBranchAnalyzedLanguageCountByProjectDto::getBranch, + PrBranchAnalyzedLanguageCountByProjectDto::getPullRequest) .containsExactlyInAnyOrder( tuple(project1.uuid(), 3L, 3L), tuple(project2.uuid(), 1L, 1L), @@ -592,13 +593,13 @@ public class BranchDaoTest { ProjectDto project3Dto = db.components().getProjectDto(project3); ProjectDto project4Dto = db.components().getProjectDto(project4); - BranchDto branch1 = db.components().insertProjectBranch(project1Dto, branchDto -> branchDto.setNeedIssueSync(true)); - BranchDto branch2 = db.components().insertProjectBranch(project1Dto, branchDto -> branchDto.setNeedIssueSync(false)); - BranchDto branch3 = db.components().insertProjectBranch(project2Dto); + db.components().insertProjectBranch(project1Dto, branchDto -> branchDto.setNeedIssueSync(true)); + db.components().insertProjectBranch(project1Dto, branchDto -> branchDto.setNeedIssueSync(false)); + db.components().insertProjectBranch(project2Dto); assertThat(underTest.selectProjectUuidsWithIssuesNeedSync(db.getSession(), Sets.newHashSet(project1Dto.getUuid(), project2Dto.getUuid(), project3Dto.getUuid(), project4Dto.getUuid()))) - .containsOnly(project1.uuid()); + .containsOnly(project1.uuid()); } @Test @@ -607,13 +608,13 @@ public class BranchDaoTest { assertThat(underTest.hasAnyBranchWhereNeedIssueSync(dbSession, false)).isFalse(); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setNeedIssueSync(false)); + db.components().insertProjectBranch(project, b -> b.setNeedIssueSync(false)); assertThat(underTest.hasAnyBranchWhereNeedIssueSync(dbSession, true)).isFalse(); assertThat(underTest.hasAnyBranchWhereNeedIssueSync(dbSession, false)).isTrue(); project = db.components().insertPrivateProject(); - branch = db.components().insertProjectBranch(project, b -> b.setNeedIssueSync(true)); + db.components().insertProjectBranch(project, b -> b.setNeedIssueSync(true)); assertThat(underTest.hasAnyBranchWhereNeedIssueSync(dbSession, true)).isTrue(); } @@ -622,9 +623,9 @@ public class BranchDaoTest { assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.BRANCH, 0L)).isZero(); ComponentDto project = db.components().insertPrivateProject(); - ComponentDto branch1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); - ComponentDto branch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); - ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); + db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); + db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); + db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.BRANCH, 0L)).isEqualTo(3); assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.BRANCH, NOW)).isEqualTo(3); assertThat(underTest.countByTypeAndCreationDate(dbSession, BranchType.BRANCH, NOW + 100)).isZero(); @@ -838,11 +839,11 @@ public class BranchDaoTest { ComponentDto project2 = db.components().insertPrivateProject(); ProjectDto projectDto = db.components().getProjectDto(project1); db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)); - BranchDto projectBranch1 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)); - BranchDto projectBranch2 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false)); + db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(true)); + db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false)); db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.BRANCH).setNeedIssueSync(false)); - BranchDto pullRequest1 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(true)); - BranchDto pullRequest2 = db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(false)); + db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(true)); + db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(false)); db.components().insertProjectBranch(projectDto, b -> b.setBranchType(BranchType.PULL_REQUEST).setNeedIssueSync(true)); assertThat(underTest.doAnyOfComponentsNeedIssueSync(dbSession, singletonList(project1.getKey()))).isTrue(); @@ -865,4 +866,21 @@ public class BranchDaoTest { assertThat(underTest.doAnyOfComponentsNeedIssueSync(dbSession, componentKeys)).isTrue(); } + + @Test + public void updateMeasuresMigratedToFalse() throws SQLException { + createMeasuresMigratedColumn(); + + ComponentDto project = db.components().insertPrivateProject(); + String uuid1 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)).uuid(); + String uuid2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)).uuid(); + + underTest.updateMeasuresMigrated(dbSession, uuid1, true); + underTest.updateMeasuresMigrated(dbSession, uuid2, true); + + underTest.updateMeasuresMigratedToFalse(dbSession); + + assertThat(underTest.isMeasuresMigrated(dbSession, uuid1)).isFalse(); + assertThat(underTest.isMeasuresMigrated(dbSession, uuid2)).isFalse(); + } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java index 9ea18c73c0e..903d3a96045 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/portfolio/PortfolioDaoTest.java @@ -57,8 +57,8 @@ public class PortfolioDaoTest { public void selectAllRoots() { PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1"); PortfolioDto p11 = addPortfolio(p1, "p11"); - PortfolioDto p111 = addPortfolio(p11, "p111"); - PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2"); + addPortfolio(p11, "p111"); + db.components().insertPrivatePortfolioDto("p2"); assertThat(portfolioDao.selectAllRoots(session)).extracting("uuid") .containsExactlyInAnyOrder("p1", "p2"); @@ -68,8 +68,8 @@ public class PortfolioDaoTest { public void selectAll() { PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1"); PortfolioDto p11 = addPortfolio(p1, "p11"); - PortfolioDto p111 = addPortfolio(p11, "p111"); - PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2"); + addPortfolio(p11, "p111"); + db.components().insertPrivatePortfolioDto("p2"); assertThat(portfolioDao.selectAll(session)).extracting("uuid") .containsExactlyInAnyOrder("p1", "p2", "p11", "p111"); @@ -79,9 +79,9 @@ public class PortfolioDaoTest { public void selectTree() { PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1"); PortfolioDto p11 = addPortfolio(p1, "p11"); - PortfolioDto p111 = addPortfolio(p11, "p111"); - PortfolioDto p12 = addPortfolio(p1, "p12"); - PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2"); + addPortfolio(p11, "p111"); + addPortfolio(p1, "p12"); + db.components().insertPrivatePortfolioDto("p2"); assertThat(portfolioDao.selectTree(session, "p1")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12"); assertThat(portfolioDao.selectTree(session, "p11")).extracting("uuid").containsOnly("p1", "p11", "p111", "p12"); @@ -159,12 +159,12 @@ public class PortfolioDaoTest { @Test public void delete() { ProjectDto proj1 = db.components().insertPrivateProjectDto("proj1"); - ProjectDto app1 = db.components().insertPrivateApplicationDto(); + db.components().insertPrivateApplicationDto(); PortfolioDto p1 = db.components().insertPrivatePortfolioDto("p1"); PortfolioDto p2 = db.components().insertPrivatePortfolioDto("p2"); PortfolioDto p3 = db.components().insertPrivatePortfolioDto("p3"); - PortfolioDto p4 = db.components().insertPrivatePortfolioDto("p4"); + db.components().insertPrivatePortfolioDto("p4"); db.components().addPortfolioProject(p1, proj1); db.components().addPortfolioProject(p2, proj1); @@ -194,7 +194,7 @@ public class PortfolioDaoTest { public void deleteAllDescendantPortfolios() { PortfolioDto root = db.components().insertPrivatePortfolioDto(); PortfolioDto child1 = addPortfolio(root); - PortfolioDto child11 = addPortfolio(child1); + addPortfolio(child1); PortfolioDto root2 = db.components().insertPrivatePortfolioDto(); portfolioDao.deleteAllDescendantPortfolios(session, root.getUuid()); @@ -226,7 +226,7 @@ public class PortfolioDaoTest { db.components().insertPrivatePortfolioDto("portfolio1"); db.components().insertPrivatePortfolioDto("portfolio2"); db.components().insertPrivatePortfolioDto("portfolio3"); - ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setKey("app1")); + db.components().insertPrivateApplicationDto(p -> p.setKey("app1")); portfolioDao.addReference(session, "portfolio1", "portfolio2"); portfolioDao.addReference(session, "portfolio2", "portfolio3"); @@ -282,8 +282,8 @@ public class PortfolioDaoTest { @Test public void selectAllReferencesToApplicationsInHierarchy() { var p1 = db.components().insertPrivatePortfolioDto("portfolio1"); - var p2 = db.components().insertPrivatePortfolioDto("portfolio2", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); - var p3 = db.components().insertPrivatePortfolioDto("portfolio3", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); + db.components().insertPrivatePortfolioDto("portfolio2", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); + db.components().insertPrivatePortfolioDto("portfolio3", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); ProjectDto app1 = db.components().insertPrivateApplicationDto(p -> p.setKey("app1")); ProjectDto app2 = db.components().insertPrivateApplicationDto(p -> p.setKey("app2")); ProjectDto app3 = db.components().insertPrivateApplicationDto(p -> p.setKey("app3")); @@ -300,8 +300,8 @@ public class PortfolioDaoTest { @Test public void selectAllReferencesToPortfoliosInHierarchy() { var p1 = db.components().insertPrivatePortfolioDto("portfolio1"); - var p2 = db.components().insertPrivatePortfolioDto("portfolio2", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); - var p3 = db.components().insertPrivatePortfolioDto("portfolio3", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); + db.components().insertPrivatePortfolioDto("portfolio2", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); + db.components().insertPrivatePortfolioDto("portfolio3", p -> p.setRootUuid(p1.getUuid()).setParentUuid(p1.getUuid())); var p4 = db.components().insertPrivatePortfolioDto("portfolio4"); var p5 = db.components().insertPrivatePortfolioDto("portfolio5"); var p6 = db.components().insertPrivatePortfolioDto("portfolio6"); @@ -442,7 +442,7 @@ public class PortfolioDaoTest { @Test public void selectReferencers() { - PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("portfolio1"); + db.components().insertPrivatePortfolioDto("portfolio1"); PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("portfolio2"); ProjectDto app1 = db.components().insertPrivateApplicationDto(c -> c.setUuid("app1")); @@ -494,14 +494,14 @@ public class PortfolioDaoTest { @Test public void deleteReferencesTo_with_non_existing_reference_doesnt_fail() { - portfolioDao.deleteReferencesTo(session, "portfolio3"); + assertThatCode(() -> portfolioDao.deleteReferencesTo(session, "portfolio3")).doesNotThrowAnyException(); } @Test public void deleteAllReferences() { PortfolioDto root = db.components().insertPrivatePortfolioDto(); PortfolioDto child1 = addPortfolio(root); - PortfolioDto child11 = addPortfolio(child1); + addPortfolio(child1); PortfolioDto root2 = db.components().insertPrivatePortfolioDto(); PortfolioDto root3 = db.components().insertPrivatePortfolioDto(); PortfolioDto root4 = db.components().insertPrivatePortfolioDto(); @@ -644,8 +644,8 @@ public class PortfolioDaoTest { @Test public void delete_nonexisting_branch_doesnt_fail() { - DbSession session = db.getSession(); - assertThatCode(() -> portfolioDao.deleteBranch(session, "nonexisting1", "nonexisting2", "branch1")) + DbSession theSession = db.getSession(); + assertThatCode(() -> portfolioDao.deleteBranch(theSession, "nonexisting1", "nonexisting2", "branch1")) .doesNotThrowAnyException(); } @@ -716,6 +716,22 @@ public class PortfolioDaoTest { } @Test + public void update_measures_migrated_to_false() throws SQLException { + new AddMeasuresMigratedColumnToPortfoliosTable(db.getDbClient().getDatabase()).execute(); + + PortfolioDto portfolio1 = db.components().insertPrivatePortfolioDto("name1"); + PortfolioDto portfolio2 = db.components().insertPrivatePortfolioDto("name2"); + + portfolioDao.updateMeasuresMigrated(session, portfolio1.getUuid(), true); + portfolioDao.updateMeasuresMigrated(session, portfolio2.getUuid(), true); + + portfolioDao.updateMeasuresMigratedToFalse(session); + + assertThat(portfolioDao.isMeasuresMigrated(session, portfolio1.getUuid())).isFalse(); + assertThat(portfolioDao.isMeasuresMigrated(session, portfolio2.getUuid())).isFalse(); + } + + @Test public void selectUuidsWithMeasuresMigratedFalse() throws SQLException { createMeasuresMigratedColumn(); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/adhoc/AddMeasuresMigratedColumnToPortfoliosTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/adhoc/AddMeasuresMigratedColumnToPortfoliosTable.java index b626863fc72..c5ee15900d3 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/adhoc/AddMeasuresMigratedColumnToPortfoliosTable.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/adhoc/AddMeasuresMigratedColumnToPortfoliosTable.java @@ -25,7 +25,7 @@ import org.sonar.db.Database; @ServerSide public class AddMeasuresMigratedColumnToPortfoliosTable extends AbstractAddMeasuresMigratedColumnToTable { - static final String PORTFOLIOS_TABLE_NAME = "portfolios"; + public static final String PORTFOLIOS_TABLE_NAME = "portfolios"; public AddMeasuresMigratedColumnToPortfoliosTable(Database db) { super(db, PORTFOLIOS_TABLE_NAME); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTable.java new file mode 100644 index 00000000000..14402205cb6 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTable.java @@ -0,0 +1,33 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.adhoc; + +import org.sonar.api.server.ServerSide; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.step.TruncateTableChange; + +import static org.sonar.server.platform.db.migration.adhoc.CreateMeasuresTable.MEASURES_TABLE_NAME; + +@ServerSide +public class TruncateMeasuresTable extends TruncateTableChange { + public TruncateMeasuresTable(Database db) { + super(db, MEASURES_TABLE_NAME); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/TruncateTableChange.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/TruncateTableChange.java new file mode 100644 index 00000000000..c5bc561f6dd --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/TruncateTableChange.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.step; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; + +public abstract class TruncateTableChange extends DataChange { + + protected final String tableName; + + protected TruncateTableChange(Database db, String tableName) { + super(db); + this.tableName = tableName; + } + + @Override + public void execute(Context context) throws SQLException { + if (!tableExists()) { + return; + } + + try (var upsert = context.prepareUpsert(truncateQuery())) { + upsert.execute().commit(); + } + } + + private String truncateQuery() { + return String.format("TRUNCATE TABLE %s", tableName); + } + + private boolean tableExists() throws SQLException { + try (var connection = getDatabase().getDataSource().getConnection()) { + return DatabaseUtils.tableExists(tableName, connection); + } + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTableTest.java new file mode 100644 index 00000000000..7a7dcdd7842 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTableTest.java @@ -0,0 +1,75 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.platform.db.migration.adhoc; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Map; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; +import org.sonar.db.DatabaseUtils; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; +import static org.sonar.server.platform.db.migration.adhoc.CreateMeasuresTable.MEASURES_TABLE_NAME; + +public class TruncateMeasuresTableTest { + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(TruncateMeasuresTableTest.class, "schema.sql"); + private final TruncateMeasuresTable underTest = new TruncateMeasuresTable(db.database()); + + @Test + public void execute_when_data_is_populated() throws SQLException { + insertMeasure(); + assertThat(db.countRowsOfTable(MEASURES_TABLE_NAME)).isOne(); + underTest.execute(); + assertThat(db.countRowsOfTable(MEASURES_TABLE_NAME)).isZero(); + assertThatCode(underTest::execute).doesNotThrowAnyException(); + } + + @Test + public void execute_when_table_does_not_exist_should_not_fail() throws SQLException { + dropMeasuresTable(); + assertThatCode(underTest::execute).doesNotThrowAnyException(); + } + + private void insertMeasure() { + Map<String, Object> columnValuePairs = Map.of( + "COMPONENT_UUID", "dc9e5eb4-3f29-4340-a891-65bdc98a2dcd", + "BRANCH_UUID", "74a9e328-be25-4e47-b3cd-efffec865837", + "JSON_VALUE", "{\"coverage\":\"80\"}", + "JSON_VALUE_HASH", 841423045768002689L, + "CREATED_AT", 1L, + "UPDATED_AT", 1L + ); + db.executeInsert(MEASURES_TABLE_NAME, columnValuePairs); + } + + private void dropMeasuresTable() throws SQLException { + try (Connection connection = db.database().getDataSource().getConnection()) { + boolean tableExists = DatabaseUtils.tableExists(MEASURES_TABLE_NAME, connection); + if (tableExists) { + db.executeDdl("drop table " + MEASURES_TABLE_NAME); + } + } + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTableTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTableTest/schema.sql new file mode 100644 index 00000000000..c39825b6ba8 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/adhoc/TruncateMeasuresTableTest/schema.sql @@ -0,0 +1,9 @@ +CREATE TABLE "MEASURES"( + "COMPONENT_UUID" CHARACTER VARYING(40) NOT NULL, + "BRANCH_UUID" CHARACTER VARYING(40) NOT NULL, + "JSON_VALUE" CHARACTER LARGE OBJECT NOT NULL, + "JSON_VALUE_HASH" BIGINT NOT NULL, + "CREATED_AT" BIGINT NOT NULL, + "UPDATED_AT" BIGINT NOT NULL +); +ALTER TABLE "MEASURES" ADD CONSTRAINT "PK_MEASURES" PRIMARY KEY("COMPONENT_UUID"); |