From 13f4b6670e24e6395138219f1a8e05873858997a Mon Sep 17 00:00:00 2001 From: Alain Kermis Date: Thu, 20 Jul 2023 16:03:24 +0200 Subject: [PATCH] SONAR-7698 Rename snapshots column build_date to analysis_date --- .../step/ReportPersistAnalysisStepIT.java | 2 +- .../step/ViewsPersistAnalysisStepIT.java | 2 +- .../analysis/ExportAnalysesStepIT.java | 4 +- .../step/PersistAnalysisStep.java | 2 +- .../analysis/ExportAnalysesStep.java | 2 +- .../src/main/protobuf/project_dump.proto | 2 +- .../AnalysisCacheCleaningSchedulerImplIT.java | 6 +- .../org/sonar/db/component/SnapshotDaoIT.java | 10 +-- .../ScannerAnalysisCacheDaoIT.java | 6 +- .../org/sonar/db/component/SnapshotDto.java | 16 ++-- .../org/sonar/db/component/SnapshotMapper.xml | 6 +- .../ScannerAnalysisCacheMapper.xml | 2 +- server/sonar-db-dao/src/schema/schema-sq.ddl | 2 +- .../sonar/db/component/SnapshotDtoTest.java | 74 ++++++++++++++++--- .../sonar/db/component/SnapshotTesting.java | 4 +- .../migration/version/v102/DbVersion102.java | 1 + .../v102/RenameBuildDateInSnapshots.java | 53 +++++++++++++ .../v102/RenameBuildDateInSnapshotsTest.java | 54 ++++++++++++++ .../RenameBuildDateInSnapshotsTest/schema.sql | 17 +++++ .../telemetry/TelemetryDataLoaderImpl.java | 2 +- .../TelemetryDataLoaderImplTest.java | 10 +-- 21 files changed, 228 insertions(+), 49 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshots.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest/schema.sql diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepIT.java index d6f66880716..f61e009d008 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepIT.java @@ -126,7 +126,7 @@ public class ReportPersistAnalysisStepIT extends BaseStepTest { assertThat(projectSnapshot.getLast()).isFalse(); assertThat(projectSnapshot.getStatus()).isEqualTo("U"); assertThat(projectSnapshot.getCreatedAt()).isEqualTo(analysisDate); - assertThat(projectSnapshot.getBuildDate()).isEqualTo(now); + assertThat(projectSnapshot.getAnalysisDate()).isEqualTo(now); assertThat(projectSnapshot.getRevision()).isEqualTo(REVISION_ID); } diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistAnalysisStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistAnalysisStepIT.java index 2b07a07158c..1849d0d3241 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistAnalysisStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistAnalysisStepIT.java @@ -111,7 +111,7 @@ public class ViewsPersistAnalysisStepIT extends BaseStepTest { assertThat(viewSnapshot.getLast()).isFalse(); assertThat(viewSnapshot.getStatus()).isEqualTo("U"); assertThat(viewSnapshot.getCreatedAt()).isEqualTo(analysisDate); - assertThat(viewSnapshot.getBuildDate()).isEqualTo(now); + assertThat(viewSnapshot.getAnalysisDate()).isEqualTo(now); } @Test diff --git a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java index 7cd38451189..4489e9dfbd7 100644 --- a/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java +++ b/server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java @@ -192,7 +192,7 @@ public class ExportAnalysesStepIT { .hasMessage("Analysis Export failed after processing 1 analyses successfully"); } - private static SnapshotDto newAnalysis(String uuid, long date, String componentUuid, @Nullable String version, boolean isLast, @Nullable String buildString, long buildDate) { + private static SnapshotDto newAnalysis(String uuid, long date, String componentUuid, @Nullable String version, boolean isLast, @Nullable String buildString, long analysisDate) { return new SnapshotDto() .setUuid(uuid) .setCreatedAt(date) @@ -201,7 +201,7 @@ public class ExportAnalysesStepIT { .setBuildString(buildString) .setLast(isLast) .setStatus(SnapshotDto.STATUS_PROCESSED) - .setBuildDate(buildDate); + .setAnalysisDate(analysisDate); } private static void assertAnalysis(ProjectDump.Analysis analysis, ComponentDto component, SnapshotDto dto) { diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisStep.java index 2b8e262b9ba..5ad72e71fd1 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisStep.java @@ -111,7 +111,7 @@ public class PersistAnalysisStep implements ComputationStep { .setLast(false) .setStatus(SnapshotDto.STATUS_UNPROCESSED) .setCreatedAt(analysisDate) - .setBuildDate(system2.now()); + .setAnalysisDate(system2.now()); if (component.getType() == PROJECT) { component.getProjectAttributes().getScmRevisionId().ifPresent(dto::setRevision); diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStep.java index 2db9737900c..4295787a3d2 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStep.java @@ -52,7 +52,7 @@ public class ExportAnalysesStep implements ComputationStep { " inner join components p on s.root_component_uuid=p.uuid" + " inner join project_branches pb on pb.uuid=p.uuid" + " where pb.project_uuid=? and pb.branch_type = 'BRANCH' and pb.exclude_from_purge=? and s.status=? and p.enabled=?" + - " order by s.build_date asc"; + " order by s.analysis_date asc"; private final DbClient dbClient; private final ProjectHolder projectHolder; diff --git a/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto b/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto index f495c6df4d0..1b73531a93e 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto +++ b/server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto @@ -49,7 +49,7 @@ message Branch { // Stream of analyses stored in file analyses.pb, including project. // Only analyses with status 'P' (processed) are exported. They are ordered -// by build_date, so that parents are always located before children. +// by analysis_date, so that parents are always located before children. message Analysis { int64 component_ref = 2; int64 date = 3; diff --git a/server/sonar-ce/src/it/java/org/sonar/ce/analysis/cache/cleaning/AnalysisCacheCleaningSchedulerImplIT.java b/server/sonar-ce/src/it/java/org/sonar/ce/analysis/cache/cleaning/AnalysisCacheCleaningSchedulerImplIT.java index fe6683c7753..a100566b60d 100644 --- a/server/sonar-ce/src/it/java/org/sonar/ce/analysis/cache/cleaning/AnalysisCacheCleaningSchedulerImplIT.java +++ b/server/sonar-ce/src/it/java/org/sonar/ce/analysis/cache/cleaning/AnalysisCacheCleaningSchedulerImplIT.java @@ -88,7 +88,7 @@ public class AnalysisCacheCleaningSchedulerImplIT { assertThat(scannerAnalysisCacheDao.selectData(dbSession, snapshot4.getRootComponentUuid())).isNull(); } - private static SnapshotDto createSnapshot(long buildtime) { + private static SnapshotDto createSnapshot(long analysisTime) { return new SnapshotDto() .setUuid(uuidFactory.create()) .setRootComponentUuid(uuidFactory.create()) @@ -97,8 +97,8 @@ public class AnalysisCacheCleaningSchedulerImplIT { .setProjectVersion("2.1-SNAPSHOT") .setPeriodMode("days1") .setPeriodParam("30") - .setPeriodDate(buildtime) - .setBuildDate(buildtime); + .setPeriodDate(analysisTime) + .setAnalysisDate(analysisTime); } } diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java index db10753d79c..fcdb5a86898 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java @@ -83,7 +83,7 @@ public class SnapshotDaoIT { .setPeriodDate(1500000000001L) .setProjectVersion("2.1.0") .setBuildString("2.1.0.2336") - .setBuildDate(1500000000006L) + .setAnalysisDate(1500000000006L) .setCreatedAt(1403042400000L) .setRevision("sha1")); @@ -97,7 +97,7 @@ public class SnapshotDaoIT { assertThat(result.getPeriodMode()).isEqualTo("days"); assertThat(result.getPeriodModeParameter()).isEqualTo("30"); assertThat(result.getPeriodDate()).isEqualTo(1500000000001L); - assertThat(result.getBuildDate()).isEqualTo(1500000000006L); + assertThat(result.getAnalysisDate()).isEqualTo(1500000000006L); assertThat(result.getCreatedAt()).isEqualTo(1403042400000L); assertThat(result.getRevision()).isEqualTo("sha1"); @@ -410,7 +410,7 @@ public class SnapshotDaoIT { .setPeriodParam("30") .setPeriodDate(1500000000001L) .setProjectVersion("2.1-SNAPSHOT") - .setBuildDate(1500000000006L) + .setAnalysisDate(1500000000006L) .setCreatedAt(1403042400000L)); assertThat(dto.getUuid()).isNotNull(); @@ -420,7 +420,7 @@ public class SnapshotDaoIT { assertThat(dto.getPeriodMode()).isEqualTo("days"); assertThat(dto.getPeriodModeParameter()).isEqualTo("30"); assertThat(dto.getPeriodDate()).isEqualTo(1500000000001L); - assertThat(dto.getBuildDate()).isEqualTo(1500000000006L); + assertThat(dto.getAnalysisDate()).isEqualTo(1500000000006L); assertThat(dto.getCreatedAt()).isEqualTo(1403042400000L); assertThat(dto.getProjectVersion()).isEqualTo("2.1-SNAPSHOT"); } @@ -558,7 +558,7 @@ public class SnapshotDaoIT { .setPeriodMode("days1") .setPeriodParam("30") .setPeriodDate(1_500_000_000_001L) - .setBuildDate(1_500_000_000_006L); + .setAnalysisDate(1_500_000_000_006L); } private CeActivityDto insertActivity(String projectUuid, SnapshotDto analysis, CeActivityDto.Status status) { diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/scannercache/ScannerAnalysisCacheDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/scannercache/ScannerAnalysisCacheDaoIT.java index 2dab8a159e0..298e285a51d 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/scannercache/ScannerAnalysisCacheDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/scannercache/ScannerAnalysisCacheDaoIT.java @@ -130,7 +130,7 @@ public class ScannerAnalysisCacheDaoIT { return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)); } - private static SnapshotDto createSnapshot(long buildtime) { + private static SnapshotDto createSnapshot(long analysisTime) { return new SnapshotDto() .setUuid(uuidFactory.create()) .setRootComponentUuid(uuidFactory.create()) @@ -139,8 +139,8 @@ public class ScannerAnalysisCacheDaoIT { .setProjectVersion("2.1-SNAPSHOT") .setPeriodMode("days1") .setPeriodParam("30") - .setPeriodDate(buildtime) - .setBuildDate(buildtime); + .setPeriodDate(analysisTime) + .setAnalysisDate(analysisTime); } } 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 b0f2b37e28b..483a91704c8 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 @@ -40,7 +40,7 @@ public final class SnapshotDto { private String uuid; private String rootComponentUuid; private Long createdAt; - private Long buildDate; + private Long analysisDate; private String status = STATUS_UNPROCESSED; private Boolean last; // maps to "version" column in the table @@ -65,12 +65,12 @@ public final class SnapshotDto { return this.uuid; } - public Long getBuildDate() { - return buildDate; + public Long getAnalysisDate() { + return analysisDate; } - public SnapshotDto setBuildDate(Long buildDate) { - this.buildDate = buildDate; + public SnapshotDto setAnalysisDate(Long analysisDate) { + this.analysisDate = analysisDate; return this; } @@ -209,7 +209,7 @@ public final class SnapshotDto { return Objects.equals(uuid, that.uuid) && Objects.equals(rootComponentUuid, that.rootComponentUuid) && Objects.equals(createdAt, that.createdAt) && - Objects.equals(buildDate, that.buildDate) && + Objects.equals(analysisDate, that.analysisDate) && Objects.equals(status, that.status) && Objects.equals(last, that.last) && Objects.equals(projectVersion, that.projectVersion) && @@ -221,7 +221,7 @@ public final class SnapshotDto { @Override public int hashCode() { - return Objects.hash(uuid, rootComponentUuid, createdAt, buildDate, status, last, projectVersion, buildString, periodMode, periodParam, periodDate); + return Objects.hash(uuid, rootComponentUuid, createdAt, analysisDate, status, last, projectVersion, buildString, periodMode, periodParam, periodDate); } @Override @@ -230,7 +230,7 @@ public final class SnapshotDto { "uuid='" + uuid + '\'' + ", componentUuid='" + rootComponentUuid + '\'' + ", createdAt=" + createdAt + - ", buildDate=" + buildDate + + ", analysisDate=" + analysisDate + ", status='" + status + '\'' + ", last=" + last + ", projectVersion='" + projectVersion + '\'' + 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 4ebbc769fff..259d9da533a 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 @@ -6,7 +6,7 @@ s.uuid as uuid, s.root_component_uuid as rootComponentUuid, s.created_at as createdAt, - s.build_date as buildDate, + s.analysis_date as analysisDate, s.status as status, s.islast as last, s.version as rawProjectVersion, @@ -207,7 +207,7 @@ uuid, root_component_uuid, created_at, - build_date, + analysis_date, status, islast, version, @@ -222,7 +222,7 @@ #{uuid, jdbcType=VARCHAR}, #{rootComponentUuid, jdbcType=VARCHAR}, #{createdAt, jdbcType=BIGINT}, - #{buildDate, jdbcType=BIGINT}, + #{analysisDate, jdbcType=BIGINT}, #{status, jdbcType=VARCHAR}, #{last, jdbcType=BOOLEAN}, #{projectVersion, jdbcType=VARCHAR}, diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerAnalysisCacheMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerAnalysisCacheMapper.xml index 2b641ed9b41..6fb3ff0c942 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerAnalysisCacheMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerAnalysisCacheMapper.xml @@ -18,7 +18,7 @@ left outer join snapshots s on sac.branch_uuid = s.root_component_uuid where - s.build_date < #{timestamp,jdbcType=BIGINT} and s.islast=${_true} + s.analysis_date < #{timestamp,jdbcType=BIGINT} and s.islast=${_true} or s.islast is null ) diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 46f7cdb381d..b043cd08d28 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -969,7 +969,7 @@ CREATE TABLE "SNAPSHOTS"( "VERSION" CHARACTER VARYING(500), "BUILD_STRING" CHARACTER VARYING(100), "REVISION" CHARACTER VARYING(100), - "BUILD_DATE" BIGINT, + "ANALYSIS_DATE" BIGINT, "PERIOD1_MODE" CHARACTER VARYING(100), "PERIOD1_PARAM" CHARACTER VARYING(100), "PERIOD1_DATE" BIGINT, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java index b9d2aaec441..2c6a71b8a60 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java @@ -31,17 +31,9 @@ public class SnapshotDtoTest { @Test public void test_getter_and_setter() { - SnapshotDto snapshotDto = new SnapshotDto() - .setBuildDate(parseDate("2014-07-02").getTime()) - .setRootComponentUuid("uuid_21") - .setLast(true) - .setProjectVersion("1.0") - .setBuildString("1.0.1.123") - .setPeriodMode("mode1") - .setPeriodParam("param1") - .setPeriodDate(parseDate("2014-06-01").getTime()); + SnapshotDto snapshotDto = create(); - assertThat(snapshotDto.getBuildDate()).isEqualTo(parseDate("2014-07-02").getTime()); + assertThat(snapshotDto.getAnalysisDate()).isEqualTo(parseDate("2014-07-02").getTime()); assertThat(snapshotDto.getRootComponentUuid()).isEqualTo("uuid_21"); assertThat(snapshotDto.getLast()).isTrue(); assertThat(snapshotDto.getProjectVersion()).isEqualTo("1.0"); @@ -78,4 +70,66 @@ public class SnapshotDtoTest { " length (101) is longer than the maximum authorized (100). " + "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); } + + @Test + public void equals_whenSameObject_shouldReturnTrue() { + SnapshotDto snapshotDto = create(); + assertThat(snapshotDto.equals(snapshotDto)).isTrue(); + } + + @Test + public void equals_whenComparedToNull_shouldReturnFalse() { + SnapshotDto snapshotDto = create(); + assertThat(snapshotDto.equals(null)).isFalse(); + } + + @Test + public void equals_whenComparedToDifferentClass_shouldReturnFalse() { + SnapshotDto snapshotDto = create(); + Object differentObject = new Object(); + assertThat(snapshotDto.equals(differentObject)).isFalse(); + } + + @Test + public void equals_whenComparedToDifferentInstanceWithSameValues_shouldReturnTrue() { + SnapshotDto snapshotDto1 = create(); + SnapshotDto snapshotDto2 = create(); + assertThat(snapshotDto1.equals(snapshotDto2)).isTrue(); + assertThat(snapshotDto2.equals(snapshotDto1)).isTrue(); + } + + @Test + public void equals_whenComparedToDifferentInstanceWithDifferentValues_shouldReturnFalse() { + SnapshotDto snapshotDto1 = create(); + SnapshotDto snapshotDto2 = create().setBuildString("some-other-string"); + assertThat(snapshotDto1.equals(snapshotDto2)).isFalse(); + assertThat(snapshotDto2.equals(snapshotDto1)).isFalse(); + } + + @Test + public void hashcode_whenDifferentInstanceWithSameValues_shouldBeEqual() { + SnapshotDto snapshotDto1 = create(); + SnapshotDto snapshotDto2 = create(); + assertThat(snapshotDto1).hasSameHashCodeAs(snapshotDto2); + } + + @Test + public void hashcode_whenDifferentInstanceWithDifferentValues_shouldNotBeEqual() { + SnapshotDto snapshotDto1 = create(); + SnapshotDto snapshotDto2 = create().setBuildString("some-other-string"); + assertThat(snapshotDto1).doesNotHaveSameHashCodeAs(snapshotDto2); + } + + private SnapshotDto create() { + return new SnapshotDto() + .setAnalysisDate(parseDate("2014-07-02").getTime()) + .setRootComponentUuid("uuid_21") + .setLast(true) + .setProjectVersion("1.0") + .setBuildString("1.0.1.123") + .setPeriodMode("mode1") + .setPeriodParam("param1") + .setPeriodDate(parseDate("2014-06-01").getTime()); + } + } diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/SnapshotTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/SnapshotTesting.java index 7deed40f20f..4774ed2184d 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/SnapshotTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/SnapshotTesting.java @@ -47,7 +47,7 @@ public class SnapshotTesting { .setRootComponentUuid(uuid) .setStatus(SnapshotDto.STATUS_PROCESSED) .setCreatedAt(System.currentTimeMillis()) - .setBuildDate(System.currentTimeMillis()) + .setAnalysisDate(System.currentTimeMillis()) .setRevision(randomAlphanumeric(50)) .setLast(true); } @@ -58,7 +58,7 @@ public class SnapshotTesting { .setRootComponentUuid(randomAlphanumeric(40)) .setStatus(randomAscii(1)) .setCreatedAt(System.currentTimeMillis()) - .setBuildDate(System.currentTimeMillis()) + .setAnalysisDate(System.currentTimeMillis()) .setLast(true); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java index cd6bfeb4d7e..a8ba7cb1915 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java @@ -75,6 +75,7 @@ public class DbVersion102 implements DbVersion { .add(10_2_022, "Populate 'purged' column in 'snapshots' table", PopulatePurgedColumnInSnapshots.class) .add(10_2_023, "Make 'purged' column not nullable in 'snapshots' table", MakePurgedColumnNotNullableInSnapshots.class) .add(10_2_024, "Drop 'purge_status' column in 'snapshots' table", DropPurgeStatusColumnInSnapshots.class) + .add(10_2_025, "Rename 'build_date' in 'snapshots' table to 'analysis_date", RenameBuildDateInSnapshots.class) ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshots.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshots.java new file mode 100644 index 00000000000..f2ca83a832b --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshots.java @@ -0,0 +1,53 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.version.v102; + +import java.sql.Connection; +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.db.DatabaseUtils; +import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef; +import org.sonar.server.platform.db.migration.sql.RenameColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class RenameBuildDateInSnapshots extends DdlChange { + private static final String TABLE_NAME = "snapshots"; + + private static final BigIntegerColumnDef columnDefinition = BigIntegerColumnDef.newBigIntegerColumnDefBuilder() + .setColumnName("analysis_date") + .setIsNullable(true) + .build(); + + public RenameBuildDateInSnapshots(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection()) { + if (!DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "analysis_date") + && DatabaseUtils.tableColumnExists(connection, TABLE_NAME, "build_date")) { + context.execute(new RenameColumnsBuilder(getDialect(), TABLE_NAME) + .renameColumn("build_date", columnDefinition) + .build()); + } + } + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest.java new file mode 100644 index 00000000000..62761472ea4 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest.java @@ -0,0 +1,54 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.version.v102; + +import java.sql.SQLException; +import java.sql.Types; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +public class RenameBuildDateInSnapshotsTest { + private static final String TABLE_NAME = "snapshots"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(RenameBuildDateInSnapshotsTest.class, "schema.sql"); + + private final RenameBuildDateInSnapshots underTest = new RenameBuildDateInSnapshots(db.database()); + + @Test + public void execute_whenExecuted_shouldRenameColumn() throws SQLException { + assertColumnExists("build_date"); + underTest.execute(); + assertColumnExists("analysis_date"); + } + + @Test + public void execute_whenExecutedTwice_shouldNotFail() throws SQLException { + assertColumnExists("build_date"); + underTest.execute(); + underTest.execute(); + assertColumnExists("analysis_date"); + } + + private void assertColumnExists(String columnName) { + db.assertColumnDefinition(TABLE_NAME, columnName, Types.BIGINT, null, true); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest/schema.sql new file mode 100644 index 00000000000..404ebcdf7ec --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest/schema.sql @@ -0,0 +1,17 @@ +CREATE TABLE "SNAPSHOTS"( + "UUID" CHARACTER VARYING(50) NOT NULL, + "COMPONENT_UUID" CHARACTER VARYING(50) NOT NULL, + "STATUS" CHARACTER VARYING(4) DEFAULT 'U' NOT NULL, + "ISLAST" BOOLEAN DEFAULT FALSE NOT NULL, + "VERSION" CHARACTER VARYING(500), + "PURGE_STATUS" INTEGER, + "BUILD_STRING" CHARACTER VARYING(100), + "REVISION" CHARACTER VARYING(100), + "BUILD_DATE" BIGINT, + "PERIOD1_MODE" CHARACTER VARYING(100), + "PERIOD1_PARAM" CHARACTER VARYING(100), + "PERIOD1_DATE" BIGINT, + "CREATED_AT" BIGINT +); +ALTER TABLE "SNAPSHOTS" ADD CONSTRAINT "PK_SNAPSHOTS" PRIMARY KEY("UUID"); +CREATE INDEX "SNAPSHOT_COMPONENT" ON "SNAPSHOTS"("COMPONENT_UUID" NULLS FIRST); \ No newline at end of file diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java index 969e44fadc9..b8b1fdeabab 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java @@ -316,7 +316,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { List branchUuids = branchesWithLargestNcloc.stream().map(ProjectLocDistributionDto::branchUuid).toList(); Map latestSnapshotMap = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, branchUuids) .stream() - .collect(toMap(SnapshotDto::getRootComponentUuid, SnapshotDto::getBuildDate)); + .collect(toMap(SnapshotDto::getRootComponentUuid, SnapshotDto::getAnalysisDate)); data.setProjects(buildProjectsList(branchesWithLargestNcloc, latestSnapshotMap)); } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java index 8e6040a6ce1..0bcc269bd82 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java @@ -185,8 +185,8 @@ public class TelemetryDataLoaderImplTest { db.measures().insertLiveMeasure(mainBranch2, coverage, m -> m.setValue(80d)); db.measures().insertLiveMeasure(mainBranch2, nclocDistrib, m -> m.setValue(null).setData("java=180;js=20")); - SnapshotDto project1Analysis = db.components().insertSnapshot(mainBranch1, t -> t.setLast(true).setBuildDate(analysisDate)); - SnapshotDto project2Analysis = db.components().insertSnapshot(mainBranch2, t -> t.setLast(true).setBuildDate(analysisDate)); + SnapshotDto project1Analysis = db.components().insertSnapshot(mainBranch1, t -> t.setLast(true).setAnalysisDate(analysisDate)); + SnapshotDto project2Analysis = db.components().insertSnapshot(mainBranch2, t -> t.setLast(true).setAnalysisDate(analysisDate)); db.measures().insertMeasure(mainBranch1, project1Analysis, nclocDistrib, m -> m.setData("java=70;js=30;kotlin=10")); db.measures().insertMeasure(mainBranch2, project2Analysis, nclocDistrib, m -> m.setData("java=180;js=20")); @@ -294,9 +294,9 @@ public class TelemetryDataLoaderImplTest { ProjectData projectData2 = db.components().insertPrivateProject(); ComponentDto mainBranch2 = projectData2.getMainBranchComponent(); - SnapshotDto project1Analysis1 = db.components().insertSnapshot(mainBranch1, t -> t.setLast(true).setBuildDate(analysisDate)); - SnapshotDto project1Analysis2 = db.components().insertSnapshot(mainBranch1, t -> t.setLast(true).setBuildDate(analysisDate)); - SnapshotDto project2Analysis = db.components().insertSnapshot(mainBranch2, t -> t.setLast(true).setBuildDate(analysisDate)); + SnapshotDto project1Analysis1 = db.components().insertSnapshot(mainBranch1, t -> t.setLast(true).setAnalysisDate(analysisDate)); + SnapshotDto project1Analysis2 = db.components().insertSnapshot(mainBranch1, t -> t.setLast(true).setAnalysisDate(analysisDate)); + SnapshotDto project2Analysis = db.components().insertSnapshot(mainBranch2, t -> t.setLast(true).setAnalysisDate(analysisDate)); db.measures().insertMeasure(mainBranch1, project1Analysis1, qg, pm -> pm.setData("OK")); db.measures().insertMeasure(mainBranch1, project1Analysis2, qg, pm -> pm.setData("ERROR")); db.measures().insertMeasure(mainBranch2, project2Analysis, qg, pm -> pm.setData("ERROR")); -- 2.39.5