From: Claire Villard Date: Fri, 11 Oct 2024 13:53:24 +0000 (+0200) Subject: SONAR-22870 Log the item UUID on error during the measures migration X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c289b869b8914d3833cf19fd129de11d53782d93;p=sonarqube.git SONAR-22870 Log the item UUID on error during the measures migration --- diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java index 358a6ace30c..27d896ff796 100644 --- a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java @@ -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"); diff --git a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java index f0b87843f20..38d30950f09 100644 --- a/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java +++ b/server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java @@ -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"); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java index 1c79d917294..2782a74699b 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java @@ -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) {