aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-migration
diff options
context:
space:
mode:
authorClaire Villard <claire.villard@sonarsource.com>2024-10-11 15:53:24 +0200
committersonartech <sonartech@sonarsource.com>2024-10-11 20:02:43 +0000
commitc289b869b8914d3833cf19fd129de11d53782d93 (patch)
tree317d72f12c32990ef1e22d2acaf14138c56c6044 /server/sonar-db-migration
parentd5a774c80cbdd6d18fe5192f14f7efaafe6c6e88 (diff)
downloadsonarqube-c289b869b8914d3833cf19fd129de11d53782d93.tar.gz
sonarqube-c289b869b8914d3833cf19fd129de11d53782d93.zip
SONAR-22870 Log the item UUID on error during the measures migration
Diffstat (limited to 'server/sonar-db-migration')
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigrateBranchesLiveMeasuresToMeasuresIT.java23
-rw-r--r--server/sonar-db-migration/src/it/java/org/sonar/server/platform/db/migration/version/v108/MigratePortfoliosLiveMeasuresToMeasuresIT.java23
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v108/AbstractMigrateLiveMeasuresToMeasures.java7
3 files changed, 52 insertions, 1 deletions
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);
@@ -57,6 +63,23 @@ class MigrateBranchesLiveMeasuresToMeasuresIT {
}
@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");
String qgStatusMetricUuid = insertMetric("quality_gate_status", "STRING");
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);
@@ -57,6 +63,23 @@ class MigratePortfoliosLiveMeasuresToMeasuresIT {
}
@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");
String qgStatusMetricUuid = insertMetric("quality_gate_status", "STRING");
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) {