]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5473 Fix progressive migration of file_sources.src_hash
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 30 Jan 2015 10:12:12 +0000 (11:12 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 30 Jan 2015 15:48:32 +0000 (16:48 +0100)
sonar-batch/src/main/java/org/sonar/batch/index/SourcePersister.java
sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/file_sources_missing_src_hash.xml [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/source/db/FileSourceMapper.xml

index 512e13be07e2210b606d3bd58164f8e404f336d3..76fdd9015f5c8c403e578bacfff3db2a13406cb6 100644 (file)
@@ -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))
index 92fac0e8fa5e7dbb124080f2b565464d14a0a919..1aa65d51b20199285d9a2d1d33feb1c0c6ee2122 100644 (file)
@@ -98,6 +98,31 @@ public class SourcePersisterTest extends AbstractDaoTestCase {
     basedir = temp.newFolder();
   }
 
+  @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");
@@ -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 (file)
index 0000000..4a26ff9
--- /dev/null
@@ -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>
index 9ac62cef3e3840dd161adb4432f97af111d057d1..009f36d1451f52a566797960403a2da762c9e12e 100644 (file)
@@ -11,7 +11,7 @@
   </select>
   
   <select id="selectAllFileDataHashByProject" parameterType="string" resultType="org.sonar.core.source.db.FileSourceDto">
-    SELECT id, file_uuid as fileUuid, data_hash as dataHash
+    SELECT id, file_uuid as fileUuid, data_hash as dataHash, src_hash as srcHash
     FROM file_sources
     WHERE project_uuid = #{projectUuid}
   </select>