From 36988cc09fdd1bbf63b6e66e84513469bb79a792 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Wed, 22 Sep 2021 15:39:52 -0500 Subject: [PATCH] SONAR-15425 'api/rules/repositories' is empty for new instances --- .../org/sonar/db/rule/RuleRepositoryDao.java | 19 ++++++++---- .../sonar/db/rule/RuleRepositoryMapper.java | 5 +++- .../sonar/db/rule/RuleRepositoryMapper.xml | 5 ++++ .../sonar/db/rule/RuleRepositoryDaoTest.java | 30 +++++++++++++------ .../org/sonar/server/rule/RegisterRules.java | 12 +++++--- .../sonar/server/rule/RegisterRulesTest.java | 2 +- .../qualityprofile/ws/CompareActionTest.java | 2 +- .../sonar/server/rule/ws/AppActionTest.java | 2 +- .../rule/ws/RepositoriesActionTest.java | 2 +- 9 files changed, 56 insertions(+), 23 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java index ae64c9c4cb9..130c499fcad 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryDao.java @@ -21,6 +21,7 @@ package org.sonar.db.rule; import java.util.Collection; import java.util.List; +import java.util.Set; import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DatabaseUtils; @@ -44,6 +45,10 @@ public class RuleRepositoryDao implements Dao { return dbSession.getMapper(RuleRepositoryMapper.class).selectAll(); } + public Set selectAllKeys(DbSession dbSession) { + return dbSession.getMapper(RuleRepositoryMapper.class).selectAllKeys(); + } + /** * @return a non-null list ordered by key (as implemented by database, order may * depend on case sensitivity) @@ -52,14 +57,18 @@ public class RuleRepositoryDao implements Dao { return dbSession.getMapper(RuleRepositoryMapper.class).selectByLanguage(language); } - public void insertOrUpdate(DbSession dbSession, Collection dtos) { + public void insert(DbSession dbSession, Collection dtos) { RuleRepositoryMapper mapper = dbSession.getMapper(RuleRepositoryMapper.class); long now = system2.now(); for (RuleRepositoryDto dto : dtos) { - int updated = mapper.update(dto); - if (updated == 0) { - mapper.insert(dto, now); - } + mapper.insert(dto, now); + } + } + + public void update(DbSession dbSession, Collection dtos) { + RuleRepositoryMapper mapper = dbSession.getMapper(RuleRepositoryMapper.class); + for (RuleRepositoryDto dto : dtos) { + mapper.update(dto); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java index 216bac31ec5..32cf93e4598 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleRepositoryMapper.java @@ -21,6 +21,7 @@ package org.sonar.db.rule; import java.util.Collection; import java.util.List; +import java.util.Set; import javax.annotation.CheckForNull; import org.apache.ibatis.annotations.Param; @@ -28,6 +29,8 @@ public interface RuleRepositoryMapper { List selectAll(); + Set selectAllKeys(); + List selectByLanguage(@Param("language") String language); @CheckForNull @@ -35,7 +38,7 @@ public interface RuleRepositoryMapper { void insert(@Param("repository") RuleRepositoryDto repository, @Param("now") long now); - int update(@Param("repository") RuleRepositoryDto repository); + void update(@Param("repository") RuleRepositoryDto repository); void deleteIfKeyNotIn(@Param("keys") Collection keys); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml index b6e3321b950..962dd8a1a1a 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleRepositoryMapper.xml @@ -13,6 +13,11 @@ order by kee + +