]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7698 Rename snapshots column build_date to analysis_date
authorAlain Kermis <alain.kermis@sonarsource.com>
Thu, 20 Jul 2023 14:03:24 +0000 (16:03 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 21 Jul 2023 20:03:17 +0000 (20:03 +0000)
21 files changed:
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ReportPersistAnalysisStepIT.java
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectanalysis/step/ViewsPersistAnalysisStepIT.java
server/sonar-ce-task-projectanalysis/src/it/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStepIT.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisStep.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/analysis/ExportAnalysesStep.java
server/sonar-ce-task-projectanalysis/src/main/protobuf/project_dump.proto
server/sonar-ce/src/it/java/org/sonar/ce/analysis/cache/cleaning/AnalysisCacheCleaningSchedulerImplIT.java
server/sonar-db-dao/src/it/java/org/sonar/db/component/SnapshotDaoIT.java
server/sonar-db-dao/src/it/java/org/sonar/db/scannercache/ScannerAnalysisCacheDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/component/SnapshotMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/scannercache/ScannerAnalysisCacheMapper.xml
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/SnapshotTesting.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/DbVersion102.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshots.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v102/RenameBuildDateInSnapshotsTest/schema.sql [new file with mode: 0644]
server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java

index d6f668807166d1765a06863ae3f32623f7297ef4..f61e009d00864b6633f14205c7caaa7c8d267ca5 100644 (file)
@@ -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);
   }
 
index 2b07a07158c39293ca6dc2ac405a016fbda454da..1849d0d32418dde8c4d81068b9c65f01edab7b1e 100644 (file)
@@ -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
index 7cd38451189dad6341f8261d9d14bc5bfa86af1e..4489e9dfbd796bfaf6b83aa2b0a6cb6013c68780 100644 (file)
@@ -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) {
index 2b8e262b9baec4606e3ca57988b3aeaa170656ce..5ad72e71fd1edb8ca7bbebb4792eae7ca25aefe7 100644 (file)
@@ -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);
index 2db9737900c4467d0d202e90c30297edd899b6c8..4295787a3d2e792f197a74a91acc6d48d9294e7c 100644 (file)
@@ -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;
index f495c6df4d05a4d4233576d07cefbe3cbe650d42..1b73531a93e8a8719cc85db0ba1df311135470f3 100644 (file)
@@ -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;
index fe6683c7753e57d2dd19616fd8b4894f6921d64e..a100566b60deb1a8e556ae19dd1cbbd3b680ef62 100644 (file)
@@ -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);
   }
 
 }
index db10753d79cac12e957240437f9544456f9a496a..fcdb5a868981341151ac67d9e0f4218ff9606b78 100644 (file)
@@ -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) {
index 2dab8a159e095b1b27fd9e240a4ba5d3b1b30723..298e285a51d5af1e459eeb886e0b166cc9091d8e 100644 (file)
@@ -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);
   }
 
 }
index b0f2b37e28b0f5a4585d00b7e6227b5bcd9ccb42..483a91704c862c0b182bd598497b04f28900ad89 100644 (file)
@@ -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 + '\'' +
index 4ebbc769fff393033c475c97bd83eacdf81bb8e1..259d9da533abc3e15b7209b891fd846dbd5cbf15 100644 (file)
@@ -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,
       uuid,
       root_component_uuid,
       created_at,
-      build_date,
+      analysis_date,
       status,
       islast,
       version,
       #{uuid, jdbcType=VARCHAR},
       #{rootComponentUuid, jdbcType=VARCHAR},
       #{createdAt, jdbcType=BIGINT},
-      #{buildDate, jdbcType=BIGINT},
+      #{analysisDate, jdbcType=BIGINT},
       #{status, jdbcType=VARCHAR},
       #{last, jdbcType=BOOLEAN},
       #{projectVersion, jdbcType=VARCHAR},
index 2b641ed9b41a77ad513bbcd318bda40f0dcd8177..6fb3ff0c942119c50da816dae6201c402fe3e073 100644 (file)
@@ -18,7 +18,7 @@
       left outer join snapshots s on
         sac.branch_uuid = s.root_component_uuid
       where
-        s.build_date &lt; #{timestamp,jdbcType=BIGINT} and s.islast=${_true}
+        s.analysis_date &lt; #{timestamp,jdbcType=BIGINT} and s.islast=${_true}
         or s.islast is null
     )
   </delete>
index 46f7cdb381d315762f1f6a5fe47204036082d372..b043cd08d28f0cc525c5a4a74c6346667a122dc0 100644 (file)
@@ -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,
index b9d2aaec441598fa76162d611a5c752902ca564d..2c6a71b8a60afe51000173c44b7a57c5dac067e5 100644 (file)
@@ -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());
+  }
+
 }
index 7deed40f20fb61f11a0ba1260db3892f1b0ab7fc..4774ed2184d83a1dc7204d8ddd0e169d1f18377a 100644 (file)
@@ -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);
   }
 }
index cd6bfeb4d7eed80519c948199e66c86475640abd..a8ba7cb1915064959dbe362773c49ac45ad52c7f 100644 (file)
@@ -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 (file)
index 0000000..f2ca83a
--- /dev/null
@@ -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 (file)
index 0000000..6276147
--- /dev/null
@@ -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 (file)
index 0000000..404ebcd
--- /dev/null
@@ -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
index 969e44fadc917429b89c343bb9e984c3804a0ed5..b8b1fdeabab9c038f65810d6c085522a4eb06519 100644 (file)
@@ -316,7 +316,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
     List<String> branchUuids = branchesWithLargestNcloc.stream().map(ProjectLocDistributionDto::branchUuid).toList();
     Map<String, Long> latestSnapshotMap = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, branchUuids)
       .stream()
-      .collect(toMap(SnapshotDto::getRootComponentUuid, SnapshotDto::getBuildDate));
+      .collect(toMap(SnapshotDto::getRootComponentUuid, SnapshotDto::getAnalysisDate));
     data.setProjects(buildProjectsList(branchesWithLargestNcloc, latestSnapshotMap));
   }
 
index 8e6040a6ce106f09044070238cdbc30ac04d5ca2..0bcc269bd82377db0e4f18711c6901410297bb4a 100644 (file)
@@ -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"));