aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-01-30 11:12:12 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2015-01-30 16:48:32 +0100
commit8d9f68e8ba9fc37de36d8ed98312ecb4c9aa6147 (patch)
tree393f52eedbb22b802951800e1788f2f9585ed282 /sonar-batch
parentcda0bf9eef6b2cf978afb438d7d9142e6cf04db4 (diff)
downloadsonarqube-8d9f68e8ba9fc37de36d8ed98312ecb4c9aa6147.tar.gz
sonarqube-8d9f68e8ba9fc37de36d8ed98312ecb4c9aa6147.zip
SONAR-5473 Fix progressive migration of file_sources.src_hash
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java3
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java29
-rw-r--r--sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/file_sources_missing_src_hash.xml9
3 files changed, 39 insertions, 2 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
index 512e13be07e..76fdd9015f5 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
@@ -149,7 +149,8 @@ public class SourcePersister implements ScanPersister {
mapper.insert(newFileSource);
session.commit();
} else {
- if (!newDataHash.equals(previous.getDataHash())) {
+ // Update only if data_hash has changed or if src_hash is missing (progressive migration)
+ if (!newDataHash.equals(previous.getDataHash()) || !inputFile.hash().equals(previous.getSrcHash())) {
previous
.setData(newData)
.setLineHashes(lineHashesAsMd5Hex(inputFile))
diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
index 92fac0e8fa5..1aa65d51b20 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
@@ -99,6 +99,31 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
}
@Test
+ public void testPersistUpdateWhenSrcHashIsMissing() throws Exception {
+ setupData("file_sources_missing_src_hash");
+ Date now = DateUtils.parseDateTime("2014-10-29T16:44:02+0100");
+ when(system2.newDate()).thenReturn(now);
+
+ String relativePathSame = "src/same.java";
+ java.io.File sameFile = new java.io.File(basedir, relativePathSame);
+ FileUtils.write(sameFile, "unchanged\ncontent");
+ DefaultInputFile inputFileNew = new DefaultInputFile(PROJECT_KEY, relativePathSame)
+ .setLines(2)
+ .setAbsolutePath(sameFile.getAbsolutePath())
+ .setHash("123456")
+ .setLineHashes(new byte[][] {md5("unchanged"), md5("content")});
+ when(inputPathCache.all()).thenReturn(Arrays.<InputPath>asList(inputFileNew));
+
+ mockResourceCache(relativePathSame, PROJECT_KEY, "uuidsame");
+
+ sourcePersister.persist();
+ FileSourceDto fileSourceDto = new FileSourceDao(getMyBatis()).select("uuidsame");
+ assertThat(fileSourceDto.getCreatedAt()).isEqualTo(DateUtils.parseDateTime("2014-10-10T16:44:02+0200").getTime());
+ assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(now.getTime());
+ assertThat(fileSourceDto.getSrcHash()).isEqualTo("123456");
+ }
+
+ @Test
public void testPersistDontTouchUnchanged() throws Exception {
setupData("file_sources");
when(system2.newDate()).thenReturn(DateUtils.parseDateTime("2014-10-29T16:44:02+0100"));
@@ -106,7 +131,9 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
String relativePathSame = "src/same.java";
java.io.File sameFile = new java.io.File(basedir, relativePathSame);
FileUtils.write(sameFile, "unchanged\ncontent");
- DefaultInputFile inputFileNew = new DefaultInputFile(PROJECT_KEY, relativePathSame).setLines(2).setAbsolutePath(sameFile.getAbsolutePath())
+ DefaultInputFile inputFileNew = new DefaultInputFile(PROJECT_KEY, relativePathSame).setLines(2)
+ .setAbsolutePath(sameFile.getAbsolutePath())
+ .setHash("123456")
.setLineHashes(new byte[][] {md5("unchanged"), md5("ncontent")});
when(inputPathCache.all()).thenReturn(Arrays.<InputPath>asList(inputFileNew));
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/file_sources_missing_src_hash.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/file_sources_missing_src_hash.xml
new file mode 100644
index 00000000000..4a26ff98e6a
--- /dev/null
+++ b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/file_sources_missing_src_hash.xml
@@ -0,0 +1,9 @@
+<dataset>
+ <file_sources id="101" project_uuid="projectUuid" file_uuid="uuidsame"
+ data=",,,,,,,,,,,,,,,unchanged&#13;&#10;,,,,,,,,,,,,,,,content&#13;&#10;"
+ line_hashes="8d7b3d6b83c0a517eac07e1aac94b773&#10;9a0364b9e99bb480dd25e1f0284c8555"
+ data_hash="0263047cd758c68c27683625f072f010"
+ src_hash="[null]"
+ created_at="1412952242000" updated_at="1412952242000" />
+
+</dataset>