diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-08-18 18:27:47 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-08-22 18:13:43 +0200 |
commit | e0c834ebf18b722009ae5940e98b5916c8bbea71 (patch) | |
tree | 5d7d7a92a918f00a0fc872137f97c1b9ebe9542c | |
parent | 54e5d34f816386987a5ccd0634e311d692a64588 (diff) | |
download | sonarqube-e0c834ebf18b722009ae5940e98b5916c8bbea71.tar.gz sonarqube-e0c834ebf18b722009ae5940e98b5916c8bbea71.zip |
SONAR-6846 support duplicate snapshots with islast=true
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/version/v50/FeedFileSources.java | 21 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java | 26 |
2 files changed, 38 insertions, 9 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/version/v50/FeedFileSources.java b/sonar-db/src/main/java/org/sonar/db/version/v50/FeedFileSources.java index bf45c9ee424..89a44faa4bb 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/v50/FeedFileSources.java +++ b/sonar-db/src/main/java/org/sonar/db/version/v50/FeedFileSources.java @@ -22,6 +22,7 @@ package org.sonar.db.version.v50; import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.util.Date; +import java.util.Objects; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.System2; @@ -96,10 +97,7 @@ public class FeedFileSources extends BaseDataChange { // duplication_data "m13.text_value, " + - "m13.measure_data, " + - - // to detect multiple rows in snapshot_sources for the same snapshot - "s.id " + + "m13.measure_data " + "FROM snapshots s " + "JOIN snapshot_sources ss " + @@ -141,7 +139,13 @@ public class FeedFileSources extends BaseDataChange { "AND f.scope = 'FIL' " + "AND p.scope = 'PRJ' AND p.qualifier = 'TRK' " + "AND fs.file_uuid IS NULL " + - "ORDER BY s.id, ss.id desc"; + "ORDER BY " + + // to be able to detect duplicate a given file uuid only once + "f.uuid, " + + // to take into account the (presumably) most recent snapshot in case of duplicate + "s.id desc," + + // to take into account the (presumably) most recent line in FILE_SOURCES in case of duplicate + "ss.id desc"; public FeedFileSources(Database db, System2 system) { super(db); @@ -150,7 +154,7 @@ public class FeedFileSources extends BaseDataChange { private static final class FileSourceBuilder implements MassUpdate.Handler { private final long now; - private long previousSnapshotId = -1; + private String previousFileUuid = null; public FileSourceBuilder(System2 system) { now = system.now(); @@ -188,12 +192,11 @@ public class FeedFileSources extends BaseDataChange { byte[] longOverallCovCond = row.getNullableBytes(28); byte[] shortDuplicationData = row.getNullableBytes(29); byte[] longDuplicationData = row.getNullableBytes(30); - long snapshotId = row.getLong(31); - if (snapshotId == previousSnapshotId) { + if (Objects.equals(fileUuid, previousFileUuid)) { return false; } - this.previousSnapshotId = snapshotId; + this.previousFileUuid = fileUuid; String[] sourceData = new FileSourceDto(source, ofNullableBytes(shortRevisions, longRevisions), diff --git a/sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java b/sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java index 371b45f8fa0..e5851d6e4df 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/v50/FeedFileSourcesTest.java @@ -89,6 +89,32 @@ public class FeedFileSourcesTest { } @Test + public void migrate_ignores_duplicate_multiple_rows_in_snapshots_with_islast_true() throws Exception { + db.prepareDbUnit(getClass(), "before.xml"); + db.executeUpdateSql("insert into snapshots " + + "(id,project_id,parent_snapshot_id,root_project_id,root_snapshot_id,status,islast) " + + "values " + + "(9,3,4,1,2,'P',true)"); + db.executeUpdateSql("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(6, 'class Foo {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')"); + + db.executeUpdateSql("insert into snapshot_sources " + + "(snapshot_id, data, updated_at) " + + "values " + + "(9, 'class Bar {\r\n // Empty\r\n}\r\n', '2014-10-31 16:44:02.000')"); + + migration.execute(); + + List<Map<String, Object>> results = getFileSources(); + assertThat(results).hasSize(2); + + // the lastest inserted row is the one taken into account + assertThat(results.get(1).get("data")).isEqualTo(",,,,,,,,,,,,,,,class Bar {\r\n,,,,,,,,,,,,,,, // Empty\r\n,,,,,,,,,,,,,,,}\r\n,,,,,,,,,,,,,,,\r\n"); + } + + @Test public void migrate_sources_with_no_scm_no_coverage() throws Exception { db.prepareDbUnit(getClass(), "before.xml"); |