]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22870 Log the item UUID on error during the measures migration
authorClaire Villard <claire.villard@sonarsource.com>
Fri, 11 Oct 2024 13:53:24 +0000 (15:53 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 11 Oct 2024 20:02:43 +0000 (20:02 +0000)
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java
server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java

index 358a6ace30c47af9889c9795ccbea3d3d2c143ca..27d896ff796eaeab1922463ecd77eec5d1ef9de0 100644 (file)
@@ -26,6 +26,8 @@ import java.util.List;
 import java.util.Map;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
+import org.slf4j.event.Level;
+import org.sonar.api.testfixtures.log.LogTesterJUnit5;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.SequenceUuidFactory;
 import org.sonar.db.MigrationDbTester;
@@ -33,6 +35,7 @@ import org.sonar.server.platform.db.migration.step.DataChange;
 
 import static java.lang.String.format;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 import static org.assertj.core.api.Assertions.tuple;
 import static org.mockito.Mockito.mock;
 
@@ -45,6 +48,9 @@ class MigrateBranchesLiveMeasuresToMeasuresIT {
   @RegisterExtension
   public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(MigrateBranchesLiveMeasuresToMeasures.class);
 
+  @RegisterExtension
+  private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
+
   private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory();
   private final System2 system2 = mock();
   private final DataChange underTest = new MigrateBranchesLiveMeasuresToMeasures(db.database(), system2);
@@ -56,6 +62,23 @@ class MigrateBranchesLiveMeasuresToMeasuresIT {
     assertThat(db.countRowsOfTable("measures")).isZero();
   }
 
+  @Test
+  void log_the_item_uuid_when_the_migration_fails() {
+    String nclocMetricUuid = insertMetric("ncloc", "INT");
+    String branch1 = "branch_1";
+    insertNotMigratedBranch(branch1);
+    insertMeasure(branch1, nclocMetricUuid, Map.of("value", 120));
+
+    db.executeDdl("drop table measures");
+    db.assertTableDoesNotExist("measures");
+
+    assertThatExceptionOfType(SQLException.class)
+      .isThrownBy(underTest::execute);
+
+    assertThat(logTester.logs(Level.ERROR))
+      .contains("Migration of branch branch_1 failed");
+  }
+
   @Test
   void shall_not_migrate_when_branch_is_already_flagged() throws SQLException {
     String nclocMetricUuid = insertMetric("ncloc", "INT");
index f0b87843f20a481d19164da39392d050ed674d4c..38d30950f09f5044cec604b3060b7a5d8540f435 100644 (file)
@@ -26,6 +26,8 @@ import java.util.List;
 import java.util.Map;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
+import org.slf4j.event.Level;
+import org.sonar.api.testfixtures.log.LogTesterJUnit5;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.SequenceUuidFactory;
 import org.sonar.db.MigrationDbTester;
@@ -33,6 +35,7 @@ import org.sonar.server.platform.db.migration.step.DataChange;
 
 import static java.lang.String.format;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 import static org.assertj.core.api.Assertions.tuple;
 import static org.mockito.Mockito.mock;
 
@@ -45,6 +48,9 @@ class MigratePortfoliosLiveMeasuresToMeasuresIT {
   @RegisterExtension
   public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(MigratePortfoliosLiveMeasuresToMeasures.class);
 
+  @RegisterExtension
+  private final LogTesterJUnit5 logTester = new LogTesterJUnit5();
+
   private final SequenceUuidFactory uuidFactory = new SequenceUuidFactory();
   private final System2 system2 = mock();
   private final DataChange underTest = new MigratePortfoliosLiveMeasuresToMeasures(db.database(), system2);
@@ -56,6 +62,23 @@ class MigratePortfoliosLiveMeasuresToMeasuresIT {
     assertThat(db.countRowsOfTable("measures")).isZero();
   }
 
+  @Test
+  void log_the_item_uuid_when_the_migration_fails() {
+    String nclocMetricUuid = insertMetric("ncloc", "INT");
+    String portfolio = "portfolio_1";
+    insertNotMigratedPortfolio(portfolio);
+    insertMeasure(portfolio, nclocMetricUuid, Map.of("value", 120));
+
+    db.executeDdl("drop table measures");
+    db.assertTableDoesNotExist("measures");
+
+    assertThatExceptionOfType(SQLException.class)
+      .isThrownBy(underTest::execute);
+
+    assertThat(logTester.logs(Level.ERROR))
+      .contains("Migration of portfolio portfolio_1 failed");
+  }
+
   @Test
   void shall_not_migrate_when_portfolio_is_already_flagged() throws SQLException {
     String nclocMetricUuid = insertMetric("ncloc", "INT");
index 1c79d9172949467145a8ecb08f56f2bdec6e4fb7..2782a74699b0e2fdd8d274bf70ce5664d77fe910 100644 (file)
@@ -111,7 +111,12 @@ public abstract class AbstractMigrateLiveMeasuresToMeasures extends DataChange {
     int migrated = 0;
 
     for (String uuid : uuids) {
-      migrateItem(uuid, context);
+      try {
+        migrateItem(uuid, context);
+      } catch (Exception e) {
+        LOGGER.error(format("Migration of %s %s failed", item, uuid));
+        throw e;
+      }
 
       migrated++;
       if (migrated % 100 == 0) {