From e3109b5fa377886002072a5289619b361cef2f29 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 3 Apr 2020 10:40:39 +0200 Subject: [PATCH] SONAR-13221 Set primary key of 'SNAPSHOTS' table to column 'UUID' and drop 'ID' --- .../projectanalysis/analysis/Analysis.java | 22 +----- .../step/BuildComponentTreeStep.java | 1 - .../analysis/AnalysisImplTest.java | 30 +------- .../AnalysisMetadataHolderImplTest.java | 1 - .../filemove/FileMoveDetectionStepTest.java | 5 +- .../scm/ScmInfoDbLoaderTest.java | 2 - ...ProjectDuplicationsRepositoryStepTest.java | 17 +++-- .../org/sonar/db/component/SnapshotDto.java | 18 +---- .../sonar/db/purge/PurgeableAnalysisDto.java | 10 --- .../org/sonar/db/component/SnapshotMapper.xml | 3 +- .../org/sonar/db/purge/PurgeMapper.xml | 1 - server/sonar-db-dao/src/schema/schema-sq.ddl | 3 +- .../sonar/db/component/SnapshotDaoTest.java | 4 +- .../sonar/db/component/SnapshotDtoTest.java | 2 - .../org/sonar/db/purge/PurgeCommandsTest.java | 75 +++++++++---------- .../java/org/sonar/db/purge/PurgeDaoTest.java | 12 +-- .../period/DefaultPeriodCleanerTest.java | 6 +- .../db/migration/version/v83/DbVersion83.java | 8 ++ ...rimaryKeyOnUuidColumnOfSnapshotsTable.java | 38 ++++++++++ .../issues/DropIdColumnOfSnapshotsTable.java | 41 ++++++++++ ...pPrimaryKeyOnIdColumnOfSnapshotsTable.java | 41 ++++++++++ ...ryKeyOnUuidColumnOfSnapshotsTableTest.java | 50 +++++++++++++ .../DropIdColumnOfSnapshotsTableTest.java | 51 +++++++++++++ ...maryKeyOnIdColumnOfSnapshotsTableTest.java | 56 ++++++++++++++ .../schema.sql | 30 ++++++++ .../schema.sql | 31 ++++++++ .../schema.sql | 31 ++++++++ 27 files changed, 446 insertions(+), 143 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/snapshots/issues/AddPrimaryKeyOnUuidColumnOfSnapshotsTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/snapshots/issues/DropIdColumnOfSnapshotsTable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/snapshots/issues/DropPrimaryKeyOnIdColumnOfSnapshotsTable.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/snapshots/AddPrimaryKeyOnUuidColumnOfSnapshotsTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/snapshots/DropIdColumnOfSnapshotsTableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/snapshots/DropPrimaryKeyOnIdColumnOfSnapshotsTableTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/snapshots/AddPrimaryKeyOnUuidColumnOfSnapshotsTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/snapshots/DropIdColumnOfSnapshotsTableTest/schema.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v83/snapshots/DropPrimaryKeyOnIdColumnOfSnapshotsTableTest/schema.sql diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/Analysis.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/Analysis.java index 24b8e35c443..ac973cfec1a 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/Analysis.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/analysis/Analysis.java @@ -19,6 +19,7 @@ */ package org.sonar.ce.task.projectanalysis.analysis; +import java.util.Objects; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -28,20 +29,14 @@ import static com.google.common.base.Preconditions.checkNotNull; @Immutable public class Analysis { - private long id; private String uuid; private long createdAt; private Analysis(Builder builder) { - this.id = builder.id; this.uuid = builder.uuid; this.createdAt = builder.createdAt; } - public long getId() { - return id; - } - public String getUuid() { return uuid; } @@ -51,18 +46,11 @@ public class Analysis { } public static final class Builder { - @CheckForNull - private Long id; @CheckForNull private String uuid; @CheckForNull private Long createdAt; - public Builder setId(long id) { - this.id = id; - return this; - } - public Builder setUuid(String uuid) { this.uuid = uuid; return this; @@ -74,7 +62,6 @@ public class Analysis { } public Analysis build() { - checkNotNull(id, "id cannot be null"); checkNotNull(uuid, "uuid cannot be null"); checkNotNull(createdAt, "createdAt cannot be null"); return new Analysis(this); @@ -91,19 +78,18 @@ public class Analysis { } Analysis analysis = (Analysis) o; - return id == analysis.id; + return Objects.equals(uuid, analysis.uuid); } @Override public int hashCode() { - return (int) (id ^ (id >>> 32)); + return Objects.hash(uuid); } @Override public String toString() { return "Analysis{" + - "id=" + id + - ", uuid='" + uuid + '\'' + + "uuid='" + uuid + '\'' + ", createdAt=" + createdAt + '}'; } diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStep.java index d49e6df2d00..e26b7b82579 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStep.java @@ -143,7 +143,6 @@ public class BuildComponentTreeStep implements ComputationStep { private static Analysis toAnalysis(SnapshotDto dto) { return new Analysis.Builder() - .setId(dto.getId()) .setUuid(dto.getUuid()) .setCreatedAt(dto.getCreatedAt()) .build(); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisImplTest.java index a1c1ca5f67f..ae38500b568 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisImplTest.java @@ -37,34 +37,20 @@ public class AnalysisImplTest { @Test public void build_snapshot() { Analysis analysis = new Analysis.Builder() - .setId(ID) .setUuid(UUID) .setCreatedAt(CREATED_AT) .build(); - assertThat(analysis.getId()).isEqualTo(ID); assertThat(analysis.getUuid()).isEqualTo(UUID); assertThat(analysis.getCreatedAt()).isEqualTo(CREATED_AT); } - @Test - public void fail_with_NPE_when_building_snapshot_without_id() { - thrown.expect(NullPointerException.class); - thrown.expectMessage("id cannot be null"); - - new Analysis.Builder() - .setUuid(UUID) - .setCreatedAt(CREATED_AT) - .build(); - } - @Test public void fail_with_NPE_when_building_snapshot_without_uuid() { thrown.expect(NullPointerException.class); thrown.expectMessage("uuid cannot be null"); new Analysis.Builder() - .setId(ID) .setCreatedAt(CREATED_AT) .build(); } @@ -75,7 +61,6 @@ public class AnalysisImplTest { thrown.expectMessage("createdAt cannot be null"); new Analysis.Builder() - .setId(ID) .setUuid(UUID) .build(); } @@ -83,45 +68,34 @@ public class AnalysisImplTest { @Test public void test_toString() { assertThat(new Analysis.Builder() - .setId(ID) .setUuid(UUID) .setCreatedAt(CREATED_AT) .build().toString()) - .isEqualTo("Analysis{id=10, uuid='uuid ', createdAt=123456789}"); + .isEqualTo("Analysis{uuid='uuid ', createdAt=123456789}"); } @Test public void test_equals_and_hascode() { Analysis analysis = new Analysis.Builder() - .setId(ID) .setUuid(UUID) .setCreatedAt(CREATED_AT) .build(); Analysis sameAnalysis = new Analysis.Builder() - .setId(ID) .setUuid(UUID) .setCreatedAt(CREATED_AT) .build(); - Analysis sameAnalysisNotSameUuid = new Analysis.Builder() - .setId(ID) - .setUuid("other uuid") - .setCreatedAt(CREATED_AT) - .build(); Analysis otherAnalysis = new Analysis.Builder() - .setId(11L) - .setUuid(UUID) + .setUuid("other uuid") .setCreatedAt(CREATED_AT) .build(); assertThat(analysis).isEqualTo(analysis); assertThat(analysis).isEqualTo(sameAnalysis); - assertThat(analysis).isEqualTo(sameAnalysisNotSameUuid); assertThat(analysis).isNotEqualTo(otherAnalysis); assertThat(analysis).isNotEqualTo(null); assertThat(analysis.hashCode()).isEqualTo(analysis.hashCode()); assertThat(analysis.hashCode()).isEqualTo(sameAnalysis.hashCode()); - assertThat(analysis.hashCode()).isEqualTo(sameAnalysisNotSameUuid.hashCode()); assertThat(analysis.hashCode()).isNotEqualTo(otherAnalysis.hashCode()); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java index 19c10802184..b44c61bdc63 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java @@ -47,7 +47,6 @@ import static org.sonar.db.organization.OrganizationTesting.newOrganizationDto; public class AnalysisMetadataHolderImplTest { private static Analysis baseProjectAnalysis = new Analysis.Builder() - .setId(1) .setUuid("uuid_1") .setCreatedAt(123456789L) .build(); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java index 54173e19e6d..c50515ce4ce 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/filemove/FileMoveDetectionStepTest.java @@ -62,10 +62,9 @@ import static org.sonar.ce.task.projectanalysis.filemove.FileMoveDetectionStep.M public class FileMoveDetectionStepTest { - private static final long SNAPSHOT_ID = 98765; + private static final String SNAPSHOT_UUID = "uuid_1"; private static final Analysis ANALYSIS = new Analysis.Builder() - .setId(SNAPSHOT_ID) - .setUuid("uuid_1") + .setUuid(SNAPSHOT_UUID) .setCreatedAt(86521) .build(); private static final int ROOT_REF = 1; diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java index 800bd0203ae..269878f2bc9 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/scm/ScmInfoDbLoaderTest.java @@ -52,10 +52,8 @@ public class ScmInfoDbLoaderTest { static final int FILE_REF = 1; static final Component FILE = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").build(); static final long DATE_1 = 123456789L; - static final long DATE_2 = 1234567810L; static Analysis baseProjectAnalysis = new Analysis.Builder() - .setId(1) .setUuid("uuid_1") .setCreatedAt(123456789L) .build(); diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java index 2d40c145935..22edc104968 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadCrossProjectDuplicationsRepositoryStepTest.java @@ -54,10 +54,12 @@ import org.sonar.duplications.block.ByteArray; import org.sonar.scanner.protocol.output.ScannerReport; import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE; @@ -111,7 +113,6 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { dbSession.commit(); baseProjectAnalysis = new Analysis.Builder() - .setId(projectSnapshot.getId()) .setUuid(projectSnapshot.getUuid()) .setCreatedAt(projectSnapshot.getCreatedAt()) .build(); @@ -145,12 +146,12 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setStartTokenIndex(0) .setEndTokenIndex(10) .build(); - batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock)); + batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock)); underTest.execute(new TestComputationStepContext()); verify(integrateCrossProjectDuplications).computeCpd(CURRENT_FILE, - asList( + singletonList( new Block.Builder() .setResourceId(CURRENT_FILE_KEY) .setBlockHash(new ByteArray(hash)) @@ -158,7 +159,7 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setLines(originBlock.getStartLine(), originBlock.getEndLine()) .setUnit(originBlock.getStartTokenIndex(), originBlock.getEndTokenIndex()) .build()), - asList( + singletonList( new Block.Builder() .setResourceId(otherFile.getDbKey()) .setBlockHash(new ByteArray(hash)) @@ -283,7 +284,7 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setStartTokenIndex(0) .setEndTokenIndex(10) .build(); - batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock)); + batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock)); underTest.execute(new TestComputationStepContext()); @@ -299,7 +300,7 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { underTest.execute(new TestComputationStepContext()); - verifyZeroInteractions(integrateCrossProjectDuplications); + verifyNoInteractions(integrateCrossProjectDuplications); } @Test @@ -314,11 +315,11 @@ public class LoadCrossProjectDuplicationsRepositoryStepTest { .setStartTokenIndex(0) .setEndTokenIndex(10) .build(); - batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock)); + batchReportReader.putDuplicationBlocks(FILE_REF, singletonList(originBlock)); underTest.execute(new TestComputationStepContext()); - verifyZeroInteractions(integrateCrossProjectDuplications); + verifyNoInteractions(integrateCrossProjectDuplications); } private ComponentDto createProject(String projectKey) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java index ced97ee7fab..550b53ff0ce 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java @@ -36,7 +36,6 @@ public final class SnapshotDto { public static final int MAX_VERSION_LENGTH = 100; public static final int MAX_BUILD_STRING_LENGTH = 100; - private Long id; private String uuid; private String componentUuid; private Long createdAt; @@ -56,15 +55,6 @@ public final class SnapshotDto { @Nullable private String revision; - public Long getId() { - return id; - } - - public SnapshotDto setId(Long id) { - this.id = id; - return this; - } - public SnapshotDto setUuid(String s) { this.uuid = s; return this; @@ -215,8 +205,7 @@ public final class SnapshotDto { return false; } SnapshotDto that = (SnapshotDto) o; - return Objects.equals(id, that.id) && - Objects.equals(uuid, that.uuid) && + return Objects.equals(uuid, that.uuid) && Objects.equals(componentUuid, that.componentUuid) && Objects.equals(createdAt, that.createdAt) && Objects.equals(buildDate, that.buildDate) && @@ -231,14 +220,13 @@ public final class SnapshotDto { @Override public int hashCode() { - return Objects.hash(id, uuid, componentUuid, createdAt, buildDate, status, last, projectVersion, buildString, periodMode, periodParam, periodDate); + return Objects.hash(uuid, componentUuid, createdAt, buildDate, status, last, projectVersion, buildString, periodMode, periodParam, periodDate); } @Override public String toString() { return "SnapshotDto{" + - "id=" + id + - ", uuid='" + uuid + '\'' + + "uuid='" + uuid + '\'' + ", componentUuid='" + componentUuid + '\'' + ", createdAt=" + createdAt + ", buildDate=" + buildDate + diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java index 0e7f34871a4..b946ce48377 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeableAnalysisDto.java @@ -30,7 +30,6 @@ import org.apache.commons.lang.builder.ToStringStyle; */ public class PurgeableAnalysisDto implements Comparable { private Date date; - private long analysisId; private String analysisUuid; private String version; private boolean hasEvents; @@ -45,15 +44,6 @@ public class PurgeableAnalysisDto implements Comparable { return this; } - public long getAnalysisId() { - return analysisId; - } - - public PurgeableAnalysisDto setAnalysisId(long analysisId) { - this.analysisId = analysisId; - return this; - } - public String getAnalysisUuid() { return analysisUuid; } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml index 082040e4bed..aa53f2e4b82 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml @@ -3,7 +3,6 @@ - s.id, s.uuid as uuid, s.component_uuid as componentUuId, s.created_at as createdAt, @@ -157,7 +156,7 @@ where uuid = #{uuid,jdbcType=VARCHAR} - + insert into snapshots ( uuid, component_uuid, diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml index d7869650ff4..17453714d35 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml @@ -24,7 +24,6 @@