diff options
Diffstat (limited to 'sonar-core')
6 files changed, 63 insertions, 4 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDao.java b/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDao.java index eced8499a4d..16864674f20 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDao.java +++ b/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceDao.java @@ -121,4 +121,8 @@ public class FileSourceDao implements BatchComponent, ServerComponent, DaoCompon } } + public void updateDateWhenUpdatedDateIsZero(DbSession session, String projectUuid, long updateDate) { + session.getMapper(FileSourceMapper.class).updateDateWhenUpdatedDateIsZero(projectUuid, updateDate); + } + } diff --git a/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceMapper.java b/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceMapper.java index e8876892357..633e4d140ed 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/source/db/FileSourceMapper.java @@ -20,6 +20,8 @@ package org.sonar.core.source.db; +import org.apache.ibatis.annotations.Param; + import javax.annotation.CheckForNull; import java.util.List; @@ -35,6 +37,8 @@ public interface FileSourceMapper { void update(FileSourceDto dto); + void updateDateWhenUpdatedDateIsZero(@Param("projectUuid") String projectUuid, @Param("date") Long updateDate); + @CheckForNull String selectLineHashes(String fileUuid); } diff --git a/sonar-core/src/main/resources/org/sonar/core/source/db/FileSourceMapper.xml b/sonar-core/src/main/resources/org/sonar/core/source/db/FileSourceMapper.xml index 2edae7f055c..49ede1dfb30 100644 --- a/sonar-core/src/main/resources/org/sonar/core/source/db/FileSourceMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/source/db/FileSourceMapper.xml @@ -18,20 +18,27 @@ </select> <insert id="insert" parameterType="org.sonar.core.source.db.FileSourceDto" useGeneratedKeys="false"> - insert into file_sources (project_uuid, file_uuid, created_at, updated_at, binary_data, line_hashes, data_hash, src_hash) - values (#{projectUuid,jdbcType=VARCHAR}, #{fileUuid,jdbcType=VARCHAR}, #{createdAt,jdbcType=BIGINT}, + INSERT INTO file_sources (project_uuid, file_uuid, created_at, updated_at, binary_data, line_hashes, data_hash, src_hash) + VALUES (#{projectUuid,jdbcType=VARCHAR}, #{fileUuid,jdbcType=VARCHAR}, #{createdAt,jdbcType=BIGINT}, #{updatedAt,jdbcType=BIGINT}, #{binaryData,jdbcType=BLOB}, #{lineHashes,jdbcType=CLOB}, #{dataHash,jdbcType=VARCHAR}, #{srcHash,jdbcType=VARCHAR}) </insert> <update id="update" parameterType="org.sonar.core.source.db.FileSourceDto" useGeneratedKeys="false"> - update file_sources set + UPDATE file_sources SET updated_at = #{updatedAt,jdbcType=BIGINT}, binary_data = #{binaryData,jdbcType=BLOB}, line_hashes = #{lineHashes,jdbcType=CLOB}, data_hash = #{dataHash,jdbcType=VARCHAR}, src_hash = #{srcHash,jdbcType=VARCHAR} - where id = #{id} + WHERE id = #{id} + </update> + + <update id="updateDateWhenUpdatedDateIsZero" parameterType="org.sonar.core.source.db.FileSourceDto" useGeneratedKeys="false"> + UPDATE file_sources SET + updated_at = #{date,jdbcType=BIGINT} + WHERE project_uuid = #{projectUuid} + AND updated_at = 0 </update> <select id="selectLineHashes" parameterType="string" resultType="String"> diff --git a/sonar-core/src/test/java/org/sonar/core/source/db/FileSourceDaoTest.java b/sonar-core/src/test/java/org/sonar/core/source/db/FileSourceDaoTest.java index aed0f04fd71..8f2ed1fb45a 100644 --- a/sonar-core/src/test/java/org/sonar/core/source/db/FileSourceDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/source/db/FileSourceDaoTest.java @@ -128,6 +128,16 @@ public class FileSourceDaoTest extends AbstractDaoTestCase { checkTable("update", "file_sources", "project_uuid", "file_uuid", "data_hash", "line_hashes", "src_hash", "created_at", "updated_at"); } + @Test + public void update_date_when_updated_date_is_zero() throws Exception { + setupData("update_date_when_updated_date_is_zero"); + + dao.updateDateWhenUpdatedDateIsZero(session, "ABCD", 1500000000002L); + session.commit(); + + checkTable("update_date_when_updated_date_is_zero", "file_sources", "project_uuid", "file_uuid", "data_hash", "line_hashes", "src_hash", "created_at", "updated_at"); + } + private static class ReaderToStringFunction implements Function<Reader, String> { String result = null; diff --git a/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero-result.xml b/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero-result.xml new file mode 100644 index 00000000000..931bab04e18 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero-result.xml @@ -0,0 +1,18 @@ +<dataset> + + <!-- Updated --> + <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="1500000000002"/> + + <!-- Not updated because updated_at is not null --> + <file_sources id="102" project_uuid="ABCD" file_uuid="FILE2_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="1500000000000"/> + + <!-- Not updated because on another project --> + <file_sources id="103" project_uuid="BCDE" file_uuid="FILE3_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="0"/> + +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero.xml b/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero.xml new file mode 100644 index 00000000000..9782d5c3837 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/source/db/FileSourceDaoTest/update_date_when_updated_date_is_zero.xml @@ -0,0 +1,16 @@ +<dataset> + + <!-- Only this source should be updated --> + <file_sources id="101" project_uuid="ABCD" file_uuid="FILE1_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="0"/> + + <file_sources id="102" project_uuid="ABCD" file_uuid="FILE2_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="1500000000000"/> + + <file_sources id="103" project_uuid="BCDE" file_uuid="FILE3_UUID" + binary_data="abcde" data_hash="hash" line_hashes="ABC\nDEF\nGHI" src_hash="FILE_HASH" + created_at="1500000000000" updated_at="0"/> + +</dataset> |