瀏覽代碼

SONAR-13221 change PK of RULES and update FK

tags/8.4.0.35506
Michal Duda 4 年之前
父節點
當前提交
bf05b3c7c2
共有 100 個檔案被更改,包括 3462 行新增520 行删除
  1. 7
    3
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java
  2. 1
    2
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/Rule.java
  3. 5
    5
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleImpl.java
  4. 2
    2
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepository.java
  5. 14
    14
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImpl.java
  6. 2
    2
      server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistIssuesStep.java
  7. 5
    5
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreatorTest.java
  8. 6
    6
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/DumbRule.java
  9. 24
    26
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImplTest.java
  10. 9
    9
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryRule.java
  11. 2
    2
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/UpdateConflictResolverTest.java
  12. 0
    4
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MapBasedRawMeasureRepositoryTest.java
  13. 8
    5
      server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAdHocRulesStepTest.java
  14. 7
    7
      server/sonar-db-dao/src/main/java/org/sonar/db/es/RuleExtensionId.java
  15. 15
    11
      server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java
  16. 13
    12
      server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java
  17. 11
    7
      server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java
  18. 5
    5
      server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java
  19. 7
    3
      server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/IndexedActiveRuleDto.java
  20. 10
    6
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/DeprecatedRuleKeyDto.java
  21. 22
    20
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java
  22. 19
    22
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java
  23. 9
    9
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java
  24. 5
    5
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleExtensionForIndexingDto.java
  25. 8
    4
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java
  26. 6
    6
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java
  27. 10
    6
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java
  28. 10
    6
      server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleParamDto.java
  29. 13
    11
      server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml
  30. 23
    23
      server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml
  31. 3
    3
      server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml
  32. 2
    2
      server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
  33. 50
    46
      server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
  34. 20
    20
      server/sonar-db-dao/src/schema/schema-sq.ddl
  35. 4
    4
      server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java
  36. 3
    3
      server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java
  37. 6
    6
      server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueMapperTest.java
  38. 36
    34
      server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java
  39. 9
    9
      server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java
  40. 100
    100
      server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java
  41. 15
    17
      server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java
  42. 17
    17
      server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleDbTester.java
  43. 13
    11
      server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleTesting.java
  44. 82
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java
  45. 38
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/AddPrimaryKeyOnUuidColumnOfRulesTable.java
  46. 58
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/AddUuidAndTemplateUuidColumnsToRules.java
  47. 37
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/DropIdColumnOfRulesTable.java
  48. 41
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/DropPrimaryKeyOnIdColumnOfRulesTable.java
  49. 37
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/DropTemplateIdColumnOfRulesTable.java
  50. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/MakeRulesUuidColumnNotNullable.java
  51. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesTemplateUuid.java
  52. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesUuid.java
  53. 70
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddIndexToActiveRulesTable.java
  54. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddRuleUuidColumnToActiveRulesTable.java
  55. 43
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropIndexOnRuleIdColumnOfActiveRulesTable.java
  56. 37
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropRuleIdColumnOfActiveRulesTable.java
  57. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/MakeActiveRulesRuleUuidColumnNotNullable.java
  58. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/PopulateActiveRulesRuleUuidColumn.java
  59. 63
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddIndexToDeprecatedRuleKeysTable.java
  60. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddRuleUuidColumnToDeprecatedRuleKeysTable.java
  61. 41
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTable.java
  62. 37
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropRuleIdColumnOfDeprecatedRuleKeysTable.java
  63. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/MakeDeprecatedRuleKeysRuleUuidColumnNotNullable.java
  64. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumn.java
  65. 62
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/AddIndexToIssuesTable.java
  66. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/AddRuleUuidColumnToIssuesTable.java
  67. 41
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/DropIndexOnRuleIdColumnOfIssuesTable.java
  68. 37
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/DropRuleIdColumnOfIssuesTable.java
  69. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/PopulateIssuesRuleUuidColumn.java
  70. 38
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/AddPrimaryKeyOnUuidAndOrganizationUuidColumnOfRulesMetadataTable.java
  71. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/AddRuleUuidColumnToRulesMetadataTable.java
  72. 41
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/DropPrimaryKeyOnIdColumnOfRulesMetadataTable.java
  73. 37
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/DropRuleIdColumnOfRulesMetadataTable.java
  74. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/MakeRulesMetadataRuleUuidColumnNotNullable.java
  75. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/PopulateRulesMetadataRuleUuidColumn.java
  76. 81
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/AddIndexesToRulesParametersTable.java
  77. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/AddRuleUuidColumnToRulesParametersTable.java
  78. 45
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/DropIndexesOnRuleIdColumnOfRulesParametersTable.java
  79. 37
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/DropRuleIdColumnOfRulesParametersTable.java
  80. 50
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/MakeRulesParametersRuleUuidColumnNotNullable.java
  81. 49
    0
      server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/PopulateRulesParametersRuleUuidColumn.java
  82. 50
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/AddPrimaryKeyOnUuidColumnOfRulesTableTest.java
  83. 66
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/AddUuidAndTemplateUuidColumnsToRulesTest.java
  84. 50
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/DropIdColumnOfRulesTableTest.java
  85. 56
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/DropPrimaryKeyOnIdColumnOfRulesTableTest.java
  86. 50
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/DropTemplateIdColumnOfRulesTableTest.java
  87. 43
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/MakeRulesUuidColumnNotNullableTest.java
  88. 74
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesTemplateUuidTest.java
  89. 75
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesUuidTest.java
  90. 51
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddIndexToActiveRulesTableTest.java
  91. 79
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddRuleUuidColumnToActiveRulesTableTest.java
  92. 54
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropIndexOnRuleIdColumnOfActiveRulesTableTest.java
  93. 50
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropRuleIdColumnOfActiveRulesTableTest.java
  94. 43
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/MakeActiveRulesRuleUuidColumnNotNullableTest.java
  95. 84
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/PopulateActiveRulesRuleUuidColumnTest.java
  96. 51
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddIndexToDeprecatedRuleKeysTableTest.java
  97. 80
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddRuleUuidColumnToDeprecatedRuleKeysTableTest.java
  98. 54
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTableTest.java
  99. 50
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropRuleIdColumnOfDeprecatedRuleKeysTableTest.java
  100. 0
    0
      server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/MakeDeprecatedRuleKeysRuleUuidColumnNotNullableTest.java

+ 7
- 3
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java 查看文件

@@ -24,6 +24,7 @@ import java.util.Objects;
import java.util.Optional;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
@@ -45,11 +46,13 @@ public class AdHocRuleCreator {
private final DbClient dbClient;
private final System2 system2;
private final RuleIndexer ruleIndexer;
private final UuidFactory uuidFactory;

public AdHocRuleCreator(DbClient dbClient, System2 system2, RuleIndexer ruleIndexer) {
public AdHocRuleCreator(DbClient dbClient, System2 system2, RuleIndexer ruleIndexer, UuidFactory uuidFactory) {
this.dbClient = dbClient;
this.system2 = system2;
this.ruleIndexer = ruleIndexer;
this.uuidFactory = uuidFactory;
}

/**
@@ -63,6 +66,7 @@ public class AdHocRuleCreator {
long now = system2.now();
if (!existingRuleDtoOpt.isPresent()) {
RuleDefinitionDto dto = new RuleDefinitionDto()
.setUuid(uuidFactory.create())
.setRuleKey(adHoc.getKey())
.setIsExternal(true)
.setIsAdHoc(true)
@@ -73,7 +77,7 @@ public class AdHocRuleCreator {
.setUpdatedAt(now);
dao.insert(dbSession, dto);
metadata = new RuleMetadataDto()
.setRuleId(dto.getId())
.setRuleUuid(dto.getUuid())
.setOrganizationUuid(organizationDto.getUuid());
} else {
// No need to update the rule, only org specific metadata
@@ -110,7 +114,7 @@ public class AdHocRuleCreator {
}

RuleDto ruleDto = dao.selectOrFailByKey(dbSession, organizationDto, adHoc.getKey());
ruleIndexer.commitAndIndex(dbSession, ruleDto.getId());
ruleIndexer.commitAndIndex(dbSession, ruleDto.getUuid());
return ruleDto;
}


+ 1
- 2
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/Rule.java 查看文件

@@ -27,8 +27,7 @@ import org.sonar.api.rules.RuleType;
import org.sonar.api.server.debt.DebtRemediationFunction;

public interface Rule {

int getId();
String getUuid();

RuleKey getKey();


+ 5
- 5
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleImpl.java 查看文件

@@ -36,7 +36,7 @@ import static com.google.common.collect.Sets.union;
@Immutable
public class RuleImpl implements Rule {

private final int id;
private final String uuid;
private final RuleKey key;
private final String name;
private final String language;
@@ -49,7 +49,7 @@ public class RuleImpl implements Rule {
private final boolean isAdHoc;

public RuleImpl(RuleDto dto) {
this.id = dto.getId();
this.uuid = dto.getUuid();
this.key = dto.getKey();
this.name = dto.getName();
this.language = dto.getLanguage();
@@ -63,8 +63,8 @@ public class RuleImpl implements Rule {
}

@Override
public int getId() {
return id;
public String getUuid() {
return this.uuid;
}

@Override
@@ -129,7 +129,7 @@ public class RuleImpl implements Rule {
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("id", id)
.add("uuid", uuid)
.add("key", key)
.add("name", name)
.add("language", language)

+ 2
- 2
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepository.java 查看文件

@@ -38,14 +38,14 @@ public interface RuleRepository {
/**
* @throws IllegalArgumentException when there is no Rule for the specified RuleKey in the repository
*/
Rule getById(int id);
Rule getByUuid(String uuid);

/**
* @throws NullPointerException if {@code key} is {@code null}
*/
Optional<Rule> findByKey(RuleKey key);

Optional<Rule> findById(int id);
Optional<Rule> findByUuid(String uuid);

void addOrUpdateAddHocRuleIfNeeded(RuleKey ruleKey, Supplier<NewAdHocRule> ruleSupplier);


+ 14
- 14
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImpl.java 查看文件

@@ -46,7 +46,7 @@ public class RuleRepositoryImpl implements RuleRepository {
@CheckForNull
private Map<RuleKey, Rule> rulesByKey;
@CheckForNull
private Map<Integer, Rule> rulesById;
private Map<String, Rule> rulesByUuid;

private final AdHocRuleCreator creator;
private final DbClient dbClient;
@@ -80,7 +80,7 @@ public class RuleRepositoryImpl implements RuleRepository {

private void persistAndIndex(DbSession dbSession, NewAdHocRule adHocRule) {
Rule rule = new RuleImpl(creator.persistAndIndex(dbSession, adHocRule, analysisMetadataHolder.getOrganization().toDto()));
rulesById.put(rule.getId(), rule);
rulesByUuid.put(rule.getUuid(), rule);
rulesByKey.put(adHocRule.getKey(), rule);
}

@@ -105,19 +105,19 @@ public class RuleRepositoryImpl implements RuleRepository {
}

@Override
public Rule getById(int id) {
public Rule getByUuid(String uuid) {
ensureInitialized();

Rule rule = rulesById.get(id);
checkArgument(rule != null, "Can not find rule for id %s. This rule does not exist in DB", id);
Rule rule = rulesByUuid.get(uuid);
checkArgument(rule != null, "Can not find rule for uuid %s. This rule does not exist in DB", uuid);
return rule;
}

@Override
public Optional<Rule> findById(int id) {
public Optional<Rule> findByUuid(String uuid) {
ensureInitialized();

return Optional.ofNullable(rulesById.get(id));
return Optional.ofNullable(rulesByUuid.get(uuid));
}

private static void verifyKeyArgument(RuleKey key) {
@@ -134,15 +134,15 @@ public class RuleRepositoryImpl implements RuleRepository {

private void loadRulesFromDb(DbSession dbSession) {
this.rulesByKey = new HashMap<>();
this.rulesById = new HashMap<>();
this.rulesByUuid = new HashMap<>();
String organizationUuid = analysisMetadataHolder.getOrganization().getUuid();
Multimap<Integer, DeprecatedRuleKeyDto> deprecatedRuleKeysByRuleId = dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream()
.collect(MoreCollectors.index(DeprecatedRuleKeyDto::getRuleId));
Multimap<String, DeprecatedRuleKeyDto> deprecatedRuleKeysByRuleUuid = dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream()
.collect(MoreCollectors.index(DeprecatedRuleKeyDto::getRuleUuid));
for (RuleDto ruleDto : dbClient.ruleDao().selectAll(dbSession, organizationUuid)) {
Rule rule = new RuleImpl(ruleDto);
rulesByKey.put(ruleDto.getKey(), rule);
rulesById.put(ruleDto.getId(), rule);
deprecatedRuleKeysByRuleId.get(ruleDto.getId()).forEach(t -> rulesByKey.put(RuleKey.of(t.getOldRepositoryKey(), t.getOldRuleKey()), rule));
rulesByUuid.put(ruleDto.getUuid(), rule);
deprecatedRuleKeysByRuleUuid.get(ruleDto.getUuid()).forEach(t -> rulesByKey.put(RuleKey.of(t.getOldRepositoryKey(), t.getOldRuleKey()), rule));
}
}

@@ -158,8 +158,8 @@ public class RuleRepositoryImpl implements RuleRepository {
}

@Override
public int getId() {
throw new UnsupportedOperationException("Rule is not persisted, can't know the id");
public String getUuid() {
throw new UnsupportedOperationException("Rule is not persisted, can't know the uuid");
}

@Override

+ 2
- 2
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/PersistIssuesStep.java 查看文件

@@ -109,8 +109,8 @@ public class PersistIssuesStep implements ComputationStep {

long now = system2.now();
addedIssues.forEach(i -> {
int ruleId = ruleRepository.getByKey(i.ruleKey()).getId();
IssueDto dto = IssueDto.toDtoForComputationInsert(i, ruleId, now);
String ruleUuid = ruleRepository.getByKey(i.ruleKey()).getUuid();
IssueDto dto = IssueDto.toDtoForComputationInsert(i, ruleUuid, now);
mapper.insert(dto);
statistics.inserts++;
});

+ 5
- 5
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreatorTest.java 查看文件

@@ -24,6 +24,7 @@ import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.System2;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
@@ -46,7 +47,7 @@ public class AdHocRuleCreatorTest {
public EsTester es = EsTester.create();

private RuleIndexer indexer = new RuleIndexer(es.client(), db.getDbClient());
private AdHocRuleCreator underTest = new AdHocRuleCreator(db.getDbClient(), System2.INSTANCE, indexer);
private AdHocRuleCreator underTest = new AdHocRuleCreator(db.getDbClient(), System2.INSTANCE, indexer, new SequenceUuidFactory());
private DbSession dbSession = db.getSession();

@Test
@@ -59,7 +60,7 @@ public class AdHocRuleCreatorTest {
assertThat(rule).isNotNull();
assertThat(rule.isExternal()).isTrue();
assertThat(rule.isAdHoc()).isTrue();
assertThat(rule.getId()).isGreaterThan(0);
assertThat(rule.getUuid()).isNotBlank();
assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
assertThat(rule.getDescription()).isNull();
@@ -88,7 +89,7 @@ public class AdHocRuleCreatorTest {
assertThat(rule).isNotNull();
assertThat(rule.isExternal()).isTrue();
assertThat(rule.isAdHoc()).isTrue();
assertThat(rule.getId()).isGreaterThan(0);
assertThat(rule.getUuid()).isNotBlank();
assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign");
assertThat(rule.getDescription()).isNull();
@@ -145,7 +146,7 @@ public class AdHocRuleCreatorTest {
assertThat(ruleUpdated).isNotNull();
assertThat(ruleUpdated.isExternal()).isTrue();
assertThat(ruleUpdated.isAdHoc()).isTrue();
assertThat(ruleUpdated.getId()).isGreaterThan(0);
assertThat(ruleUpdated.getUuid()).isNotBlank();
assertThat(ruleUpdated.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign"));
assertThat(ruleUpdated.getName()).isEqualTo("eslint:no-cond-assign");
assertThat(ruleUpdated.getDescription()).isNull();
@@ -155,7 +156,6 @@ public class AdHocRuleCreatorTest {
assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo("A description updated");
assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(Severity.CRITICAL);
assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant());

assertThat(ruleUpdated.getDefinition().getCreatedAt()).isEqualTo(creationDate);
assertThat(ruleUpdated.getMetadata().getCreatedAt()).isEqualTo(creationDate);
assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isGreaterThan(creationDate);

+ 6
- 6
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/DumbRule.java 查看文件

@@ -31,7 +31,7 @@ import org.sonar.api.server.debt.DebtRemediationFunction;
import static java.util.Objects.requireNonNull;

public class DumbRule implements Rule {
private Integer id;
private String uuid;
private RuleKey key;
private String name;
private String language;
@@ -45,13 +45,13 @@ public class DumbRule implements Rule {

public DumbRule(RuleKey key) {
this.key = key;
this.id = key.hashCode();
this.uuid = key.rule();
this.name = "name_" + key;
}

@Override
public int getId() {
return requireNonNull(id);
public String getUuid() {
return requireNonNull(uuid);
}

@Override
@@ -105,8 +105,8 @@ public class DumbRule implements Rule {
return isAdHoc;
}

public DumbRule setId(Integer id) {
this.id = id;
public DumbRule setUuid(String uuid) {
this.uuid = uuid;
return this;
}


+ 24
- 26
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImplTest.java 查看文件

@@ -30,6 +30,7 @@ import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.System2;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
@@ -52,14 +53,12 @@ import static org.mockito.Mockito.when;
import static org.mockito.internal.verification.VerificationModeFactory.times;

public class RuleRepositoryImplTest {

private static final RuleDto AB_RULE = createABRuleDto()
.setId(9688);
private static final RuleDto AB_RULE = createABRuleDto().setUuid("rule-uuid");
private static final RuleKey AB_RULE_DEPRECATED_KEY_1 = RuleKey.of("old_a", "old_b");
private static final RuleKey AB_RULE_DEPRECATED_KEY_2 = RuleKey.of(AB_RULE.getRepositoryKey(), "old_b");
private static final RuleKey DEPRECATED_KEY_OF_NON_EXITING_RULE = RuleKey.of("some_rep", "some_key");
private static final RuleKey AC_RULE_KEY = RuleKey.of("a", "c");
private static final int AC_RULE_ID = 684;
private static final String AC_RULE_UUID = "uuid-684";
private static final String ORGANIZATION_UUID = "org-1";
private static final String QUALITY_GATE_UUID = "QUALITY_GATE_UUID";

@@ -77,7 +76,7 @@ public class RuleRepositoryImplTest {
private RuleDao ruleDao = mock(RuleDao.class);

private RuleIndexer ruleIndexer = mock(RuleIndexer.class);
private AdHocRuleCreator adHocRuleCreator = new AdHocRuleCreator(db.getDbClient(), System2.INSTANCE, ruleIndexer);
private AdHocRuleCreator adHocRuleCreator = new AdHocRuleCreator(db.getDbClient(), System2.INSTANCE, ruleIndexer, new SequenceUuidFactory());
private RuleRepositoryImpl underTest = new RuleRepositoryImpl(adHocRuleCreator, dbClient, analysisMetadataHolder);

@Before
@@ -87,17 +86,17 @@ public class RuleRepositoryImplTest {
when(ruleDao.selectAll(any(DbSession.class), eq(ORGANIZATION_UUID))).thenReturn(ImmutableList.of(AB_RULE));
DeprecatedRuleKeyDto abDeprecatedRuleKey1 = deprecatedRuleKeyOf(AB_RULE, AB_RULE_DEPRECATED_KEY_1);
DeprecatedRuleKeyDto abDeprecatedRuleKey2 = deprecatedRuleKeyOf(AB_RULE, AB_RULE_DEPRECATED_KEY_2);
DeprecatedRuleKeyDto deprecatedRuleOfNonExistingRule = deprecatedRuleKeyOf(77777, DEPRECATED_KEY_OF_NON_EXITING_RULE);
DeprecatedRuleKeyDto deprecatedRuleOfNonExistingRule = deprecatedRuleKeyOf("unknown-rule-uuid", DEPRECATED_KEY_OF_NON_EXITING_RULE);
when(ruleDao.selectAllDeprecatedRuleKeys(any(DbSession.class))).thenReturn(ImmutableSet.of(
abDeprecatedRuleKey1, abDeprecatedRuleKey2, deprecatedRuleOfNonExistingRule));
}

private static DeprecatedRuleKeyDto deprecatedRuleKeyOf(RuleDto ruleDto, RuleKey deprecatedRuleKey) {
return deprecatedRuleKeyOf(ruleDto.getId(), deprecatedRuleKey);
return deprecatedRuleKeyOf(ruleDto.getUuid(), deprecatedRuleKey);
}

private static DeprecatedRuleKeyDto deprecatedRuleKeyOf(int ruleId, RuleKey deprecatedRuleKey) {
return new DeprecatedRuleKeyDto().setRuleId(ruleId)
private static DeprecatedRuleKeyDto deprecatedRuleKeyOf(String ruleUuid, RuleKey deprecatedRuleKey) {
return new DeprecatedRuleKeyDto().setRuleUuid(ruleUuid)
.setOldRepositoryKey(deprecatedRuleKey.repository())
.setOldRuleKey(deprecatedRuleKey.rule());
}
@@ -127,7 +126,7 @@ public class RuleRepositoryImplTest {

@Test
public void first_call_to_getById_triggers_call_to_db_and_any_subsequent_get_or_find_call_does_not() {
underTest.getById(AB_RULE.getId());
underTest.getByUuid(AB_RULE.getUuid());

verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));

@@ -136,7 +135,7 @@ public class RuleRepositoryImplTest {

@Test
public void first_call_to_findById_triggers_call_to_db_and_any_subsequent_get_or_find_call_does_not() {
underTest.findById(AB_RULE.getId());
underTest.findByUuid(AB_RULE.getUuid());

verify(ruleDao, times(1)).selectAll(any(DbSession.class), eq(ORGANIZATION_UUID));

@@ -237,30 +236,30 @@ public class RuleRepositoryImplTest {
}

@Test
public void getById_returns_Rule_if_it_exists_in_DB() {
Rule rule = underTest.getById(AB_RULE.getId());
public void getByUuid_returns_Rule_if_it_exists_in_DB() {
Rule rule = underTest.getByUuid(AB_RULE.getUuid());

assertIsABRule(rule);
}

@Test
public void getById_throws_IAE_if_rules_does_not_exist_in_DB() {
public void getByUuid_throws_IAE_if_rules_does_not_exist_in_DB() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Can not find rule for id " + AC_RULE_ID + ". This rule does not exist in DB");
expectedException.expectMessage("Can not find rule for uuid " + AC_RULE_UUID + ". This rule does not exist in DB");

underTest.getById(AC_RULE_ID);
underTest.getByUuid(AC_RULE_UUID);
}

@Test
public void findById_returns_absent_if_rule_does_not_exist_in_DB() {
Optional<Rule> rule = underTest.findById(AC_RULE_ID);
public void findByUuid_returns_absent_if_rule_does_not_exist_in_DB() {
Optional<Rule> rule = underTest.findByUuid(AC_RULE_UUID);

assertThat(rule).isEmpty();
}

@Test
public void findById_returns_Rule_if_it_exists_in_DB() {
Optional<Rule> rule = underTest.findById(AB_RULE.getId());
public void findByUuid_returns_Rule_if_it_exists_in_DB() {
Optional<Rule> rule = underTest.findByUuid(AB_RULE.getUuid());

assertIsABRule(rule.get());
}
@@ -295,8 +294,8 @@ public class RuleRepositoryImplTest {
Rule rule = underTest.getByKey(ruleKey);
assertThat(rule).isNotNull();

assertThat(underTest.getById(ruleDefinitionDto.get().getId())).isNotNull();
verify(ruleIndexer).commitAndIndex(db.getSession(), ruleDefinitionDto.get().getId());
assertThat(underTest.getByUuid(ruleDefinitionDto.get().getUuid())).isNotNull();
verify(ruleIndexer).commitAndIndex(db.getSession(), ruleDefinitionDto.get().getUuid());
}

private void expectNullRuleKeyNPE() {
@@ -312,10 +311,10 @@ public class RuleRepositoryImplTest {
underTest.findByKey(AB_RULE.getKey());
assertNoCallToDb();
reset(ruleDao);
underTest.getById(AB_RULE.getId());
underTest.getByUuid(AB_RULE.getUuid());
assertNoCallToDb();
reset(ruleDao);
underTest.findById(AB_RULE.getId());
underTest.findByUuid(AB_RULE.getUuid());
assertNoCallToDb();
}

@@ -325,7 +324,7 @@ public class RuleRepositoryImplTest {

private void assertIsABRule(Rule rule) {
assertThat(rule).isNotNull();
assertThat(rule.getId()).isEqualTo(AB_RULE.getId());
assertThat(rule.getUuid()).isEqualTo(AB_RULE.getUuid());
assertThat(rule.getKey()).isEqualTo(AB_RULE.getKey());
assertThat(rule.getRemediationFunction()).isNull();
assertThat(rule.getStatus()).isEqualTo(RuleStatus.REMOVED);
@@ -334,7 +333,6 @@ public class RuleRepositoryImplTest {
private static RuleDto createABRuleDto() {
RuleKey ruleKey = RuleKey.of("a", "b");
return new RuleDto()
.setId(ruleKey.hashCode())
.setRepositoryKey(ruleKey.repository())
.setRuleKey(ruleKey.rule())
.setStatus(RuleStatus.REMOVED)

+ 9
- 9
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryRule.java 查看文件

@@ -33,13 +33,13 @@ import static java.util.Objects.requireNonNull;
public class RuleRepositoryRule extends ExternalResource implements RuleRepository {

private final Map<RuleKey, Rule> rulesByKey = new HashMap<>();
private final Map<Integer, Rule> rulesById = new HashMap<>();
private final Map<String, Rule> rulesByUuid = new HashMap<>();
private final Map<RuleKey, NewAdHocRule> newExternalRulesById = new HashMap<>();

@Override
protected void after() {
rulesByKey.clear();
rulesById.clear();
rulesByUuid.clear();
}

@Override
@@ -50,8 +50,8 @@ public class RuleRepositoryRule extends ExternalResource implements RuleReposito
}

@Override
public Rule getById(int id) {
Rule rule = rulesById.get(id);
public Rule getByUuid(String uuid) {
Rule rule = rulesByUuid.get(uuid);
checkArgument(rule != null);
return rule;
}
@@ -62,8 +62,8 @@ public class RuleRepositoryRule extends ExternalResource implements RuleReposito
}

@Override
public Optional<Rule> findById(int id) {
return Optional.ofNullable(rulesById.get(id));
public Optional<Rule> findByUuid(String uuid) {
return Optional.ofNullable(rulesByUuid.get(uuid));
}

@Override
@@ -73,15 +73,15 @@ public class RuleRepositoryRule extends ExternalResource implements RuleReposito

public DumbRule add(RuleKey key) {
DumbRule rule = new DumbRule(key);
rule.setId(key.hashCode());
rule.setUuid(key.rule());
rulesByKey.put(key, rule);
rulesById.put(rule.getId(), rule);
rulesByUuid.put(rule.getUuid(), rule);
return rule;
}

public RuleRepositoryRule add(DumbRule rule) {
rulesByKey.put(requireNonNull(rule.getKey()), rule);
rulesById.put(rule.getId(), rule);
rulesByUuid.put(rule.getUuid(), rule);
return this;
}


+ 2
- 2
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/UpdateConflictResolverTest.java 查看文件

@@ -53,7 +53,7 @@ public class UpdateConflictResolverTest {
IssueDto issueDto = new IssueDto()
.setKee("ABCDE")
.setType(CODE_SMELL)
.setRuleId(10)
.setRuleUuid("uuid-10")
.setRuleKey("squid", "AvoidCycles")
.setProjectUuid("U1")
.setComponentUuid("U2")
@@ -97,7 +97,7 @@ public class UpdateConflictResolverTest {
// Issue as seen and changed by end-user
IssueDto dbIssue = new IssueDto()
.setKee("ABCDE")
.setRuleId(10)
.setRuleUuid("uuid-10")
.setRuleKey("squid", "AvoidCycles")
.setComponentUuid("100")
.setComponentKey("struts:org.apache.struts.Action")

+ 0
- 4
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/measure/MapBasedRawMeasureRepositoryTest.java 查看文件

@@ -30,7 +30,6 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.System2;
import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
import org.sonar.ce.task.projectanalysis.component.Component;
@@ -41,7 +40,6 @@ import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
import org.sonar.ce.task.projectanalysis.metric.ReportMetricValidator;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.rule.RuleDto;

import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
@@ -74,8 +72,6 @@ public class MapBasedRawMeasureRepositoryTest {

private static final Measure SOME_MEASURE = Measure.newMeasureBuilder().create("some value");

private static final RuleDto SOME_RULE = RuleDto.createFor(RuleKey.of("A", "1")).setId(963);

private ReportMetricValidator reportMetricValidator = mock(ReportMetricValidator.class);

private MetricRepository metricRepository = mock(MetricRepository.class);

+ 8
- 5
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAdHocRulesStepTest.java 查看文件

@@ -31,6 +31,7 @@ import org.sonar.ce.task.projectanalysis.issue.NewAdHocRule;
import org.sonar.ce.task.projectanalysis.issue.RuleRepositoryImpl;
import org.sonar.ce.task.step.ComputationStep;
import org.sonar.ce.task.step.TestComputationStepContext;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.rule.RuleDao;
@@ -60,7 +61,7 @@ public class PersistAdHocRulesStepTest extends BaseStepTest {
public EsTester es = EsTester.create();

private RuleIndexer indexer = new RuleIndexer(es.client(), dbClient);
private AdHocRuleCreator adHocRuleCreator = new AdHocRuleCreator(dbClient, System2.INSTANCE, indexer);
private AdHocRuleCreator adHocRuleCreator = new AdHocRuleCreator(dbClient, System2.INSTANCE, indexer, new SequenceUuidFactory());

@Override
protected ComputationStep step() {
@@ -77,7 +78,8 @@ public class PersistAdHocRulesStepTest extends BaseStepTest {
public void persist_and_index_new_ad_hoc_rules() {

RuleKey ruleKey = RuleKey.of("external_eslint", "no-cond-assign");
ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey, () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey,
() -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));

underTest.execute(new TestComputationStepContext());

@@ -94,15 +96,16 @@ public class PersistAdHocRulesStepTest extends BaseStepTest {
assertThat(reloaded.getSeverity()).isNull();
assertThat(reloaded.getName()).isEqualTo("eslint:no-cond-assign");

assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isEqualTo(1l);
assertThat(es.getDocuments(RuleIndexDefinition.TYPE_RULE).iterator().next().getId()).isEqualTo(Integer.toString(reloaded.getId()));
assertThat(es.countDocuments(RuleIndexDefinition.TYPE_RULE)).isEqualTo(1L);
assertThat(es.getDocuments(RuleIndexDefinition.TYPE_RULE).iterator().next().getId()).isEqualTo(reloaded.getUuid());
}

@Test
public void do_not_persist_existing_external_rules() {
RuleKey ruleKey = RuleKey.of("eslint", "no-cond-assign");
db.rules().insert(ruleKey, r -> r.setIsExternal(true));
ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey, () -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));
ruleRepository.addOrUpdateAddHocRuleIfNeeded(ruleKey,
() -> new NewAdHocRule(ScannerReport.ExternalIssue.newBuilder().setEngineId("eslint").setRuleId("no-cond-assign").build()));

underTest.execute(new TestComputationStepContext());


+ 7
- 7
server/sonar-db-dao/src/main/java/org/sonar/db/es/RuleExtensionId.java 查看文件

@@ -27,28 +27,28 @@ import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;

public class RuleExtensionId {
private final int ruleId;
private final String ruleUuid;
private final String organizationUuid;
private final String id;

private static final Splitter ID_SPLITTER = Splitter.on(CharMatcher.anyOf("|"));

public RuleExtensionId(String organizationUuid, int ruleId) {
public RuleExtensionId(String organizationUuid, String ruleUuid) {
this.organizationUuid = organizationUuid;
this.ruleId = ruleId;
this.id = format("%s|%s", ruleId, organizationUuid);
this.ruleUuid = ruleUuid;
this.id = format("%s|%s", ruleUuid, organizationUuid);
}

public RuleExtensionId(String ruleExtensionId) {
List<String> splittedId = ID_SPLITTER.splitToList(ruleExtensionId);
checkArgument(splittedId.size() == 2, "Incorrect Id %s", ruleExtensionId);
this.id = ruleExtensionId;
this.ruleId = Integer.parseInt(splittedId.get(0));
this.ruleUuid = splittedId.get(0);
this.organizationUuid = splittedId.get(1);
}

public int getRuleId() {
return ruleId;
public String getRuleUuid() {
return this.ruleUuid;
}

public String getOrganizationUuid() {

+ 15
- 11
server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java 查看文件

@@ -58,7 +58,7 @@ public final class IssueDto implements Serializable {
private String kee;
private String componentUuid;
private String projectUuid;
private Integer ruleId;
private String ruleUuid;
private String severity;
private boolean manualSeverity;
private String message;
@@ -100,10 +100,14 @@ public final class IssueDto implements Serializable {
// populate only when retrieving closed issue for issue tracking
private String closedChangeData;

public IssueDto() {
// nothing to do
}

/**
* On batch side, component keys and uuid are useless
*/
public static IssueDto toDtoForComputationInsert(DefaultIssue issue, int ruleId, long now) {
public static IssueDto toDtoForComputationInsert(DefaultIssue issue, String ruleUuid, long now) {
return new IssueDto()
.setKee(issue.key())
.setType(issue.type())
@@ -118,7 +122,7 @@ public final class IssueDto implements Serializable {
.setManualSeverity(issue.manualSeverity())
.setChecksum(issue.checksum())
.setAssigneeUuid(issue.assignee())
.setRuleId(ruleId)
.setRuleUuid(ruleUuid)
.setRuleKey(issue.ruleKey().repository(), issue.ruleKey().rule())
.setExternal(issue.isFromExternalRuleEngine())
.setTags(issue.tags())
@@ -143,8 +147,8 @@ public final class IssueDto implements Serializable {
/**
* On server side, we need component keys and uuid
*/
public static IssueDto toDtoForServerInsert(DefaultIssue issue, ComponentDto component, ComponentDto project, int ruleId, long now) {
return toDtoForComputationInsert(issue, ruleId, now)
public static IssueDto toDtoForServerInsert(DefaultIssue issue, ComponentDto component, ComponentDto project, String ruleUuid, long now) {
return toDtoForComputationInsert(issue, ruleUuid, now)
.setComponent(component)
.setProject(project);
}
@@ -213,15 +217,15 @@ public final class IssueDto implements Serializable {
return this;
}

public Integer getRuleId() {
return ruleId;
public String getRuleUuid() {
return ruleUuid;
}

/**
* please use setRule(RuleDto rule)
*/
public IssueDto setRuleId(Integer ruleId) {
this.ruleId = ruleId;
public IssueDto setRuleUuid(String ruleUuid) {
this.ruleUuid = ruleUuid;
return this;
}

@@ -437,8 +441,8 @@ public final class IssueDto implements Serializable {
}

public IssueDto setRule(RuleDefinitionDto rule) {
Preconditions.checkNotNull(rule.getId(), "Rule must be persisted.");
this.ruleId = rule.getId();
Preconditions.checkNotNull(rule.getUuid(), "Rule must be persisted.");
this.ruleUuid = rule.getUuid();
this.ruleKey = rule.getRuleKey();
this.ruleRepo = rule.getRepositoryKey();
this.language = rule.getLanguage();

+ 13
- 12
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java 查看文件

@@ -58,16 +58,16 @@ public class ActiveRuleDao implements Dao {
return Optional.ofNullable(mapper(dbSession).selectByKey(key.getRuleProfileUuid(), key.getRuleKey().repository(), key.getRuleKey().rule()));
}

public List<OrgActiveRuleDto> selectByRuleId(DbSession dbSession, OrganizationDto organization, int ruleId) {
return mapper(dbSession).selectByRuleId(organization.getUuid(), ruleId);
public List<OrgActiveRuleDto> selectByRuleUuid(DbSession dbSession, OrganizationDto organization, String ruleUuid) {
return mapper(dbSession).selectByRuleUuid(organization.getUuid(), ruleUuid);
}

public List<ActiveRuleDto> selectByRuleIdOfAllOrganizations(DbSession dbSession, int ruleId) {
return mapper(dbSession).selectByRuleIdOfAllOrganizations(ruleId);
public List<ActiveRuleDto> selectByRuleUuidOfAllOrganizations(DbSession dbSession, String ruleUuid) {
return mapper(dbSession).selectByRuleUuidOfAllOrganizations(ruleUuid);
}

public List<OrgActiveRuleDto> selectByRuleIds(DbSession dbSession, OrganizationDto organization, List<Integer> ids) {
return executeLargeInputs(ids, chunk -> mapper(dbSession).selectByRuleIds(organization.getUuid(), chunk));
public List<OrgActiveRuleDto> selectByRuleUuids(DbSession dbSession, OrganizationDto organization, List<String> uuids) {
return executeLargeInputs(uuids, chunk -> mapper(dbSession).selectByRuleUuids(organization.getUuid(), chunk));
}

/**
@@ -89,17 +89,18 @@ public class ActiveRuleDao implements Dao {
return mapper(dbSession).selectByRuleProfileUuid(ruleProfileDto.getUuid());
}

public Collection<ActiveRuleDto> selectByRulesAndRuleProfileUuids(DbSession dbSession, Collection<Integer> ruleIds, Collection<String> ruleProfileUuids) {
if (ruleIds.isEmpty() || ruleProfileUuids.isEmpty()) {
public Collection<ActiveRuleDto> selectByRulesAndRuleProfileUuids(DbSession dbSession, Collection<String> ruleUuids, Collection<String> ruleProfileUuids) {
if (ruleUuids.isEmpty() || ruleProfileUuids.isEmpty()) {
return emptyList();
}
ActiveRuleMapper mapper = mapper(dbSession);
return executeLargeInputs(ruleIds, ruleIdsChunk -> executeLargeInputs(ruleProfileUuids, chunk -> mapper.selectByRuleIdsAndRuleProfileUuids(ruleIdsChunk, chunk)));
return executeLargeInputs(ruleUuids,
ruleUuidsChunk -> executeLargeInputs(ruleProfileUuids, chunk -> mapper.selectByRuleUuidsAndRuleProfileUuids(ruleUuidsChunk, chunk)));
}

public ActiveRuleDto insert(DbSession dbSession, ActiveRuleDto item) {
checkArgument(item.getProfileUuid() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
checkArgument(item.getRuleId() != null, RULE_IS_NOT_PERSISTED);
checkArgument(item.getRuleUuid() != null, RULE_IS_NOT_PERSISTED);
checkArgument(item.getUuid() == null, ACTIVE_RULE_IS_ALREADY_PERSISTED);

item.setUuid(uuidFactory.create());
@@ -109,7 +110,7 @@ public class ActiveRuleDao implements Dao {

public ActiveRuleDto update(DbSession dbSession, ActiveRuleDto item) {
checkArgument(item.getProfileUuid() != null, QUALITY_PROFILE_IS_NOT_PERSISTED);
checkArgument(item.getRuleId() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED);
checkArgument(item.getRuleUuid() != null, ActiveRuleDao.RULE_IS_NOT_PERSISTED);
checkArgument(item.getUuid() != null, ACTIVE_RULE_IS_NOT_PERSISTED);
mapper(dbSession).update(item);
return item;
@@ -176,7 +177,7 @@ public class ActiveRuleDao implements Dao {
}

public void deleteParamsByRuleParamOfAllOrganizations(DbSession dbSession, RuleParamDto param) {
List<ActiveRuleDto> activeRules = selectByRuleIdOfAllOrganizations(dbSession, param.getRuleId());
List<ActiveRuleDto> activeRules = selectByRuleUuidOfAllOrganizations(dbSession, param.getRuleUuid());
for (ActiveRuleDto activeRule : activeRules) {
for (ActiveRuleParamDto activeParam : selectParamsByActiveRuleUuid(dbSession, activeRule.getUuid())) {
if (activeParam.getKey().equals(param.getName())) {

+ 11
- 7
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java 查看文件

@@ -38,7 +38,7 @@ public class ActiveRuleDto {

private String uuid;
private String profileUuid;
private Integer ruleId;
private String ruleUuid;
private Integer severity;
private String inheritance;

@@ -51,6 +51,10 @@ public class ActiveRuleDto {
private String ruleProfileUuid;
private String securityStandards;

public ActiveRuleDto() {
// nothing to do here
}

public ActiveRuleDto setKey(ActiveRuleKey key) {
this.repository = key.getRuleKey().repository();
this.ruleField = key.getRuleKey().rule();
@@ -84,12 +88,12 @@ public class ActiveRuleDto {
return this;
}

public Integer getRuleId() {
return ruleId;
public String getRuleUuid() {
return ruleUuid;
}

public ActiveRuleDto setRuleId(Integer ruleId) {
this.ruleId = ruleId;
public ActiveRuleDto setRuleUuid(String ruleUuid) {
this.ruleUuid = ruleUuid;
return this;
}

@@ -158,10 +162,10 @@ public class ActiveRuleDto {

public static ActiveRuleDto createFor(QProfileDto profile, RuleDefinitionDto ruleDto) {
requireNonNull(profile.getRulesProfileUuid(), "Profile is not persisted");
requireNonNull(ruleDto.getId(), "Rule is not persisted");
requireNonNull(ruleDto.getUuid(), "Rule is not persisted");
ActiveRuleDto dto = new ActiveRuleDto();
dto.setProfileUuid(profile.getRulesProfileUuid());
dto.setRuleId(ruleDto.getId());
dto.setRuleUuid(ruleDto.getUuid());
dto.setKey(ActiveRuleKey.of(profile, ruleDto.getKey()));
return dto;
}

+ 5
- 5
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java 查看文件

@@ -45,11 +45,11 @@ public interface ActiveRuleMapper {

List<ActiveRuleDto> selectByKeys(@Param("keys") List<ActiveRuleKey> keys);

List<OrgActiveRuleDto> selectByRuleId(@Param("organizationUuid") String organizationUuid, @Param("ruleId") int ruleId);
List<OrgActiveRuleDto> selectByRuleUuid(@Param("organizationUuid") String organizationUuid, @Param("ruleUuid") String ruleUuid);

List<ActiveRuleDto> selectByRuleIdOfAllOrganizations(int ruleId);
List<ActiveRuleDto> selectByRuleUuidOfAllOrganizations(String ruleUuid);

List<OrgActiveRuleDto> selectByRuleIds(@Param("organizationUuid") String organizationUuid, @Param("ruleIds") List<Integer> partitionOfRuleIds);
List<OrgActiveRuleDto> selectByRuleUuids(@Param("organizationUuid") String organizationUuid, @Param("ruleUuids") List<String> partitionOfRuleUuids);

List<OrgActiveRuleDto> selectByProfileUuid(String uuid);

@@ -57,8 +57,8 @@ public interface ActiveRuleMapper {

List<ActiveRuleDto> selectByRuleProfileUuid(@Param("ruleProfileUuid") String uuid);

List<ActiveRuleDto> selectByRuleIdsAndRuleProfileUuids(
@Param("ruleIds") Collection<Integer> ruleIds,
List<ActiveRuleDto> selectByRuleUuidsAndRuleProfileUuids(
@Param("ruleUuids") Collection<String> ruleUuids,
@Param("ruleProfileUuids") Collection<String> ruleProfileUuids);

void insertParameter(ActiveRuleParamDto dto);

+ 7
- 3
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/IndexedActiveRuleDto.java 查看文件

@@ -23,19 +23,23 @@ import javax.annotation.CheckForNull;

public class IndexedActiveRuleDto {
private String uuid;
private int ruleId;
private String ruleUuid;
private int severity;
private String inheritance;
private String repository;
private String key;
private String ruleProfileUuid;

public IndexedActiveRuleDto() {
// nothing to do here
}

public String getUuid() {
return uuid;
}

public int getRuleId() {
return ruleId;
public String getRuleUuid() {
return ruleUuid;
}

public int getSeverity() {

+ 10
- 6
server/sonar-db-dao/src/main/java/org/sonar/db/rule/DeprecatedRuleKeyDto.java 查看文件

@@ -31,9 +31,9 @@ public class DeprecatedRuleKeyDto {
*/
private String uuid;
/**
* the id of the current rule for this deprecated key
* the uuid of the current rule for this deprecated key
*/
private Integer ruleId;
private String ruleUuid;
/**
* repository key that was deprecated
*/
@@ -56,6 +56,10 @@ public class DeprecatedRuleKeyDto {
*/
private String newRuleKey;

public DeprecatedRuleKeyDto() {
// nothing to do here
}

public String getUuid() {
return uuid;
}
@@ -65,12 +69,12 @@ public class DeprecatedRuleKeyDto {
return this;
}

public Integer getRuleId() {
return ruleId;
public String getRuleUuid() {
return ruleUuid;
}

public DeprecatedRuleKeyDto setRuleId(Integer ruleId) {
this.ruleId = ruleId;
public DeprecatedRuleKeyDto setRuleUuid(String ruleUuid) {
this.ruleUuid = ruleUuid;
return this;
}


+ 22
- 20
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java 查看文件

@@ -86,30 +86,30 @@ public class RuleDao implements Dao {
return rule;
}

public Optional<RuleDto> selectById(long id, String organizationUuid, DbSession session) {
RuleDto res = mapper(session).selectById(organizationUuid, id);
public Optional<RuleDto> selectByUuid(String uuid, String organizationUuid, DbSession session) {
RuleDto res = mapper(session).selectByUuid(organizationUuid, uuid);
ensureOrganizationIsSet(organizationUuid, res);
return ofNullable(res);
}

public Optional<RuleDefinitionDto> selectDefinitionById(long id, DbSession session) {
return ofNullable(mapper(session).selectDefinitionById(id));
public Optional<RuleDefinitionDto> selectDefinitionByUuid(String uuid, DbSession session) {
return ofNullable(mapper(session).selectDefinitionByUuid(uuid));
}

public List<RuleDto> selectByIds(DbSession session, String organizationUuid, List<Integer> ids) {
if (ids.isEmpty()) {
public List<RuleDto> selectByUuids(DbSession session, String organizationUuid, List<String> uuids) {
if (uuids.isEmpty()) {
return emptyList();
}
return ensureOrganizationIsSet(
organizationUuid,
executeLargeInputs(ids, chunk -> mapper(session).selectByIds(organizationUuid, chunk)));
executeLargeInputs(uuids, chunk -> mapper(session).selectByUuids(organizationUuid, chunk)));
}

public List<RuleDefinitionDto> selectDefinitionByIds(DbSession session, Collection<Integer> ids) {
if (ids.isEmpty()) {
public List<RuleDefinitionDto> selectDefinitionByUuids(DbSession session, Collection<String> uuids) {
if (uuids.isEmpty()) {
return emptyList();
}
return executeLargeInputs(ids, mapper(session)::selectDefinitionByIds);
return executeLargeInputs(uuids, mapper(session)::selectDefinitionByUuids);
}

public List<RuleDto> selectByKeys(DbSession session, OrganizationDto organization, Collection<RuleKey> keys) {
@@ -168,10 +168,12 @@ public class RuleDao implements Dao {
}

public void insert(DbSession session, RuleDefinitionDto dto) {
checkNotNull(dto.getUuid(), "RuleDefinitionDto has no 'uuid'.");
mapper(session).insertDefinition(dto);
}

public void insert(DbSession session, RuleMetadataDto dto) {
checkNotNull(dto.getRuleUuid(), "RuleMetadataDto has no 'ruleUuid'.");
mapper(session).insertMetadata(dto);
}

@@ -203,12 +205,12 @@ public class RuleDao implements Dao {
});
}

public void scrollIndexingRulesByKeys(DbSession dbSession, Collection<Integer> ruleIds, Consumer<RuleForIndexingDto> consumer) {
public void scrollIndexingRulesByKeys(DbSession dbSession, Collection<String> ruleUuids, Consumer<RuleForIndexingDto> consumer) {
RuleMapper mapper = mapper(dbSession);

executeLargeInputsWithoutOutput(ruleIds,
pageOfRuleIds -> mapper
.selectIndexingRulesByIds(pageOfRuleIds)
executeLargeInputsWithoutOutput(ruleUuids,
pageOfRuleUuids -> mapper
.selectIndexingRulesByUuids(pageOfRuleUuids)
.forEach(consumer));
}

@@ -235,22 +237,22 @@ public class RuleDao implements Dao {
return executeLargeInputs(ruleKeys, mapper(session)::selectParamsByRuleKeys);
}

public List<RuleParamDto> selectRuleParamsByRuleIds(DbSession dbSession, Collection<Integer> ruleIds) {
return executeLargeInputs(ruleIds, mapper(dbSession)::selectParamsByRuleIds);
public List<RuleParamDto> selectRuleParamsByRuleUuids(DbSession dbSession, Collection<String> ruleUuids) {
return executeLargeInputs(ruleUuids, mapper(dbSession)::selectParamsByRuleUuids);
}

public void insertRuleParam(DbSession session, RuleDefinitionDto rule, RuleParamDto param) {
checkNotNull(rule.getId(), "Rule id must be set");
param.setRuleId(rule.getId());
checkNotNull(rule.getUuid(), "Rule uuid must be set");
param.setRuleUuid(rule.getUuid());

param.setUuid(uuidFactory.create());
mapper(session).insertParameter(param);
}

public RuleParamDto updateRuleParam(DbSession session, RuleDefinitionDto rule, RuleParamDto param) {
checkNotNull(rule.getId(), "Rule id must be set");
checkNotNull(rule.getUuid(), "Rule uuid must be set");
checkNotNull(param.getUuid(), "Rule parameter is not yet persisted must be set");
param.setRuleId(rule.getId());
param.setRuleUuid(rule.getUuid());
mapper(session).updateParameter(param);
return param;
}

+ 19
- 22
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDefinitionDto.java 查看文件

@@ -38,7 +38,7 @@ public class RuleDefinitionDto {

private static final Splitter SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();

private Integer id;
private String uuid;
private String repositoryKey;
private String ruleKey;

@@ -75,7 +75,7 @@ public class RuleDefinitionDto {
private boolean isAdHoc;

private String language;
private Integer templateId;
private String templateUuid;
private String defRemediationFunction;
private String defRemediationGapMultiplier;
private String defRemediationBaseEffort;
@@ -92,6 +92,10 @@ public class RuleDefinitionDto {
private long createdAt;
private long updatedAt;

public RuleDefinitionDto() {
// nothing to do here
}

public static Set<String> deserializeTagsString(@Nullable String tags) {
return deserializeStringSet(tags);
}
@@ -126,12 +130,12 @@ public class RuleDefinitionDto {
return this;
}

public Integer getId() {
return id;
public String getUuid() {
return uuid;
}

public RuleDefinitionDto setId(Integer id) {
this.id = id;
public RuleDefinitionDto setUuid(String uuid) {
this.uuid = uuid;
return this;
}

@@ -267,16 +271,16 @@ public class RuleDefinitionDto {
}

@CheckForNull
public Integer getTemplateId() {
return templateId;
public String getTemplateUuid() {
return templateUuid;
}

public boolean isCustomRule() {
return getTemplateId() != null;
return getTemplateUuid() != null;
}

public RuleDefinitionDto setTemplateId(@Nullable Integer templateId) {
this.templateId = templateId;
public RuleDefinitionDto setTemplateUuid(@Nullable String templateUuid) {
this.templateUuid = templateUuid;
return this;
}

@@ -414,27 +418,20 @@ public class RuleDefinitionDto {
return true;
}
RuleDefinitionDto other = (RuleDefinitionDto) obj;
return Objects.equals(id, other.id);
return Objects.equals(this.uuid, other.uuid);
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(id)
.append(this.uuid)
.toHashCode();
}

public static RuleDto createFor(RuleKey key) {
return new RuleDto()
.setId(new HashCodeBuilder(17, 37).append(key.rule()).append(key.repository()).toHashCode())
.setRepositoryKey(key.repository())
.setRuleKey(key.rule());
}

@Override
public String toString() {
return "RuleDefinitionDto{" +
"id=" + id +
"uuid=" + uuid +
", repositoryKey='" + repositoryKey + '\'' +
", ruleKey='" + ruleKey + '\'' +
", description='" + description + '\'' +
@@ -445,7 +442,7 @@ public class RuleDefinitionDto {
", severity=" + severity +
", isTemplate=" + isTemplate +
", language='" + language + '\'' +
", templateId=" + templateId +
", templateUuid=" + templateUuid +
", defRemediationFunction='" + defRemediationFunction + '\'' +
", defRemediationGapMultiplier='" + defRemediationGapMultiplier + '\'' +
", defRemediationBaseEffort='" + defRemediationBaseEffort + '\'' +

+ 9
- 9
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java 查看文件

@@ -62,13 +62,13 @@ public class RuleDto {
return definition.getKey();
}

public Integer getId() {
return definition.getId();
public String getUuid() {
return definition.getUuid();
}

public RuleDto setId(Integer id) {
definition.setId(id);
metadata.setRuleId(id);
public RuleDto setUuid(String uuid) {
definition.setUuid(uuid);
metadata.setRuleUuid(uuid);
return this;
}

@@ -257,12 +257,12 @@ public class RuleDto {
}

@CheckForNull
public Integer getTemplateId() {
return definition.getTemplateId();
public String getTemplateUuid() {
return definition.getTemplateUuid();
}

public RuleDto setTemplateId(@Nullable Integer templateId) {
definition.setTemplateId(templateId);
public RuleDto setTemplateUuid(@Nullable String templateUuid) {
definition.setTemplateUuid(templateUuid);
return this;
}


+ 5
- 5
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleExtensionForIndexingDto.java 查看文件

@@ -28,7 +28,7 @@ public class RuleExtensionForIndexingDto {

private static final Splitter TAGS_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();

private int ruleId;
private String ruleUuid;
private String pluginName;
private String pluginRuleKey;
private String organizationUuid;
@@ -52,12 +52,12 @@ public class RuleExtensionForIndexingDto {
return this;
}

public int getRuleId() {
return ruleId;
public String getRuleUuid() {
return ruleUuid;
}

public void setRuleId(int ruleId) {
this.ruleId = ruleId;
public void setRuleUuid(String ruleUuid) {
this.ruleUuid = ruleUuid;
}

public String getOrganizationUuid() {

+ 8
- 4
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java 查看文件

@@ -27,7 +27,7 @@ import org.sonar.api.rules.RuleType;

public class RuleForIndexingDto {

private Integer id;
private String uuid;
private String repository;
private String pluginRuleKey;
private String name;
@@ -47,8 +47,12 @@ public class RuleForIndexingDto {
private long createdAt;
private long updatedAt;

public Integer getId() {
return id;
public RuleForIndexingDto() {
// nothing to do here
}

public String getUuid() {
return uuid;
}

public String getRepository() {
@@ -110,7 +114,7 @@ public class RuleForIndexingDto {
public int getType() {
return type;
}
public boolean isExternal() {
return isExternal;
}

+ 6
- 6
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java 查看文件

@@ -35,13 +35,13 @@ public interface RuleMapper {

void selectEnabled(ResultHandler<RuleDefinitionDto> resultHandler);

RuleDto selectById(@Param("organizationUuid") String organizationUuid, @Param("id") long id);
RuleDto selectByUuid(@Param("organizationUuid") String organizationUuid, @Param("uuid") String uuid);

RuleDefinitionDto selectDefinitionById(long id);
RuleDefinitionDto selectDefinitionByUuid(String uuid);

List<RuleDto> selectByIds(@Param("organizationUuid") String organizationUuid, @Param("ids") List<Integer> ids);
List<RuleDto> selectByUuids(@Param("organizationUuid") String organizationUuid, @Param("uuids") List<String> uuids);

List<RuleDefinitionDto> selectDefinitionByIds(@Param("ids") List<Integer> ids);
List<RuleDefinitionDto> selectDefinitionByUuids(@Param("uuids") List<String> uuids);

RuleDto selectByKey(@Param("organizationUuid") String organizationUuid, @Param("ruleKey") RuleKey ruleKey);

@@ -55,7 +55,7 @@ public interface RuleMapper {

void scrollIndexingRules(ResultHandler<RuleForIndexingDto> handler);

List<RuleForIndexingDto> selectIndexingRulesByIds(@Param("ruleIds") List<Integer> ruleIds);
List<RuleForIndexingDto> selectIndexingRulesByUuids(@Param("ruleUuids") List<String> ruleUuids);

void scrollIndexingRuleExtensions(ResultHandler<RuleExtensionForIndexingDto> handler);

@@ -75,7 +75,7 @@ public interface RuleMapper {

void updateMetadata(RuleMetadataDto ruleMetadataDto);

List<RuleParamDto> selectParamsByRuleIds(@Param("ruleIds") List<Integer> ruleIds);
List<RuleParamDto> selectParamsByRuleUuids(@Param("ruleUuids") List<String> ruleUuids);

List<RuleParamDto> selectParamsByRuleKey(RuleKey ruleKey);


+ 10
- 6
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java 查看文件

@@ -31,7 +31,7 @@ import org.sonar.api.rules.RuleType;
import static com.google.common.base.Preconditions.checkArgument;

public class RuleMetadataDto {
private int ruleId;
private String ruleUuid;
private String organizationUuid;
private String noteData;
private String noteUserUuid;
@@ -42,6 +42,10 @@ public class RuleMetadataDto {
private String remediationBaseEffort;
private String tags;

public RuleMetadataDto() {
// nothing to do here
}

/**
* Name of on ad hoc rule.
* When {@link RuleDefinitionDto#isAdHoc} is true, this field should always be set
@@ -68,12 +72,12 @@ public class RuleMetadataDto {
private long createdAt;
private long updatedAt;

public int getRuleId() {
return ruleId;
public String getRuleUuid() {
return ruleUuid;
}

public RuleMetadataDto setRuleId(int ruleId) {
this.ruleId = ruleId;
public RuleMetadataDto setRuleUuid(String ruleUuid) {
this.ruleUuid = ruleUuid;
return this;
}

@@ -245,7 +249,7 @@ public class RuleMetadataDto {
@Override
public String toString() {
return "RuleMetadataDto{" +
"ruleId=" + ruleId +
"ruleUuid=" + ruleUuid +
", organizationUuid='" + organizationUuid + '\'' +
", noteData='" + noteData + '\'' +
", noteUserUuid='" + noteUserUuid + '\'' +

+ 10
- 6
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleParamDto.java 查看文件

@@ -29,12 +29,16 @@ import static com.google.common.base.Preconditions.checkArgument;
public class RuleParamDto {

private String uuid;
private Integer ruleId;
private String ruleUuid;
private String name;
private String type;
private String defaultValue;
private String description;

public RuleParamDto() {
// nothing to do here
}

public String getUuid() {
return uuid;
}
@@ -44,12 +48,12 @@ public class RuleParamDto {
return this;
}

public Integer getRuleId() {
return ruleId;
public String getRuleUuid() {
return ruleUuid;
}

public RuleParamDto setRuleId(Integer ruleId) {
this.ruleId = ruleId;
public RuleParamDto setRuleUuid(String ruleUuid) {
this.ruleUuid = ruleUuid;
return this;
}

@@ -100,7 +104,7 @@ public class RuleParamDto {

public static RuleParamDto createFor(RuleDefinitionDto rule) {
// Should eventually switch to RuleKey (RuleKey is available before insert)
return new RuleParamDto().setRuleId(rule.getId());
return new RuleParamDto().setRuleUuid(rule.getUuid());
}

}

+ 13
- 11
server/sonar-db-dao/src/main/resources/org/sonar/db/issue/IssueMapper.xml 查看文件

@@ -6,7 +6,7 @@

<sql id="issueColumns">
i.kee as kee,
i.rule_id as ruleId,
i.rule_uuid as ruleUuid,
i.severity as severity,
i.manual_severity as manualSeverity,
i.message as message,
@@ -99,11 +99,13 @@


<insert id="insert" parameterType="Issue" useGeneratedKeys="false">
INSERT INTO issues (kee, rule_id, severity, manual_severity,
INSERT INTO issues (kee, rule_uuid, severity, manual_severity,
message, line, locations, gap, effort, status, tags,
resolution, checksum, assignee, author_login, issue_attributes, issue_creation_date, issue_update_date,
issue_close_date, created_at, updated_at, component_uuid, project_uuid, issue_type)
VALUES (#{kee,jdbcType=VARCHAR}, #{ruleId,jdbcType=INTEGER},
VALUES (
#{kee,jdbcType=VARCHAR},
#{ruleUuid,jdbcType=VARCHAR},
#{severity,jdbcType=VARCHAR},
#{manualSeverity,jdbcType=BOOLEAN}, #{message,jdbcType=VARCHAR}, #{line,jdbcType=INTEGER},
#{locations,jdbcType=BINARY},
@@ -179,7 +181,7 @@
select
<include refid="issueColumns"/>
from issues i
inner join rules r on r.id=i.rule_id
inner join rules r on r.uuid=i.rule_uuid
inner join components p on p.uuid=i.component_uuid
inner join components root on root.uuid=i.project_uuid
where i.kee=#{kee,jdbcType=VARCHAR}
@@ -189,7 +191,7 @@
select
<include refid="issueColumns"/>
from issues i
inner join rules r on r.id=i.rule_id
inner join rules r on r.uuid=i.rule_uuid
inner join components p on p.uuid=i.component_uuid
inner join components root on root.uuid=i.project_uuid
where
@@ -201,7 +203,7 @@
select
<include refid="issueColumns"/>
from issues i
inner join rules r on r.id=i.rule_id
inner join rules r on r.uuid=i.rule_uuid
inner join components p on p.uuid=i.component_uuid
inner join components root on root.uuid=i.project_uuid
where
@@ -217,7 +219,7 @@
ic.change_data as closedChangeData
from issues i
inner join rules r on
r.id = i.rule_id
r.uuid = i.rule_uuid
inner join components p on
p.uuid = i.component_uuid
inner join components root on
@@ -259,7 +261,7 @@
select
<include refid="issueColumns"/>
from issues i
inner join rules r on r.id=i.rule_id
inner join rules r on r.uuid=i.rule_uuid
inner join components p on p.uuid=i.component_uuid
inner join components root on root.uuid=i.project_uuid
where i.kee in
@@ -272,7 +274,7 @@
select
<include refid="issueColumns"/>
from issues i
inner join rules r on r.id=i.rule_id
inner join rules r on r.uuid=i.rule_uuid
inner join components p on p.uuid=i.component_uuid
inner join components root on root.uuid=i.project_uuid
where
@@ -296,7 +298,7 @@
b.kee as branchKey,
b.key_type as keyType
from issues i
inner join rules r on r.id = i.rule_id
inner join rules r on r.uuid = i.rule_uuid
inner join project_branches b on i.project_uuid = b.uuid
where i.component_uuid in
<foreach collection="list" open="(" close=")" item="key" separator=",">
@@ -309,7 +311,7 @@
select
<include refid="issueColumns"/>
from issues i
inner join rules r on r.id = i.rule_id
inner join rules r on r.uuid = i.rule_uuid
inner join components p on p.uuid = i.component_uuid
inner join components root on root.uuid = i.project_uuid
where

+ 23
- 23
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml 查看文件

@@ -6,7 +6,7 @@
<sql id="activeRuleColumns">
a.uuid,
a.profile_uuid as "profileUuid",
a.rule_id as "ruleId",
a.rule_uuid as "ruleUuid",
a.failure_level as "severity",
a.inheritance as "inheritance",
r.plugin_rule_key as "rulefield",
@@ -19,7 +19,7 @@
<sql id="orgActiveRuleColumns">
a.uuid,
a.profile_uuid as "profileUuid",
a.rule_id as "ruleId",
a.rule_uuid as "ruleUuid",
a.failure_level as "severity",
a.inheritance as "inheritance",
r.plugin_rule_key as "rulefield",
@@ -34,22 +34,22 @@

<sql id="activeRuleKeyJoin">
inner join rules_profiles rp on rp.uuid = a.profile_uuid
inner join rules r on r.id = a.rule_id
inner join rules r on r.uuid = a.rule_uuid
</sql>

<insert id="insert" parameterType="ActiveRule">
insert into active_rules (
uuid,
profile_uuid,
rule_id,
rule_uuid,
failure_level,
inheritance,
created_at,
updated_at
) values (
#{uuid, jdbcType=VARCHAR},
#{profileUuid, jdbcType=BIGINT},
#{ruleId, jdbcType=BIGINT},
#{profileUuid, jdbcType=VARCHAR},
#{ruleUuid, jdbcType=VARCHAR},
#{severity, jdbcType=INTEGER},
#{inheritance, jdbcType=VARCHAR},
#{createdAt, jdbcType=BIGINT},
@@ -122,7 +122,7 @@
from active_rules a
inner join rules_profiles rp on rp.uuid = a.profile_uuid
inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid
inner join rules r on r.id = a.rule_id and r.status != 'REMOVED'
inner join rules r on r.uuid = a.rule_uuid and r.status != 'REMOVED'
where oqp.uuid = #{id, jdbcType=VARCHAR}
</select>

@@ -132,7 +132,7 @@
from active_rules a
inner join rules_profiles rp on rp.uuid = a.profile_uuid
inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid
inner join rules r on r.id = a.rule_id and r.status != 'REMOVED'
inner join rules r on r.uuid = a.rule_uuid and r.status != 'REMOVED'
where
r.rule_type in <foreach collection="types" item="type" separator="," open="(" close=")">#{type, jdbcType=INTEGER}</foreach>
and oqp.uuid in <foreach collection="profileUuids" item="uuid" separator="," open="(" close=")">#{uuid, jdbcType=VARCHAR}</foreach>
@@ -147,48 +147,48 @@
rp.uuid = #{ruleProfileUuid, jdbcType=VARCHAR}
</select>

<select id="selectByRuleIdsAndRuleProfileUuids" parameterType="map" resultType="org.sonar.db.qualityprofile.ActiveRuleDto">
<select id="selectByRuleUuidsAndRuleProfileUuids" parameterType="map" resultType="org.sonar.db.qualityprofile.ActiveRuleDto">
select
<include refid="activeRuleColumns"/>
from active_rules a
<include refid="activeRuleKeyJoin"/>
where
r.id in <foreach collection="ruleIds" item="ruleId" separator="," open="(" close=")">#{ruleId, jdbcType=BIGINT}</foreach>
r.uuid in <foreach collection="ruleUuids" item="ruleUuid" separator="," open="(" close=")">#{ruleUuid, jdbcType=VARCHAR}</foreach>
and rp.uuid in <foreach collection="ruleProfileUuids" item="ruleProfileUuid" separator="," open="(" close=")">#{ruleProfileUuid, jdbcType=VARCHAR}</foreach>
</select>

<select id="selectByRuleId" parameterType="map" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto">
<select id="selectByRuleUuid" parameterType="map" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto">
select
<include refid="orgActiveRuleColumns"/>
from active_rules a
inner join rules_profiles rp on rp.uuid = a.profile_uuid
inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid
inner join rules r on r.id = a.rule_id
inner join rules r on r.uuid = a.rule_uuid
where
a.rule_id = #{ruleId, jdbcType=BIGINT}
a.rule_uuid = #{ruleUuid, jdbcType=VARCHAR}
and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
</select>

<select id="selectByRuleIdOfAllOrganizations" parameterType="Integer" resultType="ActiveRule">
<select id="selectByRuleUuidOfAllOrganizations" parameterType="String" resultType="ActiveRule">
select
<include refid="activeRuleColumns"/>
from active_rules a
<include refid="activeRuleKeyJoin"/>
where
a.rule_id = #{ruleId, jdbcType=BIGINT}
r.uuid = #{ruleUuid, jdbcType=VARCHAR}
</select>

<select id="selectByRuleIds" parameterType="List" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto">
<select id="selectByRuleUuids" parameterType="List" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto">
select
<include refid="orgActiveRuleColumns"/>
from active_rules a
inner join rules_profiles rp on rp.uuid = a.profile_uuid
inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid
inner join rules r on r.id = a.rule_id
inner join rules r on r.uuid = a.rule_uuid
where
a.rule_id in
<foreach collection="ruleIds" item="ruleId" separator="," open="(" close=")">
#{ruleId, jdbcType=BIGINT}
a.rule_uuid in
<foreach collection="ruleUuids" item="ruleUuid" separator="," open="(" close=")">
#{ruleUuid, jdbcType=VARCHAR}
</foreach>
and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
</select>
@@ -278,7 +278,7 @@
from active_rules ar
inner join rules_profiles rp on rp.uuid = ar.profile_uuid
inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid
inner join rules r on r.id = ar.rule_id
inner join rules r on r.uuid = ar.rule_uuid
<where>
oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
and <foreach collection="profileUuids" item="profileUuid" open="(" separator=" or " close=")">
@@ -319,13 +319,13 @@
ar.uuid as "uuid",
ar.failure_level as "severity",
ar.inheritance as "inheritance",
r.id as "ruleId",
r.uuid as "ruleUuid",
r.plugin_name as "repository",
r.plugin_rule_key as "key",
rp.uuid as "ruleProfileUuid"
from active_rules ar
inner join rules_profiles rp on rp.uuid = ar.profile_uuid
inner join rules r on r.id = ar.rule_id
inner join rules r on r.uuid = ar.rule_uuid
</sql>
</mapper>


+ 3
- 3
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml 查看文件

@@ -30,9 +30,9 @@
from active_rules a
inner join rules_profiles rp on rp.uuid = a.profile_uuid
inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid
inner join rules r on r.id = a.rule_id and r.status != 'REMOVED'
left join rules rt on rt.id = r.template_id
left join rules_metadata rm on rm.rule_id = r.id
inner join rules r on r.uuid = a.rule_uuid and r.status != 'REMOVED'
left join rules rt on rt.uuid = r.template_uuid
left join rules_metadata rm on rm.rule_uuid = r.uuid
where oqp.uuid = #{id, jdbcType=VARCHAR}
</select>


+ 2
- 2
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml 查看文件

@@ -157,7 +157,7 @@
AND rp.language IN <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach>
AND NOT EXISTS (
SELECT 1 FROM active_rules ar
INNER JOIN rules r ON r.id = ar.rule_id AND r.status &lt;&gt; 'REMOVED'
INNER JOIN rules r ON r.uuid = ar.rule_uuid AND r.status &lt;&gt; 'REMOVED'
WHERE profile_uuid = rp.uuid
)
</select>
@@ -181,7 +181,7 @@
where rp.is_built_in = ${_true}
AND EXISTS (
SELECT 1 FROM active_rules ar
INNER JOIN rules r ON r.id = ar.rule_id AND r.status &lt;&gt; 'REMOVED'
INNER JOIN rules r ON r.uuid = ar.rule_uuid AND r.status &lt;&gt; 'REMOVED'
WHERE profile_uuid=rp.uuid
)
</select>

+ 50
- 46
server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml 查看文件

@@ -4,7 +4,7 @@
<mapper namespace="org.sonar.db.rule.RuleMapper">

<sql id="selectDefinitionColumns">
r.id,
r.uuid,
r.plugin_rule_key as "ruleKey",
r.plugin_name as "repositoryKey",
r.description,
@@ -17,7 +17,7 @@
r.is_external as "isExternal",
r.is_ad_hoc as "isAdHoc",
r.language as "language",
r.template_id as "templateId",
r.template_uuid as "templateUuid",
r.def_remediation_function as "defRemediationFunction",
r.def_remediation_gap_mult as "defRemediationGapMultiplier",
r.def_remediation_base_effort as "defRemediationBaseEffort",
@@ -57,7 +57,7 @@

<sql id="outerJoinRulesMetadata">
left outer join rules_metadata rm on
rm.rule_id = r.id
rm.rule_uuid = r.uuid
and rm.organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
</sql>

@@ -85,45 +85,45 @@
r.status != 'REMOVED'
</select>

<select id="selectById" parameterType="map" resultType="Rule">
<select id="selectByUuid" parameterType="map" resultType="Rule">
select
<include refid="selectJoinedTablesColumns"/>
from
rules r
<include refid="outerJoinRulesMetadata"/>
where
r.id=#{id,jdbcType=INTEGER}
r.uuid=#{uuid,jdbcType=VARCHAR}
</select>

<select id="selectDefinitionById" parameterType="Long" resultType="org.sonar.db.rule.RuleDefinitionDto">
<select id="selectDefinitionByUuid" parameterType="String" resultType="org.sonar.db.rule.RuleDefinitionDto">
select
<include refid="selectRuleTableColumns"/>
from
rules r
where
r.id=#{id,jdbcType=INTEGER}
r.uuid=#{uuid,jdbcType=VARCHAR}
</select>

<select id="selectByIds" parameterType="map" resultType="Rule">
<select id="selectByUuids" parameterType="map" resultType="Rule">
select
<include refid="selectJoinedTablesColumns"/>
from
rules r
<include refid="outerJoinRulesMetadata"/>
where
<foreach collection="ids" index="index" item="id" open="" separator=" or " close="">
r.id=#{id,jdbcType=INTEGER}
<foreach collection="uuids" index="index" item="uuid" open="" separator=" or " close="">
r.uuid=#{uuid,jdbcType=VARCHAR}
</foreach>
</select>

<select id="selectDefinitionByIds" parameterType="map" resultType="org.sonar.db.rule.RuleDefinitionDto">
<select id="selectDefinitionByUuids" parameterType="map" resultType="org.sonar.db.rule.RuleDefinitionDto">
select
<include refid="selectRuleTableColumns"/>
from
rules r
where
<foreach collection="ids" index="index" item="id" open="" separator=" or " close="">
r.id=#{id,jdbcType=INTEGER}
<foreach collection="uuids" index="index" item="uuid" open="" separator=" or " close="">
r.uuid=#{uuid,jdbcType=VARCHAR}
</foreach>
</select>

@@ -152,7 +152,7 @@
<include refid="sqlSelectIndexingRuleExtensions" />
and
<foreach collection="ruleExtensionIds" index="index" item="ruleExtId" open="" separator=" or " close="">
( r.id = #{ruleExtId.ruleId, jdbcType=INTEGER} and
( r.uuid = #{ruleExtId.ruleUuid, jdbcType=VARCHAR} and
rm.organization_uuid = #{ruleExtId.organizationUuid, jdbcType=VARCHAR} )
</foreach>
</select>
@@ -163,13 +163,13 @@

<sql id="sqlSelectIndexingRuleExtensions">
select
r.id as "ruleId",
r.uuid as "ruleUuid",
r.plugin_name as "pluginName",
r.plugin_rule_key as "pluginRuleKey",
rm.organization_uuid as "organizationUuid",
rm.tags as "tags"
from rules r
inner join rules_metadata rm on rm.rule_id = r.id
inner join rules_metadata rm on rm.rule_uuid = r.uuid
where
rm.tags is not null and
rm.tags != ''
@@ -177,20 +177,20 @@

<sql id="sqlSelectIndexingRuleExtensions" databaseId="oracle">
select
r.id as "ruleId",
r.uuid as "ruleUuid",
r.plugin_name as "pluginName",
r.plugin_rule_key as "pluginRuleKey",
rm.organization_uuid as "organizationUuid",
rm.tags as "tags"
from rules r
inner join rules_metadata rm on rm.rule_id = r.id
inner join rules_metadata rm on rm.rule_uuid = r.uuid
where
rm.tags is not null
</sql>

<select id="selectMetadataByKey" parameterType="map" resultType="org.sonar.db.rule.RuleMetadataDto">
select
rm.rule_id as "ruleId",
rm.rule_uuid as "ruleUuid",
rm.organization_uuid as "organizationUuid",
rm.note_data as "noteData",
rm.note_user_uuid as "noteUserUuid",
@@ -208,7 +208,7 @@
rm.updated_at as "updatedAt"
from
rules_metadata rm
inner join rules r on rm.rule_id = r.id
inner join rules r on rm.rule_uuid = r.uuid
where
r.plugin_name=#{ruleKey.repository,jdbcType=VARCHAR}
and r.plugin_rule_key=#{ruleKey.rule,jdbcType=VARCHAR}
@@ -238,21 +238,23 @@
</foreach>
</select>

<select id="selectIndexingRulesByIds" parameterType="map" resultType="org.sonar.db.rule.RuleForIndexingDto">
<select id="selectIndexingRulesByUuids" parameterType="map" resultType="org.sonar.db.rule.RuleForIndexingDto">
<include refid="sqlSelectIndexingRules"/>
where
<foreach collection="ruleIds" index="index" item="ruleId" open="" separator=" or " close="">
r.id=#{ruleId,jdbcType=INTEGER}
<foreach collection="ruleUuids" index="index" item="ruleUuid" open="" separator=" or " close="">
r.uuid=#{ruleUuid,jdbcType=VARCHAR}
</foreach>
order by r.created_at asc
</select>

<select id="scrollIndexingRules" resultType="org.sonar.db.rule.RuleForIndexingDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
<include refid="sqlSelectIndexingRules"/>
order by r.created_at asc
</select>

<sql id="sqlSelectIndexingRules">
select
r.id as "id",
r.uuid as "uuid",
r.plugin_name as "repository",
r.plugin_rule_key as "pluginRuleKey",
r.name as "name",
@@ -272,7 +274,7 @@
r.created_at as "createdAt",
r.updated_at as "updatedAt"
from rules r
left outer join rules t on t.id = r.template_id
left outer join rules t on t.uuid = r.template_uuid
</sql>

<select id="selectByQuery" parameterType="map" resultType="Rule">
@@ -308,8 +310,9 @@
and r.language in <foreach collection="languages" item="language" separator="," open="(" close=")">#{language, jdbcType=VARCHAR}</foreach>
</select>

<insert id="insertDefinition" parameterType="org.sonar.db.rule.RuleDefinitionDto" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
<insert id="insertDefinition" parameterType="org.sonar.db.rule.RuleDefinitionDto" useGeneratedKeys="false">
insert into rules (
uuid,
plugin_key,
plugin_rule_key,
plugin_name,
@@ -323,7 +326,7 @@
is_external,
is_ad_hoc,
language,
template_id,
template_uuid,
def_remediation_function,
def_remediation_gap_mult,
def_remediation_base_effort,
@@ -336,6 +339,7 @@
updated_at
)
values (
#{uuid,jdbcType=VARCHAR},
#{pluginKey,jdbcType=VARCHAR},
#{ruleKey,jdbcType=VARCHAR},
#{repositoryKey,jdbcType=VARCHAR},
@@ -349,7 +353,7 @@
#{isExternal,jdbcType=BOOLEAN},
#{isAdHoc,jdbcType=BOOLEAN},
#{language,jdbcType=VARCHAR},
#{templateId,jdbcType=INTEGER},
#{templateUuid,jdbcType=VARCHAR},
#{defRemediationFunction,jdbcType=VARCHAR},
#{defRemediationGapMultiplier,jdbcType=VARCHAR},
#{defRemediationBaseEffort,jdbcType=VARCHAR},
@@ -378,7 +382,7 @@
is_external=#{isExternal,jdbcType=BOOLEAN},
is_ad_hoc=#{isAdHoc,jdbcType=BOOLEAN},
language=#{language,jdbcType=VARCHAR},
template_id=#{templateId,jdbcType=INTEGER},
template_uuid=#{templateUuid,jdbcType=VARCHAR},
def_remediation_function=#{defRemediationFunction,jdbcType=VARCHAR},
def_remediation_gap_mult=#{defRemediationGapMultiplier,jdbcType=VARCHAR},
def_remediation_base_effort=#{defRemediationBaseEffort,jdbcType=VARCHAR},
@@ -389,7 +393,7 @@
rule_type=#{type,jdbcType=TINYINT},
updated_at=#{updatedAt,jdbcType=BIGINT}
where
id=#{id,jdbcType=INTEGER}
uuid=#{uuid,jdbcType=VARCHAR}
</update>

<select id="countMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto" resultType="int">
@@ -398,13 +402,13 @@
from
rules_metadata rm
where
rm.rule_id=#{ruleId,jdbcType=INTEGER}
rm.rule_uuid=#{ruleUuid,jdbcType=VARCHAR}
and rm.organization_uuid=#{organizationUuid,jdbcType=VARCHAR}
</select>

<insert id="insertMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto">
insert into rules_metadata (
rule_id,
rule_uuid,
organization_uuid,
note_data,
note_user_uuid,
@@ -422,7 +426,7 @@
updated_at
)
values (
#{ruleId,jdbcType=INTEGER},
#{ruleUuid,jdbcType=VARCHAR},
#{organizationUuid,jdbcType=VARCHAR},
#{noteData,jdbcType=CLOB},
#{noteUserUuid,jdbcType=VARCHAR},
@@ -457,7 +461,7 @@
ad_hoc_type=#{adHocType,jdbcType=TINYINT},
updated_at=#{updatedAt,jdbcType=BIGINT}
where
rule_id=#{ruleId,jdbcType=INTEGER}
rule_uuid=#{ruleUuid,jdbcType=VARCHAR}
and organization_uuid=#{organizationUuid,jdbcType=VARCHAR}
</update>

@@ -470,21 +474,21 @@

<sql id="paramColumns">
p.uuid as "uuid",
p.rule_id as "ruleId",
p.rule_uuid as "ruleUuid",
p.name as "name",
p.param_type as "type",
p.default_value as "defaultValue",
p.description as "description"
</sql>

<select id="selectParamsByRuleIds" resultType="RuleParam">
<select id="selectParamsByRuleUuids" resultType="RuleParam">
select
<include refid="paramColumns"/>
from
rules_parameters p
where
<foreach item="id" index="index" collection="ruleIds" open="(" separator=" or " close=")">
p.rule_id=#{id,jdbcType=INTEGER}
<foreach item="uuid" index="index" collection="ruleUuids" open="(" separator=" or " close=")">
p.rule_uuid=#{uuid,jdbcType=VARCHAR}
</foreach>
</select>

@@ -494,7 +498,7 @@
from
rules_parameters p, rules r
where
p.rule_id=r.id
p.rule_uuid=r.uuid
and r.plugin_name=#{repository,jdbcType=VARCHAR}
and r.plugin_rule_key=#{rule,jdbcType=VARCHAR}
</select>
@@ -505,7 +509,7 @@
from
rules_parameters p
inner join rules r on
r.id=p.rule_id
r.uuid=p.rule_uuid
where
<foreach collection="ruleKeys" index="index" item="ruleKey" open="" separator=" or " close="">
(r.plugin_name=#{ruleKey.repository,jdbcType=VARCHAR} AND r.plugin_rule_key=#{ruleKey.rule,jdbcType=VARCHAR})
@@ -522,7 +526,7 @@
<insert id="insertParameter" parameterType="RuleParam" useGeneratedKeys="false">
insert into rules_parameters (
uuid,
rule_id,
rule_uuid,
name,
param_type,
default_value,
@@ -530,7 +534,7 @@
)
values (
#{uuid,jdbcType=VARCHAR},
#{ruleId,jdbcType=INTEGER},
#{ruleUuid,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR},
#{type,jdbcType=VARCHAR},
#{defaultValue,jdbcType=VARCHAR},
@@ -550,7 +554,7 @@
<select id="selectAllDeprecatedRuleKeys" resultType="org.sonar.db.rule.DeprecatedRuleKeyDto">
SELECT
drk.uuid,
drk.rule_id as "ruleId",
drk.rule_uuid as "ruleUuid",
drk.old_repository_key as "oldRepositoryKey",
drk.old_rule_key as "oldRuleKey",
r.plugin_rule_key as "newRuleKey",
@@ -558,7 +562,7 @@
drk.created_at as "createdAt"
FROM
deprecated_rule_keys drk
LEFT OUTER JOIN rules r on r.id = drk.rule_id
LEFT OUTER JOIN rules r on r.uuid = drk.rule_uuid
</select>

<delete id="deleteDeprecatedRuleKeys">
@@ -573,14 +577,14 @@
<insert id="insertDeprecatedRuleKey" parameterType="org.sonar.db.rule.DeprecatedRuleKeyDto" keyColumn="uuid" useGeneratedKeys="false" keyProperty="uuid">
INSERT INTO deprecated_rule_keys (
uuid,
rule_id,
rule_uuid,
old_repository_key,
old_rule_key,
created_at
)
values (
#{uuid,jdbcType=VARCHAR},
#{ruleId,jdbcType=INTEGER},
#{ruleUuid,jdbcType=VARCHAR},
#{oldRepositoryKey,jdbcType=VARCHAR},
#{oldRuleKey,jdbcType=VARCHAR},
#{createdAt,jdbcType=BIGINT}

+ 20
- 20
server/sonar-db-dao/src/schema/schema-sq.ddl 查看文件

@@ -24,16 +24,16 @@ ALTER TABLE "ACTIVE_RULE_PARAMETERS" ADD CONSTRAINT "PK_ACTIVE_RULE_PARAMETERS"
CREATE INDEX "ARP_ACTIVE_RULE_UUID" ON "ACTIVE_RULE_PARAMETERS"("ACTIVE_RULE_UUID");

CREATE TABLE "ACTIVE_RULES"(
"RULE_ID" INTEGER NOT NULL,
"FAILURE_LEVEL" INTEGER NOT NULL,
"INHERITANCE" VARCHAR(10),
"CREATED_AT" BIGINT,
"UPDATED_AT" BIGINT,
"UUID" VARCHAR(40) NOT NULL,
"PROFILE_UUID" VARCHAR(40) NOT NULL
"PROFILE_UUID" VARCHAR(40) NOT NULL,
"RULE_UUID" VARCHAR(40) NOT NULL
);
ALTER TABLE "ACTIVE_RULES" ADD CONSTRAINT "PK_ACTIVE_RULES" PRIMARY KEY("UUID");
CREATE UNIQUE INDEX "UNIQ_PROFILE_RULE_IDS" ON "ACTIVE_RULES"("PROFILE_UUID", "RULE_ID");
CREATE UNIQUE INDEX "UNIQ_PROFILE_RULE_UUIDS" ON "ACTIVE_RULES"("PROFILE_UUID", "RULE_UUID");

CREATE TABLE "ALM_APP_INSTALLS"(
"UUID" VARCHAR(40) NOT NULL,
@@ -226,14 +226,14 @@ CREATE UNIQUE INDEX "UNIQ_DEFAULT_QPROFILES_UUID" ON "DEFAULT_QPROFILES"("QPROFI

CREATE TABLE "DEPRECATED_RULE_KEYS"(
"UUID" VARCHAR(40) NOT NULL,
"RULE_ID" INTEGER NOT NULL,
"OLD_REPOSITORY_KEY" VARCHAR(255) NOT NULL,
"OLD_RULE_KEY" VARCHAR(200) NOT NULL,
"CREATED_AT" BIGINT NOT NULL
"CREATED_AT" BIGINT NOT NULL,
"RULE_UUID" VARCHAR(40) NOT NULL
);
ALTER TABLE "DEPRECATED_RULE_KEYS" ADD CONSTRAINT "PK_DEPRECATED_RULE_KEYS" PRIMARY KEY("UUID");
CREATE UNIQUE INDEX "UNIQ_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS"("OLD_REPOSITORY_KEY", "OLD_RULE_KEY");
CREATE INDEX "RULE_ID_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS"("RULE_ID");
CREATE INDEX "RULE_UUID_DEPRECATED_RULE_KEYS" ON "DEPRECATED_RULE_KEYS"("RULE_UUID");

CREATE TABLE "DUPLICATIONS_INDEX"(
"ANALYSIS_UUID" VARCHAR(50) NOT NULL,
@@ -377,7 +377,6 @@ CREATE INDEX "ISSUE_CHANGES_KEE" ON "ISSUE_CHANGES"("KEE");

CREATE TABLE "ISSUES"(
"KEE" VARCHAR(50) NOT NULL,
"RULE_ID" INTEGER,
"SEVERITY" VARCHAR(10),
"MANUAL_SEVERITY" BOOLEAN NOT NULL,
"MESSAGE" VARCHAR(4000),
@@ -402,7 +401,8 @@ CREATE TABLE "ISSUES"(
"PROJECT_UUID" VARCHAR(50),
"LOCATIONS" BLOB,
"ISSUE_TYPE" TINYINT,
"FROM_HOTSPOT" BOOLEAN
"FROM_HOTSPOT" BOOLEAN,
"RULE_UUID" VARCHAR(40)
);
ALTER TABLE "ISSUES" ADD CONSTRAINT "PK_ISSUES" PRIMARY KEY("KEE");
CREATE INDEX "ISSUES_ASSIGNEE" ON "ISSUES"("ASSIGNEE");
@@ -411,8 +411,8 @@ CREATE INDEX "ISSUES_CREATION_DATE" ON "ISSUES"("ISSUE_CREATION_DATE");
CREATE UNIQUE INDEX "ISSUES_KEE" ON "ISSUES"("KEE");
CREATE INDEX "ISSUES_PROJECT_UUID" ON "ISSUES"("PROJECT_UUID");
CREATE INDEX "ISSUES_RESOLUTION" ON "ISSUES"("RESOLUTION");
CREATE INDEX "ISSUES_RULE_ID" ON "ISSUES"("RULE_ID");
CREATE INDEX "ISSUES_UPDATED_AT" ON "ISSUES"("UPDATED_AT");
CREATE INDEX "ISSUES_RULE_UUID" ON "ISSUES"("RULE_UUID");

CREATE TABLE "LIVE_MEASURES"(
"UUID" VARCHAR(40) NOT NULL,
@@ -799,7 +799,6 @@ CREATE TABLE "RULE_REPOSITORIES"(
ALTER TABLE "RULE_REPOSITORIES" ADD CONSTRAINT "PK_RULE_REPOSITORIES" PRIMARY KEY("KEE");

CREATE TABLE "RULES"(
"ID" INTEGER NOT NULL AUTO_INCREMENT (1,1),
"NAME" VARCHAR(200),
"PLUGIN_RULE_KEY" VARCHAR(200) NOT NULL,
"PLUGIN_KEY" VARCHAR(200),
@@ -808,7 +807,6 @@ CREATE TABLE "RULES"(
"SCOPE" VARCHAR(20) NOT NULL,
"DESCRIPTION" CLOB(2147483647),
"PRIORITY" INTEGER,
"TEMPLATE_ID" INTEGER,
"STATUS" VARCHAR(40),
"LANGUAGE" VARCHAR(20),
"DEF_REMEDIATION_FUNCTION" VARCHAR(20),
@@ -823,13 +821,14 @@ CREATE TABLE "RULES"(
"IS_AD_HOC" BOOLEAN NOT NULL,
"IS_EXTERNAL" BOOLEAN NOT NULL,
"CREATED_AT" BIGINT,
"UPDATED_AT" BIGINT
"UPDATED_AT" BIGINT,
"UUID" VARCHAR(40) NOT NULL,
"TEMPLATE_UUID" VARCHAR(40)
);
ALTER TABLE "RULES" ADD CONSTRAINT "PK_RULES" PRIMARY KEY("ID");
ALTER TABLE "RULES" ADD CONSTRAINT "PK_RULES" PRIMARY KEY("UUID");
CREATE UNIQUE INDEX "RULES_REPO_KEY" ON "RULES"("PLUGIN_RULE_KEY", "PLUGIN_NAME");

CREATE TABLE "RULES_METADATA"(
"RULE_ID" INTEGER NOT NULL,
"ORGANIZATION_UUID" VARCHAR(40) NOT NULL,
"NOTE_DATA" CLOB(2147483647),
"NOTE_USER_UUID" VARCHAR(255),
@@ -844,21 +843,22 @@ CREATE TABLE "RULES_METADATA"(
"AD_HOC_SEVERITY" VARCHAR(10),
"AD_HOC_TYPE" TINYINT,
"CREATED_AT" BIGINT NOT NULL,
"UPDATED_AT" BIGINT NOT NULL
"UPDATED_AT" BIGINT NOT NULL,
"RULE_UUID" VARCHAR(40) NOT NULL
);
ALTER TABLE "RULES_METADATA" ADD CONSTRAINT "PK_RULES_METADATA" PRIMARY KEY("RULE_ID", "ORGANIZATION_UUID");
ALTER TABLE "RULES_METADATA" ADD CONSTRAINT "PK_RULES_METADATA" PRIMARY KEY("RULE_UUID", "ORGANIZATION_UUID");

CREATE TABLE "RULES_PARAMETERS"(
"RULE_ID" INTEGER NOT NULL,
"NAME" VARCHAR(128) NOT NULL,
"DESCRIPTION" VARCHAR(4000),
"PARAM_TYPE" VARCHAR(512) NOT NULL,
"DEFAULT_VALUE" VARCHAR(4000),
"UUID" VARCHAR(40) NOT NULL
"UUID" VARCHAR(40) NOT NULL,
"RULE_UUID" VARCHAR(40) NOT NULL
);
ALTER TABLE "RULES_PARAMETERS" ADD CONSTRAINT "PK_RULES_PARAMETERS" PRIMARY KEY("UUID");
CREATE INDEX "RULES_PARAMETERS_RULE_ID" ON "RULES_PARAMETERS"("RULE_ID");
CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_ID", "NAME");
CREATE INDEX "RULES_PARAMETERS_RULE_UUID" ON "RULES_PARAMETERS"("RULE_UUID");
CREATE UNIQUE INDEX "RULES_PARAMETERS_UNIQUE" ON "RULES_PARAMETERS"("RULE_UUID", "NAME");

CREATE TABLE "RULES_PROFILES"(
"NAME" VARCHAR(100) NOT NULL,

+ 4
- 4
server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java 查看文件

@@ -81,7 +81,7 @@ public class IssueDaoTest {
assertThat(issue.getKee()).isEqualTo(ISSUE_KEY1);
assertThat(issue.getComponentUuid()).isEqualTo(FILE_UUID);
assertThat(issue.getProjectUuid()).isEqualTo(PROJECT_UUID);
assertThat(issue.getRuleId()).isEqualTo(RULE.getId());
assertThat(issue.getRuleUuid()).isEqualTo(RULE.getUuid());
assertThat(issue.getLanguage()).isEqualTo(RULE.getLanguage());
assertThat(issue.getSeverity()).isEqualTo("BLOCKER");
assertThat(issue.getType()).isEqualTo(2);
@@ -396,7 +396,7 @@ public class IssueDaoTest {
IssueDto dto = new IssueDto();
dto.setComponent(new ComponentDto().setDbKey("struts:Action").setUuid("component-uuid"));
dto.setProject(new ComponentDto().setDbKey("struts").setUuid("project-uuid"));
dto.setRule(RuleTesting.newRule(RuleKey.of("squid", "S001")).setId(200));
dto.setRule(RuleTesting.newRule(RuleKey.of("squid", "S001")).setUuid("uuid-200"));
dto.setKee(key);
dto.setType(2);
dto.setLine(500);
@@ -425,11 +425,11 @@ public class IssueDaoTest {
db.components().insertComponent(newFileDto(projectDto).setUuid(FILE_UUID).setDbKey(FILE_KEY));
underTest.insert(db.getSession(), newIssueDto(ISSUE_KEY1)
.setMessage("the message")
.setRuleId(RULE.getId())
.setRuleUuid(RULE.getUuid())
.setComponentUuid(FILE_UUID)
.setProjectUuid(PROJECT_UUID));
underTest.insert(db.getSession(), newIssueDto(ISSUE_KEY2)
.setRuleId(RULE.getId())
.setRuleUuid(RULE.getUuid())
.setComponentUuid(FILE_UUID)
.setProjectUuid(PROJECT_UUID));
db.getSession().commit();

+ 3
- 3
server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java 查看文件

@@ -60,7 +60,7 @@ public class IssueDtoTest {
IssueDto dto = new IssueDto()
.setKee("100")
.setType(RuleType.VULNERABILITY)
.setRuleId(1)
.setRuleUuid("rule-uuid-1")
.setRuleKey("squid", "AvoidCycle")
.setLanguage("xoo")
.setComponentKey("org.sonar.sample:Sample")
@@ -116,10 +116,10 @@ public class IssueDtoTest {
public void set_rule() {
IssueDto dto = new IssueDto()
.setKee("100")
.setRule(new RuleDefinitionDto().setId(1).setRuleKey("AvoidCycle").setRepositoryKey("squid").setIsExternal(true))
.setRule(new RuleDefinitionDto().setUuid("uuid-1").setRuleKey("AvoidCycle").setRepositoryKey("squid").setIsExternal(true))
.setLanguage("xoo");

assertThat(dto.getRuleId()).isEqualTo(1);
assertThat(dto.getRuleUuid()).isEqualTo("uuid-1");
assertThat(dto.getRuleRepo()).isEqualTo("squid");
assertThat(dto.getRule()).isEqualTo("AvoidCycle");
assertThat(dto.getRuleKey().toString()).isEqualTo("squid:AvoidCycle");

+ 6
- 6
server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueMapperTest.java 查看文件

@@ -97,7 +97,7 @@ public class IssueMapperTest {
assertThat(result.getKey()).isEqualTo("ABCDE");
assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
assertThat(result.getRuleId()).isEqualTo(rule.getId());
assertThat(result.getRuleUuid()).isEqualTo(rule.getUuid());
assertThat(result.getType()).isEqualTo(2);
assertThat(result.getLine()).isEqualTo(500);
assertThat(result.getGap()).isEqualTo(3.14d);
@@ -127,7 +127,7 @@ public class IssueMapperTest {
update.setKee("ABCDE");
update.setComponentUuid("other component uuid");
update.setProjectUuid(project.uuid());
update.setRuleId(rule.getId());
update.setRuleUuid(rule.getUuid());
update.setType(3);
update.setLine(500);
update.setGap(3.14);
@@ -156,7 +156,7 @@ public class IssueMapperTest {
assertThat(result.getKey()).isEqualTo("ABCDE");
assertThat(result.getComponentUuid()).isEqualTo(file.uuid());
assertThat(result.getProjectUuid()).isEqualTo(project.uuid());
assertThat(result.getRuleId()).isEqualTo(rule.getId());
assertThat(result.getRuleUuid()).isEqualTo(rule.getUuid());
assertThat(result.getType()).isEqualTo(3);
assertThat(result.getLine()).isEqualTo(500);
assertThat(result.getGap()).isEqualTo(3.14d);
@@ -273,7 +273,7 @@ public class IssueMapperTest {
ComponentDto component = randomComponent(organization);
IssueDto issueWithRule = insertNewClosedIssue(component, ruleType);
IssueChangeDto issueChange = insertToClosedDiff(issueWithRule);
IssueDto issueWithoutRule = insertNewClosedIssue(component, new RuleDefinitionDto().setType(ruleType).setId(-50));
IssueDto issueWithoutRule = insertNewClosedIssue(component, new RuleDefinitionDto().setType(ruleType).setUuid("uuid-50"));
insertToClosedDiff(issueWithoutRule);

RecorderResultHandler resultHandler = new RecorderResultHandler();
@@ -499,7 +499,7 @@ public class IssueMapperTest {
private final IssueDto insertNewClosedIssue(ComponentDto component, RuleDefinitionDto rule, long issueCloseTime, Consumer<IssueDto>... consumers) {
IssueDto res = new IssueDto()
.setKee(UuidFactoryFast.getInstance().create())
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setType(rule.getType())
.setComponentUuid(component.uuid())
.setProjectUuid(component.projectUuid())
@@ -540,7 +540,7 @@ public class IssueMapperTest {
.setKee("ABCDE")
.setComponentUuid(file.uuid())
.setProjectUuid(project.uuid())
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setType(2)
.setLine(500)
.setGap(3.14)

+ 36
- 34
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/ActiveRuleDaoTest.java 查看文件

@@ -110,15 +110,16 @@ public class ActiveRuleDaoTest {
}

@Test
public void selectByRuleId() {
public void selectByRuleUuid() {
ActiveRuleDto activeRule1 = createFor(profile1, rule1).setSeverity(BLOCKER);
ActiveRuleDto activeRule2 = createFor(profile2, rule1).setSeverity(BLOCKER);
underTest.insert(dbSession, activeRule1);
underTest.insert(dbSession, activeRule2);
dbSession.commit();

assertThat(underTest.selectByRuleId(dbSession, organization, rule1.getId())).extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey());
assertThat(underTest.selectByRuleId(dbSession, organization, rule3.getId())).isEmpty();
assertThat(underTest.selectByRuleUuid(dbSession, organization, rule1.getUuid())).extracting("key")
.containsOnly(activeRule1.getKey(), activeRule2.getKey());
assertThat(underTest.selectByRuleUuid(dbSession, organization, rule3.getUuid())).isEmpty();
}

@Test
@@ -131,9 +132,9 @@ public class ActiveRuleDaoTest {
underTest.insert(dbSession, activeRule3);
dbSession.commit();

assertThat(underTest.selectByRuleIds(dbSession, organization, asList(rule1.getId())))
assertThat(underTest.selectByRuleUuids(dbSession, organization, singletonList(rule1.getUuid())))
.extracting("key").containsOnly(activeRule1.getKey(), activeRule3.getKey());
assertThat(underTest.selectByRuleIds(dbSession, organization, newArrayList(rule1.getId(), rule2.getId())))
assertThat(underTest.selectByRuleUuids(dbSession, organization, newArrayList(rule1.getUuid(), rule2.getUuid())))
.extracting("key").containsOnly(activeRule1.getKey(), activeRule2.getKey(), activeRule3.getKey());
}

@@ -169,8 +170,8 @@ public class ActiveRuleDaoTest {
underTest.insert(dbSession, activeRule1);

assertThat(underTest.selectByTypeAndProfileUuids(dbSession, singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList(profile1.getKee())))
.extracting(OrgActiveRuleDto::getOrgProfileUuid, OrgActiveRuleDto::getOrganizationUuid, OrgActiveRuleDto::getRuleId)
.contains(tuple(profile1.getKee(), profile1.getOrganizationUuid(), rule1.getId()));
.extracting(OrgActiveRuleDto::getOrgProfileUuid, OrgActiveRuleDto::getOrganizationUuid, OrgActiveRuleDto::getRuleUuid)
.contains(tuple(profile1.getKee(), profile1.getOrganizationUuid(), rule1.getUuid()));
}

@Test
@@ -195,14 +196,14 @@ public class ActiveRuleDaoTest {
underTest.selectByTypeAndProfileUuids(dbSession,
singletonList(RuleType.VULNERABILITY.getDbConstant()),
singletonList(profile1.getKee())))
.extracting(OrgActiveRuleDto::getOrgProfileUuid, OrgActiveRuleDto::getOrganizationUuid, OrgActiveRuleDto::getRuleId)
.contains(tuple(profile1.getKee(), profile1.getOrganizationUuid(), rule1.getId()));
.extracting(OrgActiveRuleDto::getOrgProfileUuid, OrgActiveRuleDto::getOrganizationUuid, OrgActiveRuleDto::getRuleUuid)
.contains(tuple(profile1.getKee(), profile1.getOrganizationUuid(), rule1.getUuid()));

assertThat(
underTest.selectByTypeAndProfileUuids(dbSession,
asList(RuleType.CODE_SMELL.getDbConstant(), RuleType.SECURITY_HOTSPOT.getDbConstant(), RuleType.BUG.getDbConstant()),
singletonList(profile1.getKee())))
.isEmpty();
.isEmpty();
}

@Test
@@ -231,29 +232,30 @@ public class ActiveRuleDaoTest {
underTest.insert(dbSession, rule1P2);

// empty rules
Collection<ActiveRuleDto> result = underTest.selectByRulesAndRuleProfileUuids(dbSession, emptyList(), asList(profile1.getRulesProfileUuid()));
Collection<ActiveRuleDto> result = underTest.selectByRulesAndRuleProfileUuids(dbSession, emptyList(), singletonList(profile1.getRulesProfileUuid()));
assertThat(result).isEmpty();

// empty profiles
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId()), emptyList());
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, singletonList(rule1.getUuid()), emptyList());
assertThat(result).isEmpty();

// match
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, singletonList(rule1.getUuid()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
assertThat(result)
.extracting(ActiveRuleDto::getUuid)
.containsExactlyInAnyOrder(rule1P1.getUuid(), rule1P2.getUuid());

result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId(), rule2.getId()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getUuid(), rule2.getUuid()),
asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
assertThat(result)
.extracting(ActiveRuleDto::getUuid)
.containsExactlyInAnyOrder(rule1P1.getUuid(), rule1P2.getUuid(), rule2P1.getUuid());

// do not match
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule3.getId()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, singletonList(rule3.getUuid()), asList(profile1.getRulesProfileUuid(), profile2.getRulesProfileUuid()));
assertThat(result).isEmpty();

result = underTest.selectByRulesAndRuleProfileUuids(dbSession, asList(rule1.getId()), asList("unknown"));
result = underTest.selectByRulesAndRuleProfileUuids(dbSession, singletonList(rule1.getUuid()), singletonList("unknown"));
assertThat(result).isEmpty();
}

@@ -270,7 +272,7 @@ public class ActiveRuleDaoTest {
ActiveRuleDto result = underTest.selectByKey(dbSession, activeRule.getKey()).get();
assertThat(result.getUuid()).isEqualTo(activeRule.getUuid());
assertThat(result.getKey()).isEqualTo(ActiveRuleKey.of(profile1, rule1.getKey()));
assertThat(result.getRuleId()).isEqualTo(rule1.getId());
assertThat(result.getRuleUuid()).isEqualTo(rule1.getUuid());
assertThat(result.getProfileUuid()).isEqualTo(profile1.getRulesProfileUuid());
assertThat(result.getSeverityString()).isEqualTo(BLOCKER);
assertThat(result.getInheritance()).isEqualTo(INHERITED);
@@ -291,7 +293,7 @@ public class ActiveRuleDaoTest {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Rule is not persisted");

underTest.insert(dbSession, createFor(profile1, rule1).setRuleId(null));
underTest.insert(dbSession, createFor(profile1, rule1).setRuleUuid(null));
}

@Test
@@ -324,7 +326,7 @@ public class ActiveRuleDaoTest {
ActiveRuleDto result = underTest.selectByKey(dbSession, ActiveRuleKey.of(profile1, rule1.getKey())).get();
assertThat(result.getUuid()).isEqualTo(activeRule.getUuid());
assertThat(result.getKey()).isEqualTo(ActiveRuleKey.of(profile1, rule1.getKey()));
assertThat(result.getRuleId()).isEqualTo(rule1.getId());
assertThat(result.getRuleUuid()).isEqualTo(rule1.getUuid());
assertThat(result.getProfileUuid()).isEqualTo(profile1.getRulesProfileUuid());
assertThat(result.getSeverityString()).isEqualTo(MAJOR);
assertThat(result.getInheritance()).isEqualTo(OVERRIDES);
@@ -345,7 +347,7 @@ public class ActiveRuleDaoTest {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Rule is not persisted");

underTest.update(dbSession, createFor(profile1, rule1).setUuid("uuid").setRuleId(null));
underTest.update(dbSession, createFor(profile1, rule1).setUuid("uuid").setRuleUuid(null));
}

@Test
@@ -381,7 +383,7 @@ public class ActiveRuleDaoTest {
underTest.insert(dbSession, newRow(profile1, rule2));
underTest.insert(dbSession, newRow(profile2, rule1));

underTest.deleteByRuleProfileUuids(dbSession, asList(profile1.getRulesProfileUuid()));
underTest.deleteByRuleProfileUuids(dbSession, singletonList(profile1.getRulesProfileUuid()));

assertThat(db.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1);
assertThat(underTest.selectByKey(dbSession, ActiveRuleKey.of(profile2, rule1.getKey()))).isPresent();
@@ -391,7 +393,7 @@ public class ActiveRuleDaoTest {
public void deleteByRuleProfileUuids_does_not_fail_when_rules_profile_with_specified_key_does_not_exist() {
underTest.insert(dbSession, newRow(profile1, rule1));

underTest.deleteByRuleProfileUuids(dbSession, asList("does_not_exist"));
underTest.deleteByRuleProfileUuids(dbSession, singletonList("does_not_exist"));

assertThat(db.countRowsOfTable(dbSession, "active_rules")).isEqualTo(1);
}
@@ -592,7 +594,7 @@ public class ActiveRuleDaoTest {
ActiveRuleParamDto param2 = ActiveRuleParamDto.createFor(rule2Param1).setValue("bar");
underTest.insertParam(dbSession, ar2, param2);

underTest.deleteParamsByActiveRuleUuids(dbSession, asList(ar1.getUuid()));
underTest.deleteParamsByActiveRuleUuids(dbSession, singletonList(ar1.getUuid()));

assertThat(underTest.selectParamsByActiveRuleUuid(dbSession, ar1.getUuid())).hasSize(0);
assertThat(underTest.selectParamsByActiveRuleUuid(dbSession, ar2.getUuid())).hasSize(1);
@@ -609,7 +611,7 @@ public class ActiveRuleDaoTest {
ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization);
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).build()))
.containsOnly(entry(profile1.getKee(), 2L), entry(profile2.getKee(), 1L));
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profileWithoutActiveRule)).build())).isEmpty();
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(singletonList(profileWithoutActiveRule)).build())).isEmpty();
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2, profileWithoutActiveRule)).build())).containsOnly(
entry(profile1.getKee(), 2L),
entry(profile2.getKee(), 1L));
@@ -629,9 +631,9 @@ public class ActiveRuleDaoTest {
ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization);
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1, profile2)).setRuleStatus(BETA).build()))
.containsOnly(entry(profile1.getKee(), 1L), entry(profile2.getKee(), 1L));
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1)).setRuleStatus(READY).build()))
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(singletonList(profile1)).setRuleStatus(READY).build()))
.containsOnly(entry(profile1.getKee(), 2L));
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(asList(profile1)).setRuleStatus(REMOVED).build()))
assertThat(underTest.countActiveRulesByQuery(dbSession, builder.setProfiles(singletonList(profile1)).setRuleStatus(REMOVED).build()))
.containsOnly(entry(profile1.getKee(), 1L));
}

@@ -679,13 +681,13 @@ public class ActiveRuleDaoTest {
underTest.scrollAllForIndexing(dbSession, accumulator);
assertThat(accumulator.list)
.extracting(IndexedActiveRuleDto::getUuid,
IndexedActiveRuleDto::getRuleId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
IndexedActiveRuleDto::getRuleUuid, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
IndexedActiveRuleDto::getRuleProfileUuid,
IndexedActiveRuleDto::getSeverity, IndexedActiveRuleDto::getInheritance)
.containsExactlyInAnyOrder(
tuple(ar1.getUuid(), rule1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity(), ar1.getInheritance()),
tuple(ar2.getUuid(), rule1.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity(), ar2.getInheritance()),
tuple(ar3.getUuid(), rule2.getId(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity(), ar3.getInheritance()));
tuple(ar1.getUuid(), rule1.getUuid(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity(), ar1.getInheritance()),
tuple(ar2.getUuid(), rule1.getUuid(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity(), ar2.getInheritance()),
tuple(ar3.getUuid(), rule2.getUuid(), ar3.getRuleKey().repository(), ar3.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar3.getSeverity(), ar3.getInheritance()));
}

@Test
@@ -695,14 +697,14 @@ public class ActiveRuleDaoTest {
ActiveRuleDto ar3 = db.qualityProfiles().activateRule(profile2, rule2);

Accumulator accumulator = new Accumulator();
underTest.scrollByUuidsForIndexing(dbSession, asList( ar1.getUuid(), ar2.getUuid()), accumulator);
underTest.scrollByUuidsForIndexing(dbSession, asList(ar1.getUuid(), ar2.getUuid()), accumulator);
assertThat(accumulator.list)
.extracting(IndexedActiveRuleDto::getUuid,
IndexedActiveRuleDto::getRuleId, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
IndexedActiveRuleDto::getRuleUuid, IndexedActiveRuleDto::getRepository, IndexedActiveRuleDto::getKey,
IndexedActiveRuleDto::getRuleProfileUuid, IndexedActiveRuleDto::getSeverity)
.containsExactlyInAnyOrder(
tuple(ar1.getUuid(), rule1.getId(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity()),
tuple(ar2.getUuid(), rule1.getId(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity()));
tuple(ar1.getUuid(), rule1.getUuid(), ar1.getRuleKey().repository(), ar1.getRuleKey().rule(), profile1.getRulesProfileUuid(), ar1.getSeverity()),
tuple(ar2.getUuid(), rule1.getUuid(), ar2.getRuleKey().repository(), ar2.getRuleKey().rule(), profile2.getRulesProfileUuid(), ar2.getSeverity()));
}

@Test

+ 9
- 9
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java 查看文件

@@ -72,16 +72,16 @@ public class QualityProfileExportDaoTest {
public void selectRulesByProfile_verify_columns() {
String language = "java";
RuleDefinitionDto ruleTemplate = createRule(language);
RuleDefinitionDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getId());
RuleDefinitionDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getUuid());
RuleMetadataDto customRuleMetadata = createRuleMetadata(new RuleMetadataDto()
.setRuleId(customRule.getId())
.setRuleUuid(customRule.getUuid())
.setOrganizationUuid(db.getDefaultOrganization().getUuid())
.setNoteData("Extended description")
.setTags(Sets.newHashSet("tag1", "tag2", "tag3")));

RuleDefinitionDto rule = createRule(language, RuleStatus.READY, null);
RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto()
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setOrganizationUuid(db.getDefaultOrganization().getUuid()));

QProfileDto profile = createProfile(language);
@@ -92,7 +92,7 @@ public class QualityProfileExportDaoTest {
assertThat(results).isNotNull();
assertThat(results).isNotEmpty();

//verify custom rule
// verify custom rule
ExportRuleDto exportCustomRuleDto = results.stream().filter(ExportRuleDto::isCustomRule).findFirst().get();
assertThat(exportCustomRuleDto).isNotNull();
assertThat(exportCustomRuleDto.isCustomRule()).isTrue();
@@ -108,7 +108,7 @@ public class QualityProfileExportDaoTest {
ActiveRuleDto activeCustomRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(customRule.getKey())).findFirst().get();
assertThat(exportCustomRuleDto.getSeverityString()).isEqualTo(activeCustomRule.getSeverityString());

//verify regular rule
// verify regular rule
ExportRuleDto exportRuleDto = results.stream().filter(regularRule -> !regularRule.isCustomRule()).findFirst().get();
assertThat(exportRuleDto).isNotNull();
assertThat(exportRuleDto.isCustomRule()).isFalse();
@@ -192,7 +192,6 @@ public class QualityProfileExportDaoTest {
.containsOnly(ruleParamsOfThirdRule.stream().map(RuleParamDto::getName).toArray());
}


private ExportRuleDto findExportedRuleByUuid(String uuid, List<ExportRuleDto> results) {
Optional<ExportRuleDto> found = results.stream().filter(exportRuleDto -> uuid.equals(exportRuleDto.getActiveRuleUuid())).findFirst();
if (!found.isPresent()) {
@@ -204,7 +203,7 @@ public class QualityProfileExportDaoTest {
private List<RuleParamDto> addParamsToRule(RuleDefinitionDto firstRule, int numberOfParams) {
return IntStream.range(0, numberOfParams)
.mapToObj(value -> db.rules().insertRuleParam(firstRule,
ruleParamDto -> ruleParamDto.setName("name_" + firstRule.getId() + "_" + value)))
ruleParamDto -> ruleParamDto.setName("name_" + firstRule.getUuid() + "_" + value)))
.collect(Collectors.toList());
}

@@ -216,8 +215,9 @@ public class QualityProfileExportDaoTest {
return createRule(language, status, null);
}

private RuleDefinitionDto createRule(String language, RuleStatus status, @Nullable Integer templateId) {
return db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setRepositoryKey("repoKey").setLanguage(language).setStatus(status).setTemplateId(templateId));
private RuleDefinitionDto createRule(String language, RuleStatus status, @Nullable String templateUuid) {
return db.rules().insert(ruleDefinitionDto -> ruleDefinitionDto.setRepositoryKey("repoKey").setLanguage(language).setStatus(status)
.setTemplateUuid(templateUuid));
}

private RuleMetadataDto createRuleMetadata(RuleMetadataDto metadataDto) {

+ 100
- 100
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java 查看文件

@@ -61,7 +61,7 @@ import static org.sonar.db.rule.RuleTesting.newRuleMetadata;
public class RuleDaoTest {

private static final String ORGANIZATION_UUID = "org-1";
private static final int UNKNOWN_RULE_ID = 1_234_567_890;
private static final String UNKNOWN_RULE_UUID = "unknown-uuid";

@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -104,7 +104,7 @@ public class RuleDaoTest {
RuleDefinitionDto ruleDefinition = db.rules().insert();

RuleDto rule = underTest.selectByKey(db.getSession(), organization.getUuid(), ruleDefinition.getKey()).get();
verifyNoMetadata(rule.getMetadata(), ruleDefinition, organization);
verifyNoMetadata(rule.getMetadata(), organization);
}

@Test
@@ -134,38 +134,38 @@ public class RuleDaoTest {
}

@Test
public void selectById() {
public void selectByUuid() {
RuleDefinitionDto ruleDefinition = db.rules().insert();
OrganizationDto organization = db.organizations().insert();
RuleMetadataDto metadata = newRuleMetadata(ruleDefinition, organization);
RuleDto expected = db.rules().insertRule(ruleDefinition, metadata);

assertThat(underTest.selectById(expected.getId() + 500, organization.getUuid(), db.getSession()))
assertThat(underTest.selectByUuid(expected.getUuid() + 500, organization.getUuid(), db.getSession()))
.isEmpty();
RuleDto rule = underTest.selectById(expected.getId(), organization.getUuid(), db.getSession()).get();
RuleDto rule = underTest.selectByUuid(expected.getUuid(), organization.getUuid(), db.getSession()).get();
assertEquals(rule.getDefinition(), ruleDefinition);
verifyMetadata(rule.getMetadata(), ruleDefinition, metadata);
}

@Test
public void selectById_return_rule_even_if_organization_does_not_exist() {
public void selectByUuid_return_rule_even_if_organization_does_not_exist() {
RuleDefinitionDto ruleDefinition = db.rules().insert();

assertThat(underTest.selectById(ruleDefinition.getId(), "dfdfdf", db.getSession()))
assertThat(underTest.selectByUuid(ruleDefinition.getUuid(), "dfdfdf", db.getSession()))
.isNotEmpty();
}

@Test
public void selectById_populates_organizationUuid_even_when_organization_has_no_metadata() {
public void selectByUuid_populates_organizationUuid_even_when_organization_has_no_metadata() {
OrganizationDto organization = db.organizations().insert();
RuleDefinitionDto ruleDefinition = db.rules().insert();

RuleDto rule = underTest.selectById(ruleDefinition.getId(), organization.getUuid(), db.getSession()).get();
verifyNoMetadata(rule.getMetadata(), ruleDefinition, organization);
RuleDto rule = underTest.selectByUuid(ruleDefinition.getUuid(), organization.getUuid(), db.getSession()).get();
verifyNoMetadata(rule.getMetadata(), organization);
}

@Test
public void selectById_returns_metadata_of_specified_organization() {
public void selectByUuid_returns_metadata_of_specified_organization() {
RuleDefinitionDto ruleDefinition = db.rules().insert();
OrganizationDto organization1 = db.organizations().insert();
RuleMetadataDto expectedOrg1 = newRuleMetadata(ruleDefinition, organization1);
@@ -174,23 +174,23 @@ public class RuleDaoTest {
RuleMetadataDto expectedOrg2 = newRuleMetadata(ruleDefinition, organization2);
db.rules().insertRule(ruleDefinition, expectedOrg2);

RuleDto rule = underTest.selectById(ruleDefinition.getId(), organization1.getUuid(), db.getSession()).get();
RuleDto rule = underTest.selectByUuid(ruleDefinition.getUuid(), organization1.getUuid(), db.getSession()).get();
verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg1);
rule = underTest.selectById(ruleDefinition.getId(), organization2.getUuid(), db.getSession()).get();
rule = underTest.selectByUuid(ruleDefinition.getUuid(), organization2.getUuid(), db.getSession()).get();
verifyMetadata(rule.getMetadata(), ruleDefinition, expectedOrg2);
}

@Test
public void selectDefinitionById() {
public void selectDefinitionByUuid() {
RuleDefinitionDto rule = db.rules().insert();

assertThat(underTest.selectDefinitionById(1_234_567L, db.getSession())).isEmpty();
Optional<RuleDefinitionDto> ruleDtoOptional = underTest.selectDefinitionById(rule.getId(), db.getSession());
assertThat(underTest.selectDefinitionByUuid(UNKNOWN_RULE_UUID, db.getSession())).isEmpty();
Optional<RuleDefinitionDto> ruleDtoOptional = underTest.selectDefinitionByUuid(rule.getUuid(), db.getSession());
assertThat(ruleDtoOptional).isPresent();
}

@Test
public void selectByIds() {
public void selectByUuids() {
OrganizationDto organization = db.organizations().insert();
RuleDefinitionDto rule1 = db.rules().insert();
db.rules().insertOrUpdateMetadata(rule1, organization);
@@ -199,44 +199,43 @@ public class RuleDaoTest {
RuleDefinitionDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
db.rules().insertOrUpdateMetadata(removedRule, organization);

assertThat(underTest.selectByIds(db.getSession(), organization.getUuid(), singletonList(rule1.getId()))).hasSize(1);
assertThat(underTest.selectByIds(db.getSession(), organization.getUuid(), asList(rule1.getId(), rule2.getId()))).hasSize(2);
assertThat(underTest.selectByIds(db.getSession(), organization.getUuid(), asList(rule1.getId(), rule2.getId(), UNKNOWN_RULE_ID))).hasSize(2);
assertThat(underTest.selectByIds(db.getSession(), organization.getUuid(), asList(rule1.getId(), rule2.getId(), removedRule.getId()))).hasSize(3);
assertThat(underTest.selectByIds(db.getSession(), organization.getUuid(), singletonList(UNKNOWN_RULE_ID))).isEmpty();
assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), singletonList(rule1.getUuid()))).hasSize(1);
assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2);
assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid(), UNKNOWN_RULE_UUID))).hasSize(2);
assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid(), removedRule.getUuid()))).hasSize(3);
assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), singletonList(UNKNOWN_RULE_UUID))).isEmpty();
}

@Test
public void selectByIds_populates_organizationUuid_even_when_organization_has_no_metadata() {
public void selectByUuids_populates_organizationUuid_even_when_organization_has_no_metadata() {
OrganizationDto organization = db.organizations().insert();
RuleDefinitionDto rule1 = db.rules().insert();
RuleDefinitionDto rule2 = db.rules().insert();

assertThat(underTest.selectByIds(db.getSession(), organization.getUuid(), asList(rule1.getId(), rule2.getId())))
assertThat(underTest.selectByUuids(db.getSession(), organization.getUuid(), asList(rule1.getUuid(), rule2.getUuid())))
.extracting(RuleDto::getOrganizationUuid)
.containsExactly(organization.getUuid(), organization.getUuid());
}

@Test
public void selectDefinitionByIds() {
public void selectDefinitionByUuids() {
RuleDefinitionDto rule1 = db.rules().insert();
RuleDefinitionDto rule2 = db.rules().insert();

assertThat(underTest.selectDefinitionByIds(db.getSession(), singletonList(rule1.getId()))).hasSize(1);
assertThat(underTest.selectDefinitionByIds(db.getSession(), asList(rule1.getId(), rule2.getId()))).hasSize(2);
assertThat(underTest.selectDefinitionByIds(db.getSession(), asList(rule1.getId(), rule2.getId(), UNKNOWN_RULE_ID))).hasSize(2);

assertThat(underTest.selectDefinitionByIds(db.getSession(), singletonList(UNKNOWN_RULE_ID))).isEmpty();
assertThat(underTest.selectDefinitionByUuids(db.getSession(), singletonList(rule1.getUuid()))).hasSize(1);
assertThat(underTest.selectDefinitionByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2);
assertThat(underTest.selectDefinitionByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid(), UNKNOWN_RULE_UUID))).hasSize(2);
assertThat(underTest.selectDefinitionByUuids(db.getSession(), singletonList(UNKNOWN_RULE_UUID))).isEmpty();
}

@Test
public void selectOrFailByKey() {
OrganizationDto organization = db.organizations().insert();
RuleDefinitionDto rule1 = db.rules().insert();
RuleDefinitionDto rule2 = db.rules().insert();
db.rules().insert();

RuleDto rule = underTest.selectOrFailByKey(db.getSession(), organization, rule1.getKey());
assertThat(rule.getId()).isEqualTo(rule1.getId());
assertThat(rule.getUuid()).isEqualTo(rule1.getUuid());
}

@Test
@@ -275,11 +274,11 @@ public class RuleDaoTest {
db.rules().insertOrUpdateMetadata(rule2, organization);

assertThat(underTest.selectByKeys(db.getSession(), organization.getUuid(), Collections.emptyList())).isEmpty();
assertThat(underTest.selectByKeys(db.getSession(), organization.getUuid(), asList(RuleKey.of("NOT", "FOUND")))).isEmpty();
assertThat(underTest.selectByKeys(db.getSession(), organization.getUuid(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();

List<RuleDto> rules = underTest.selectByKeys(db.getSession(), organization.getUuid(), asList(rule1.getKey(), RuleKey.of("java", "OTHER")));
assertThat(rules).hasSize(1);
assertThat(rules.get(0).getId()).isEqualTo(rule1.getId());
assertThat(rules.get(0).getUuid()).isEqualTo(rule1.getUuid());
}

@Test
@@ -297,11 +296,11 @@ public class RuleDaoTest {
RuleDefinitionDto rule = db.rules().insert();

assertThat(underTest.selectDefinitionByKeys(db.getSession(), Collections.emptyList())).isEmpty();
assertThat(underTest.selectDefinitionByKeys(db.getSession(), asList(RuleKey.of("NOT", "FOUND")))).isEmpty();
assertThat(underTest.selectDefinitionByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty();

List<RuleDefinitionDto> rules = underTest.selectDefinitionByKeys(db.getSession(), asList(rule.getKey(), RuleKey.of("java", "OTHER")));
assertThat(rules).hasSize(1);
assertThat(rules.get(0).getId()).isEqualTo(rule.getId());
assertThat(rules.get(0).getUuid()).isEqualTo(rule.getUuid());
}

@Test
@@ -312,8 +311,8 @@ public class RuleDaoTest {
RuleDto rule3 = db.rules().insertRule(organization);

assertThat(underTest.selectAll(db.getSession(), organization.getUuid()))
.extracting(RuleDto::getId)
.containsOnly(rule1.getId(), rule2.getId(), rule3.getId());
.extracting(RuleDto::getUuid)
.containsOnly(rule1.getUuid(), rule2.getUuid(), rule3.getUuid());
}

@Test
@@ -323,8 +322,8 @@ public class RuleDaoTest {
RuleDefinitionDto rule3 = db.rules().insert();

assertThat(underTest.selectAll(db.getSession(), "dfdfdf"))
.extracting(RuleDto::getId)
.containsOnly(rule1.getId(), rule2.getId(), rule3.getId());
.extracting(RuleDto::getUuid)
.containsOnly(rule1.getUuid(), rule2.getUuid(), rule3.getUuid());
}

@Test
@@ -335,8 +334,8 @@ public class RuleDaoTest {

List<RuleDto> rules = underTest.selectAll(db.getSession(), organization.getUuid());
assertThat(rules)
.extracting(RuleDto::getId)
.containsOnly(ruleDefinition1.getId(), ruleDefinition2.getId());
.extracting(RuleDto::getUuid)
.containsOnly(ruleDefinition1.getUuid(), ruleDefinition2.getUuid());
assertThat(rules)
.extracting(RuleDto::getOrganizationUuid)
.containsExactly(organization.getUuid(), organization.getUuid());
@@ -356,7 +355,7 @@ public class RuleDaoTest {
}

private void assertEquals(RuleDefinitionDto actual, RuleDefinitionDto expected) {
assertThat(actual.getId()).isEqualTo(expected.getId());
assertThat(actual.getUuid()).isEqualTo(expected.getUuid());
assertThat(actual.getRepositoryKey()).isEqualTo(expected.getRepositoryKey());
assertThat(actual.getRuleKey()).isEqualTo(expected.getRuleKey());
assertThat(actual.getKey()).isEqualTo(expected.getKey());
@@ -370,7 +369,7 @@ public class RuleDaoTest {
assertThat(actual.isExternal()).isEqualTo(expected.isExternal());
assertThat(actual.isTemplate()).isEqualTo(expected.isTemplate());
assertThat(actual.getLanguage()).isEqualTo(expected.getLanguage());
assertThat(actual.getTemplateId()).isEqualTo(expected.getTemplateId());
assertThat(actual.getTemplateUuid()).isEqualTo(expected.getTemplateUuid());
assertThat(actual.getDefRemediationFunction()).isEqualTo(expected.getDefRemediationFunction());
assertThat(actual.getDefRemediationGapMultiplier()).isEqualTo(expected.getDefRemediationGapMultiplier());
assertThat(actual.getDefRemediationBaseEffort()).isEqualTo(expected.getDefRemediationBaseEffort());
@@ -395,7 +394,7 @@ public class RuleDaoTest {
assertThat(metadata.getAdHocType()).isEqualTo(expected.getAdHocType());
}

private static void verifyNoMetadata(RuleMetadataDto metadata, RuleDefinitionDto ruleDefinition, OrganizationDto organization) {
private static void verifyNoMetadata(RuleMetadataDto metadata, OrganizationDto organization) {
assertThat(metadata.getOrganizationUuid()).isEqualTo(organization.getUuid());
assertThat(metadata.getRemediationBaseEffort()).isNull();
assertThat(metadata.getRemediationFunction()).isNull();
@@ -418,13 +417,13 @@ public class RuleDaoTest {

List<RuleDefinitionDto> ruleDtos = underTest.selectAllDefinitions(db.getSession());

assertThat(ruleDtos).extracting(RuleDefinitionDto::getId).containsOnly(rule1.getId(), rule2.getId(), removedRule.getId());
assertThat(ruleDtos).extracting(RuleDefinitionDto::getUuid).containsOnly(rule1.getUuid(), rule2.getUuid(), removedRule.getUuid());
}

@Test
public void selectEnabled_with_ResultHandler() {
RuleDefinitionDto rule = db.rules().insert();
RuleDefinitionDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED));
db.rules().insert(r -> r.setStatus(REMOVED));

final List<RuleDefinitionDto> rules = new ArrayList<>();
ResultHandler<RuleDefinitionDto> resultHandler = resultContext -> rules.add(resultContext.getResultObject());
@@ -432,7 +431,7 @@ public class RuleDaoTest {

assertThat(rules.size()).isEqualTo(1);
RuleDefinitionDto ruleDto = rules.get(0);
assertThat(ruleDto.getId()).isEqualTo(rule.getId());
assertThat(ruleDto.getUuid()).isEqualTo(rule.getUuid());
}

@Test
@@ -442,34 +441,34 @@ public class RuleDaoTest {

RuleDefinitionDto rule1 = db.rules().insert(
r -> r.setKey(RuleKey.of("java", "S001"))
.setConfigKey("S1")
.setType(RuleType.VULNERABILITY)
.setLanguage("java"));
.setConfigKey("S1")
.setType(RuleType.VULNERABILITY)
.setLanguage("java"));
db.rules().insertOrUpdateMetadata(rule1, organization);

RuleDefinitionDto rule2 = db.rules().insert(
r -> r.setKey(RuleKey.of("js", "S002"))
.setType(RuleType.SECURITY_HOTSPOT)
.setLanguage("js"));
.setType(RuleType.SECURITY_HOTSPOT)
.setLanguage("js"));
db.rules().insertOrUpdateMetadata(rule2, organization);

assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
.extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization.getUuid(), rule1.getId(), "java", RuleType.VULNERABILITY.getDbConstant()));
.extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization.getUuid(), rule1.getUuid(), "java", RuleType.VULNERABILITY.getDbConstant()));

// Rule available also on organization2
assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization2.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
.extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization2.getUuid(), rule1.getId(), "java", RuleType.VULNERABILITY.getDbConstant()));
.extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization2.getUuid(), rule1.getUuid(), "java", RuleType.VULNERABILITY.getDbConstant()));

assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("js")))
.extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization.getUuid(), rule2.getId(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
.extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization.getUuid(), rule2.getUuid(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));

// Rule available also on organization2
assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization2.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("js")))
.extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization2.getUuid(), rule2.getId(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
.extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
.containsExactly(tuple(organization2.getUuid(), rule2.getUuid(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));

assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.SECURITY_HOTSPOT.getDbConstant()), singletonList("java")))
.isEmpty();
@@ -538,7 +537,7 @@ public class RuleDaoTest {
db.rules().insertOrUpdateMetadata(rule1, organization);

assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
.extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
.extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
.isEmpty();
}

@@ -555,7 +554,7 @@ public class RuleDaoTest {
db.rules().insertOrUpdateMetadata(rule1, organization);

assertThat(underTest.selectByTypeAndLanguages(db.getSession(), organization.getUuid(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java")))
.extracting(RuleDto::getOrganizationUuid, RuleDto::getId, RuleDto::getLanguage, RuleDto::getType)
.extracting(RuleDto::getOrganizationUuid, RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
.isEmpty();
}

@@ -579,8 +578,8 @@ public class RuleDaoTest {
@Test
public void select_by_query_populates_organizationUuid_even_when_organization_has_no_metadata() {
OrganizationDto organization = db.organizations().insert();
RuleDefinitionDto rule1 = db.rules().insert();
RuleDefinitionDto rule2 = db.rules().insert();
db.rules().insert();
db.rules().insert();

assertThat(underTest.selectByQuery(db.getSession(), organization.getUuid(), RuleQuery.create()))
.extracting(RuleDto::getOrganizationUuid)
@@ -590,6 +589,7 @@ public class RuleDaoTest {
@Test
public void insert() {
RuleDefinitionDto newRule = new RuleDefinitionDto()
.setUuid("rule-uuid")
.setRuleKey("NewRuleKey")
.setRepositoryKey("plugin")
.setName("new name")
@@ -602,7 +602,7 @@ public class RuleDaoTest {
.setIsExternal(true)
.setIsAdHoc(true)
.setLanguage("dart")
.setTemplateId(3)
.setTemplateUuid("uuid-3")
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
.setDefRemediationGapMultiplier("5d")
.setDefRemediationBaseEffort("10h")
@@ -617,7 +617,7 @@ public class RuleDaoTest {
db.getSession().commit();

RuleDefinitionDto ruleDto = underTest.selectOrFailDefinitionByKey(db.getSession(), RuleKey.of("plugin", "NewRuleKey"));
assertThat(ruleDto.getId()).isNotNull();
assertThat(ruleDto.getUuid()).isNotNull();
assertThat(ruleDto.getName()).isEqualTo("new name");
assertThat(ruleDto.getDescription()).isEqualTo("new description");
assertThat(ruleDto.getDescriptionFormat()).isEqualTo(RuleDto.Format.MARKDOWN);
@@ -630,7 +630,7 @@ public class RuleDaoTest {
assertThat(ruleDto.isTemplate()).isTrue();
assertThat(ruleDto.isExternal()).isTrue();
assertThat(ruleDto.isAdHoc()).isTrue();
assertThat(ruleDto.getTemplateId()).isEqualTo(3);
assertThat(ruleDto.getTemplateUuid()).isEqualTo("uuid-3");
assertThat(ruleDto.getDefRemediationFunction()).isEqualTo("LINEAR_OFFSET");
assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo("5d");
assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo("10h");
@@ -647,7 +647,7 @@ public class RuleDaoTest {
public void update_RuleDefinitionDto() {
RuleDefinitionDto rule = db.rules().insert();
RuleDefinitionDto ruleToUpdate = new RuleDefinitionDto()
.setId(rule.getId())
.setUuid(rule.getUuid())
.setRuleKey("NewRuleKey")
.setRepositoryKey("plugin")
.setName("new name")
@@ -660,7 +660,7 @@ public class RuleDaoTest {
.setIsExternal(true)
.setIsAdHoc(true)
.setLanguage("dart")
.setTemplateId(3)
.setTemplateUuid("uuid-3")
.setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.toString())
.setDefRemediationGapMultiplier("5d")
.setDefRemediationBaseEffort("10h")
@@ -687,7 +687,7 @@ public class RuleDaoTest {
assertThat(ruleDto.isTemplate()).isTrue();
assertThat(ruleDto.isExternal()).isTrue();
assertThat(ruleDto.isAdHoc()).isTrue();
assertThat(ruleDto.getTemplateId()).isEqualTo(3);
assertThat(ruleDto.getTemplateUuid()).isEqualTo("uuid-3");
assertThat(ruleDto.getDefRemediationFunction()).isEqualTo("LINEAR_OFFSET");
assertThat(ruleDto.getDefRemediationGapMultiplier()).isEqualTo("5d");
assertThat(ruleDto.getDefRemediationBaseEffort()).isEqualTo("10h");
@@ -706,7 +706,7 @@ public class RuleDaoTest {
String organizationUuid = "org-1";

RuleMetadataDto metadataToUpdate = new RuleMetadataDto()
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setOrganizationUuid(organizationUuid)
.setNoteData("My note")
.setNoteUserUuid("admin")
@@ -758,12 +758,12 @@ public class RuleDaoTest {
String organizationUuid = "org-1";
OrganizationDto organization = OrganizationTesting.newOrganizationDto().setUuid(organizationUuid);
RuleMetadataDto metadataV1 = new RuleMetadataDto()
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setOrganizationUuid(organizationUuid)
.setCreatedAt(3_500_000_000_000L)
.setUpdatedAt(4_000_000_000_000L);
RuleMetadataDto metadataV2 = new RuleMetadataDto()
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setOrganizationUuid(organizationUuid)
.setNoteData("My note")
.setNoteUserUuid("admin")
@@ -835,7 +835,7 @@ public class RuleDaoTest {
assertThat(ruleDto.getName()).isEqualTo(ruleParam.getName());
assertThat(ruleDto.getDescription()).isEqualTo(ruleParam.getDescription());
assertThat(ruleDto.getType()).isEqualTo(ruleParam.getType());
assertThat(ruleDto.getRuleId()).isEqualTo(rule.getId());
assertThat(ruleDto.getRuleUuid()).isEqualTo(rule.getUuid());
}

@Test
@@ -868,7 +868,7 @@ public class RuleDaoTest {
assertThat(ruleParamDtos).hasSize(1);

RuleParamDto loaded = ruleParamDtos.get(0);
assertThat(loaded.getRuleId()).isEqualTo(orig.getRuleId());
assertThat(loaded.getRuleUuid()).isEqualTo(orig.getRuleUuid());
assertThat(loaded.getName()).isEqualTo(orig.getName());
assertThat(loaded.getType()).isEqualTo(orig.getType());
assertThat(loaded.getDefaultValue()).isEqualTo(orig.getDefaultValue());
@@ -900,7 +900,7 @@ public class RuleDaoTest {
assertThat(params).hasSize(1);
RuleParamDto param = new RuleParamDto()
.setUuid(ruleParam.getUuid())
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
// Name will not be updated
.setName("format")
.setType("STRING")
@@ -943,8 +943,8 @@ public class RuleDaoTest {
underTest.scrollIndexingRules(db.getSession(), accumulator);

assertThat(accumulator.list)
.extracting(RuleForIndexingDto::getId, RuleForIndexingDto::getRuleKey)
.containsExactlyInAnyOrder(tuple(r1.getId(), r1.getKey()), tuple(r2.getId(), r2.getKey()));
.extracting(RuleForIndexingDto::getUuid, RuleForIndexingDto::getRuleKey)
.containsExactlyInAnyOrder(tuple(r1.getUuid(), r1.getKey()), tuple(r2.getUuid(), r2.getKey()));
Iterator<RuleForIndexingDto> it = accumulator.list.iterator();
RuleForIndexingDto firstRule = it.next();

@@ -975,7 +975,7 @@ public class RuleDaoTest {
public void scrollIndexingRules_maps_rule_definition_fields_for_regular_rule_and_template_rule() {
Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
RuleDefinitionDto r1 = db.rules().insert();
RuleDefinitionDto r2 = db.rules().insert(rule -> rule.setTemplateId(r1.getId()));
RuleDefinitionDto r2 = db.rules().insert(rule -> rule.setTemplateUuid(r1.getUuid()));

underTest.scrollIndexingRules(db.getSession(), accumulator);

@@ -997,24 +997,24 @@ public class RuleDaoTest {
RuleDefinitionDto r1 = db.rules().insert();
db.rules().insert();

underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(r1.getId()), accumulator);
underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(r1.getUuid()), accumulator);

assertThat(accumulator.list)
.extracting(RuleForIndexingDto::getId, RuleForIndexingDto::getRuleKey)
.containsExactlyInAnyOrder(tuple(r1.getId(), r1.getKey()));
.extracting(RuleForIndexingDto::getUuid, RuleForIndexingDto::getRuleKey)
.containsExactlyInAnyOrder(tuple(r1.getUuid(), r1.getKey()));
}

@Test
public void scrollIndexingRulesByKeys_maps_rule_definition_fields_for_regular_rule_and_template_rule() {
Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
RuleDefinitionDto r1 = db.rules().insert();
RuleDefinitionDto r2 = db.rules().insert(rule -> rule.setTemplateId(r1.getId()));
RuleDefinitionDto r2 = db.rules().insert(rule -> rule.setTemplateUuid(r1.getUuid()));

underTest.scrollIndexingRulesByKeys(db.getSession(), Arrays.asList(r1.getId(), r2.getId()), accumulator);
underTest.scrollIndexingRulesByKeys(db.getSession(), Arrays.asList(r1.getUuid(), r2.getUuid()), accumulator);

assertThat(accumulator.list).hasSize(2);
RuleForIndexingDto firstRule = accumulator.list.stream().filter(t -> t.getId().equals(r1.getId())).findFirst().get();
RuleForIndexingDto secondRule = accumulator.list.stream().filter(t -> t.getId().equals(r2.getId())).findFirst().get();
RuleForIndexingDto firstRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r1.getUuid())).findFirst().get();
RuleForIndexingDto secondRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r2.getUuid())).findFirst().get();

assertRuleDefinitionFieldsAreEquals(r1, firstRule);
assertThat(firstRule.getTemplateRuleKey()).isNull();
@@ -1025,7 +1025,7 @@ public class RuleDaoTest {
}

private void assertRuleDefinitionFieldsAreEquals(RuleDefinitionDto r1, RuleForIndexingDto firstRule) {
assertThat(firstRule.getId()).isEqualTo(r1.getId());
assertThat(firstRule.getUuid()).isEqualTo(r1.getUuid());
assertThat(firstRule.getRuleKey()).isEqualTo(r1.getKey());
assertThat(firstRule.getRepository()).isEqualTo(r1.getRepositoryKey());
assertThat(firstRule.getPluginRuleKey()).isEqualTo(r1.getRuleKey());
@@ -1050,9 +1050,9 @@ public class RuleDaoTest {
public void scrollIndexingRulesByKeys_scrolls_nothing_if_key_does_not_exist() {
Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
db.rules().insert();
int nonExistingRuleId = 42;
String nonExistingRuleUuid = "non-existing-uuid";

underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(nonExistingRuleId), accumulator);
underTest.scrollIndexingRulesByKeys(db.getSession(), singletonList(nonExistingRuleUuid), accumulator);

assertThat(accumulator.list).isEmpty();
}
@@ -1068,12 +1068,12 @@ public class RuleDaoTest {
underTest.scrollIndexingRuleExtensions(db.getSession(), accumulator);

assertThat(accumulator.list)
.extracting(RuleExtensionForIndexingDto::getRuleId,
.extracting(RuleExtensionForIndexingDto::getRuleUuid,
RuleExtensionForIndexingDto::getRuleKey,
RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
.containsExactlyInAnyOrder(
tuple(r1.getId(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()),
tuple(r2.getId(), r2.getKey(), organization.getUuid(), r2Extension.getTagsAsString()));
tuple(r1.getUuid(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()),
tuple(r2.getUuid(), r2.getKey(), organization.getUuid(), r2Extension.getTagsAsString()));
}

@Test
@@ -1081,18 +1081,18 @@ public class RuleDaoTest {
Accumulator<RuleExtensionForIndexingDto> accumulator = new Accumulator<>();
RuleDefinitionDto r1 = db.rules().insert();
RuleMetadataDto r1Extension = db.rules().insertOrUpdateMetadata(r1, organization, r -> r.setTagsField("t1,t2"));
RuleExtensionId r1ExtensionId = new RuleExtensionId(organization.getUuid(), r1.getId());
RuleExtensionId r1ExtensionId = new RuleExtensionId(organization.getUuid(), r1.getUuid());
RuleDefinitionDto r2 = db.rules().insert();
db.rules().insertOrUpdateMetadata(r2, organization, r -> r.setTagsField("t1,t3"));

underTest.scrollIndexingRuleExtensionsByIds(db.getSession(), singletonList(r1ExtensionId), accumulator);

assertThat(accumulator.list)
.extracting(RuleExtensionForIndexingDto::getRuleId,
.extracting(RuleExtensionForIndexingDto::getRuleUuid,
RuleExtensionForIndexingDto::getRuleKey,
RuleExtensionForIndexingDto::getOrganizationUuid, RuleExtensionForIndexingDto::getTags)
.containsExactlyInAnyOrder(
tuple(r1.getId(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()));
tuple(r1.getUuid(), r1.getKey(), organization.getUuid(), r1Extension.getTagsAsString()));
}

@Test
@@ -1100,8 +1100,8 @@ public class RuleDaoTest {
RuleDefinitionDto r1 = db.rules().insert();
RuleDefinitionDto r2 = db.rules().insert();

db.rules().insertDeprecatedKey(r -> r.setRuleId(r1.getId()));
db.rules().insertDeprecatedKey(r -> r.setRuleId(r2.getId()));
db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r1.getUuid()));
db.rules().insertDeprecatedKey(r -> r.setRuleUuid(r2.getUuid()));

db.getSession().commit();

@@ -1161,7 +1161,7 @@ public class RuleDaoTest {
@Test
public void insertDeprecatedRuleKey() {
RuleDefinitionDto r1 = db.rules().insert();
DeprecatedRuleKeyDto deprecatedRuleKeyDto = db.rules().insertDeprecatedKey(d -> d.setRuleId(r1.getId()));
DeprecatedRuleKeyDto deprecatedRuleKeyDto = db.rules().insertDeprecatedKey(d -> d.setRuleUuid(r1.getUuid()));

db.getSession().commit();

@@ -1175,7 +1175,7 @@ public class RuleDaoTest {
assertThat(deprecatedRuleKeyDto1.getNewRuleKey()).isEqualTo(r1.getRuleKey());
assertThat(deprecatedRuleKeyDto1.getUuid()).isEqualTo(deprecatedRuleKeyDto.getUuid());
assertThat(deprecatedRuleKeyDto1.getCreatedAt()).isEqualTo(deprecatedRuleKeyDto.getCreatedAt());
assertThat(deprecatedRuleKeyDto1.getRuleId()).isEqualTo(r1.getId());
assertThat(deprecatedRuleKeyDto1.getRuleUuid()).isEqualTo(r1.getUuid());
}

@Test

+ 15
- 17
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDefinitionDtoTest.java 查看文件

@@ -19,41 +19,39 @@
*/
package org.sonar.db.rule;

import java.util.Random;
import org.junit.Test;
import org.sonar.core.util.Uuids;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.rule.RuleTesting.newRule;

public class RuleDefinitionDtoTest {

private static final Random RANDOM = new Random();

@Test
public void equals_is_based_on_id() {
int id = RANDOM.nextInt(153151);
RuleDefinitionDto dto = newRule().setId(id);
public void equals_is_based_on_uuid() {
String uuid = Uuids.createFast();
RuleDefinitionDto dto = newRule().setUuid(uuid);

assertThat(dto).isEqualTo(dto);
assertThat(dto).isEqualTo(newRule().setId(id));
assertThat(dto).isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id));
assertThat(dto).isEqualTo(newRule().setUuid(uuid));
assertThat(dto).isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(uuid));
assertThat(dto).isNotEqualTo(null);
assertThat(dto).isNotEqualTo(new Object());
assertThat(dto).isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id - 1));
assertThat(dto).isNotEqualTo(newRule().setId(id + 1));
assertThat(dto).isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(Uuids.createFast()));
assertThat(dto).isNotEqualTo(newRule().setUuid(Uuids.createFast()));
}

@Test
public void hashcode_is_based_on_id() {
int id = RANDOM.nextInt(153151);
RuleDefinitionDto dto = newRule().setId(id);
public void hashcode_is_based_on_uuid() {
String uuid = Uuids.createFast();
RuleDefinitionDto dto = newRule().setUuid(uuid);

assertThat(dto.hashCode()).isEqualTo(dto.hashCode());
assertThat(dto.hashCode()).isEqualTo(newRule().setId(id).hashCode());
assertThat(dto.hashCode()).isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id).hashCode());
assertThat(dto.hashCode()).isEqualTo(newRule().setUuid(uuid).hashCode());
assertThat(dto.hashCode()).isEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(uuid).hashCode());
assertThat(dto.hashCode()).isNotEqualTo(null);
assertThat(dto.hashCode()).isNotEqualTo(new Object().hashCode());
assertThat(dto.hashCode()).isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setId(id - 1).hashCode());
assertThat(dto.hashCode()).isNotEqualTo(newRule().setId(id + 1).hashCode());
assertThat(dto.hashCode()).isNotEqualTo(newRule().setRuleKey(dto.getRuleKey()).setUuid(Uuids.createFast()).hashCode());
assertThat(dto.hashCode()).isNotEqualTo(newRule().setUuid(Uuids.createFast()).hashCode());
}
}

+ 17
- 17
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleDbTester.java 查看文件

@@ -25,6 +25,7 @@ import java.util.function.Consumer;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.UserDto;
@@ -100,10 +101,6 @@ public class RuleDbTester {
return insert(newHotspotRule());
}

public RuleDefinitionDto insertHotspotRule(RuleKey key) {
return insert(newHotspotRule(key));
}

@SafeVarargs
public final RuleDefinitionDto insertHotspotRule(Consumer<RuleDefinitionDto>... populaters) {
RuleDefinitionDto rule = newHotspotRule();
@@ -111,21 +108,15 @@ public class RuleDbTester {
return insert(rule);
}

public RuleDefinitionDto insertHotspotRule(RuleKey key, Consumer<RuleDefinitionDto> populater) {
RuleDefinitionDto rule = newHotspotRule(key);
populater.accept(rule);
return insert(rule);
}

private static RuleDefinitionDto newHotspotRule(RuleKey key) {
return newRule(key).setType(SECURITY_HOTSPOT);
}

private static RuleDefinitionDto newHotspotRule() {
return newRule().setType(SECURITY_HOTSPOT);
}

public RuleDefinitionDto insert(RuleDefinitionDto rule) {
if (rule.getUuid() == null) {
rule.setUuid(Uuids.createFast());
}

db.getDbClient().ruleDao().insert(db.getSession(), rule);
db.commit();
return rule;
@@ -172,10 +163,14 @@ public class RuleDbTester {
}

public RuleDto insertRule(RuleDto ruleDto) {
if (ruleDto.getUuid() == null) {
ruleDto.setUuid(Uuids.createFast());
}

insert(ruleDto.getDefinition());
RuleMetadataDto metadata = ruleDto.getMetadata();
if (metadata.getOrganizationUuid() != null) {
db.getDbClient().ruleDao().insertOrUpdate(db.getSession(), metadata.setRuleId(ruleDto.getId()));
db.getDbClient().ruleDao().insertOrUpdate(db.getSession(), metadata.setRuleUuid(ruleDto.getUuid()));
db.commit();
}
return ruleDto;
@@ -199,6 +194,11 @@ public class RuleDbTester {
public RuleDto insertRule(Consumer<RuleDto> populateRuleDto) {
RuleDto ruleDto = newRuleDto();
populateRuleDto.accept(ruleDto);

if (ruleDto.getUuid() == null) {
ruleDto.setUuid(Uuids.createFast());
}

return insertRule(ruleDto);
}

@@ -212,7 +212,7 @@ public class RuleDbTester {

public RuleParamDto insertRuleParam(RuleDto rule) {
RuleParamDto param = new RuleParamDto();
param.setRuleId(rule.getId());
param.setRuleUuid(rule.getUuid());
param.setName(randomAlphabetic(10));
param.setType(RuleParamType.STRING.type());
db.getDbClient().ruleDao().insertRuleParam(db.getSession(), rule.getDefinition(), param);
@@ -221,7 +221,7 @@ public class RuleDbTester {
}

public RuleDto insertRule(RuleDefinitionDto ruleDefinition, RuleMetadataDto ruleMetadata) {
db.getDbClient().ruleDao().insertOrUpdate(db.getSession(), ruleMetadata.setRuleId(ruleDefinition.getId()));
db.getDbClient().ruleDao().insertOrUpdate(db.getSession(), ruleMetadata.setRuleUuid(ruleDefinition.getUuid()));
db.commit();
return new RuleDto(ruleDefinition, ruleMetadata);
}

+ 13
- 11
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleTesting.java 查看文件

@@ -31,6 +31,7 @@ import org.sonar.api.rules.RuleType;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.core.util.UuidFactory;
import org.sonar.core.util.UuidFactoryFast;
import org.sonar.core.util.Uuids;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.rule.RuleDto.Format;
import org.sonar.db.rule.RuleDto.Scope;
@@ -69,7 +70,7 @@ public class RuleTesting {
return new RuleDefinitionDto()
.setRepositoryKey(key.repository())
.setRuleKey(key.rule())
.setId(nextInt(100_000))
.setUuid("rule_uuid_" + randomAlphanumeric(5))
.setName("name_" + randomAlphanumeric(5))
.setDescription("description_" + randomAlphanumeric(5))
.setDescriptionFormat(Format.HTML)
@@ -93,7 +94,7 @@ public class RuleTesting {

public static RuleMetadataDto newRuleMetadata() {
return new RuleMetadataDto()
.setRuleId(nextInt(100_000))
.setRuleUuid("uuid_" + randomAlphanumeric(5))
.setOrganizationUuid("org_" + randomAlphanumeric(5))
.setRemediationBaseEffort(nextInt(10) + "h")
.setRemediationGapMultiplier(nextInt(10) + "h")
@@ -113,7 +114,7 @@ public class RuleTesting {

public static RuleMetadataDto newRuleMetadata(RuleDefinitionDto rule, OrganizationDto organization) {
return newRuleMetadata()
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setOrganizationUuid(organization.getUuid());
}

@@ -124,7 +125,7 @@ public class RuleTesting {

public static RuleParamDto newRuleParam(RuleDefinitionDto rule) {
return new RuleParamDto()
.setRuleId(rule.getId())
.setRuleUuid(rule.getUuid())
.setName("name_" + randomAlphabetic(5))
.setDefaultValue("default_" + randomAlphabetic(5))
.setDescription("description_" + randomAlphabetic(5))
@@ -136,7 +137,7 @@ public class RuleTesting {
.setUuid(uuidFactory.create())
.setOldRepositoryKey(randomAlphanumeric(50))
.setOldRuleKey(randomAlphanumeric(50))
.setRuleId(nextInt(100_000))
.setRuleUuid(randomAlphanumeric(40))
.setCreatedAt(System.currentTimeMillis());
}

@@ -202,6 +203,7 @@ public class RuleTesting {
@Deprecated
public static RuleDto newDto(RuleKey ruleKey, @Nullable OrganizationDto organization) {
RuleDto res = new RuleDto()
.setUuid("uuid_" + Uuids.createFast())
.setRuleKey(ruleKey.rule())
.setRepositoryKey(ruleKey.repository())
.setName("Rule " + ruleKey.rule())
@@ -262,18 +264,18 @@ public class RuleTesting {
*/
@Deprecated
public static RuleDto newCustomRule(RuleDto templateRule) {
checkNotNull(templateRule.getId(), "The template rule need to be persisted before creating this custom rule.");
checkNotNull(templateRule.getUuid(), "The template rule need to be persisted before creating this custom rule.");
return newDto(RuleKey.of(templateRule.getRepositoryKey(), templateRule.getRuleKey() + "_" + System.currentTimeMillis()))
.setLanguage(templateRule.getLanguage())
.setTemplateId(templateRule.getId())
.setTemplateUuid(templateRule.getUuid())
.setType(templateRule.getType());
}

public static RuleDefinitionDto newCustomRule(RuleDefinitionDto templateRule) {
checkNotNull(templateRule.getId(), "The template rule need to be persisted before creating this custom rule.");
checkNotNull(templateRule.getUuid(), "The template rule need to be persisted before creating this custom rule.");
return newRule(RuleKey.of(templateRule.getRepositoryKey(), templateRule.getRuleKey() + "_" + System.currentTimeMillis()))
.setLanguage(templateRule.getLanguage())
.setTemplateId(templateRule.getId())
.setTemplateUuid(templateRule.getUuid())
.setType(templateRule.getType());
}

@@ -333,8 +335,8 @@ public class RuleTesting {
return rule -> rule.setIsTemplate(isTemplate);
}

public static Consumer<RuleDefinitionDto> setTemplateId(@Nullable Integer templateId) {
return rule -> rule.setTemplateId(templateId);
public static Consumer<RuleDefinitionDto> setTemplateId(@Nullable String templateUuid) {
return rule -> rule.setTemplateUuid(templateUuid);
}

public static Consumer<RuleDefinitionDto> setSystemTags(String... tags) {

+ 82
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DbVersion83.java 查看文件

@@ -221,6 +221,43 @@ import org.sonar.server.platform.db.migration.version.v83.qualitygates.DropQGate
import org.sonar.server.platform.db.migration.version.v83.qualitygates.DropUniqueIndexOnUuidColumnOfQualityGatesTable;
import org.sonar.server.platform.db.migration.version.v83.qualitygates.MakeQGateUuidColumnNotNullableForQGateConditions;
import org.sonar.server.platform.db.migration.version.v83.qualitygates.PopulateQGateUuidColumnForQGateConditions;
import org.sonar.server.platform.db.migration.version.v83.rules.AddPrimaryKeyOnUuidColumnOfRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.AddUuidAndTemplateUuidColumnsToRules;
import org.sonar.server.platform.db.migration.version.v83.rules.DropIdColumnOfRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.DropPrimaryKeyOnIdColumnOfRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.DropTemplateIdColumnOfRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.MakeRulesUuidColumnNotNullable;
import org.sonar.server.platform.db.migration.version.v83.rules.PopulateRulesTemplateUuid;
import org.sonar.server.platform.db.migration.version.v83.rules.PopulateRulesUuid;
import org.sonar.server.platform.db.migration.version.v83.rules.activerules.AddIndexToActiveRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.activerules.AddRuleUuidColumnToActiveRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.activerules.DropIndexOnRuleIdColumnOfActiveRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.activerules.DropRuleIdColumnOfActiveRulesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.activerules.MakeActiveRulesRuleUuidColumnNotNullable;
import org.sonar.server.platform.db.migration.version.v83.rules.activerules.PopulateActiveRulesRuleUuidColumn;
import org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys.AddIndexToDeprecatedRuleKeysTable;
import org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys.AddRuleUuidColumnToDeprecatedRuleKeysTable;
import org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys.DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTable;
import org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys.DropRuleIdColumnOfDeprecatedRuleKeysTable;
import org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys.MakeDeprecatedRuleKeysRuleUuidColumnNotNullable;
import org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys.PopulateDeprecatedRuleKeysRuleUuidColumn;
import org.sonar.server.platform.db.migration.version.v83.rules.issues.AddIndexToIssuesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.issues.AddRuleUuidColumnToIssuesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.issues.DropIndexOnRuleIdColumnOfIssuesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.issues.DropRuleIdColumnOfIssuesTable;
import org.sonar.server.platform.db.migration.version.v83.rules.issues.PopulateIssuesRuleUuidColumn;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata.AddPrimaryKeyOnUuidAndOrganizationUuidColumnOfRulesMetadataTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata.AddRuleUuidColumnToRulesMetadataTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata.DropPrimaryKeyOnIdColumnOfRulesMetadataTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata.DropRuleIdColumnOfRulesMetadataTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata.MakeRulesMetadataRuleUuidColumnNotNullable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata.PopulateRulesMetadataRuleUuidColumn;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters.AddIndexesToRulesParametersTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters.AddRuleUuidColumnToRulesParametersTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters.DropIndexesOnRuleIdColumnOfRulesParametersTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters.DropRuleIdColumnOfRulesParametersTable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters.MakeRulesParametersRuleUuidColumnNotNullable;
import org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters.PopulateRulesParametersRuleUuidColumn;
import org.sonar.server.platform.db.migration.version.v83.rulesparameters.AddPrimaryKeyOnUuidColumnOfRulesParametersTable;
import org.sonar.server.platform.db.migration.version.v83.rulesparameters.AddUuidColumnToRulesParameters;
import org.sonar.server.platform.db.migration.version.v83.rulesparameters.DropIdColumnOfRulesParametersTable;
@@ -716,6 +753,51 @@ public class DbVersion83 implements DbVersion {
.add(3671, "Add PK index on 'uuid' column of 'USERS' table", AddPrimaryKeyOnUuidColumnOfUsersTable.class)
.add(3672, "Drop 'id' column of 'USERS' table", DropIdColumnOfUsersTable.class)

// Migration of RULES table
.add(3673, "Add 'uuid' column for 'RULES'", AddUuidAndTemplateUuidColumnsToRules.class)
.add(3674, "Populate 'uuid' column for 'RULES'", PopulateRulesUuid.class)
.add(3675, "Make 'uuid' column not nullable for 'RULES'", MakeRulesUuidColumnNotNullable.class)
.add(3676, "Populate 'templateUuid' column for 'RULES'", PopulateRulesTemplateUuid.class)
.add(3677, "Drop column 'templateId' column for 'RULES'", DropTemplateIdColumnOfRulesTable.class)
// Migration of RULES_METADATA FK to RULES, switch from rule_id to rule_uuid
.add(3678, "Add 'RULE_UUID' column for 'RULES_METADATA' table", AddRuleUuidColumnToRulesMetadataTable.class)
.add(3679, "Populate 'RULE_UUID' column for 'RULES_METADATA' table", PopulateRulesMetadataRuleUuidColumn.class)
.add(3680, "Make 'RULE_UUID' column not nullable for 'RULES_METADATA' table", MakeRulesMetadataRuleUuidColumnNotNullable.class)
.add(3681, "Drop primary key on 'RULE_ID' column of 'RULES_METADATA' table", DropPrimaryKeyOnIdColumnOfRulesMetadataTable.class)
.add(3682, "Add primary key on 'RULE_UUID' column of 'RULES_METADATA' table", AddPrimaryKeyOnUuidAndOrganizationUuidColumnOfRulesMetadataTable.class)
.add(3683, "Drop column 'RULE_ID' of 'RULES_METADATA' table", DropRuleIdColumnOfRulesMetadataTable.class)
// Migration of RULES_PARAMETERS FK to RULES, switch from rule_id to rule_uuid
.add(3684, "Add 'RULE_UUID' column for 'RULES_PARAMETERS' table", AddRuleUuidColumnToRulesParametersTable.class)
.add(3685, "Populate 'RULE_UUID' column for 'RULES_PARAMETERS' table", PopulateRulesParametersRuleUuidColumn.class)
.add(3686, "Make 'RULE_UUID' column not nullable for 'RULES_PARAMETERS' table", MakeRulesParametersRuleUuidColumnNotNullable.class)
.add(3687, "Drop indexes on 'RULE_ID' of 'RULES_PARAMETERS' table", DropIndexesOnRuleIdColumnOfRulesParametersTable.class)
.add(3688, "Add indexes to 'RULES_PARAMETERS' table", AddIndexesToRulesParametersTable.class)
.add(3689, "Drop column 'RULE_ID' of 'RULES_PARAMETERS' table", DropRuleIdColumnOfRulesParametersTable.class)
// Migration of ACTIVE_RULES FK to RULES, switch from rule_id to rule_uuid
.add(3690, "Add 'RULE_UUID' column for 'ACTIVE_RULES' table", AddRuleUuidColumnToActiveRulesTable.class)
.add(3691, "Populate 'RULE_UUID' column for 'ACTIVE_RULES' table", PopulateActiveRulesRuleUuidColumn.class)
.add(3692, "Make 'RULE_UUID' column not nullable for 'ACTIVE_RULES' table", MakeActiveRulesRuleUuidColumnNotNullable.class)
.add(3693, "Drop indexes on 'RULE_ID' of 'ACTIVE_RULES' table", DropIndexOnRuleIdColumnOfActiveRulesTable.class)
.add(3694, "Add indexes to 'ACTIVE_RULES' table", AddIndexToActiveRulesTable.class)
.add(3695, "Drop column 'RULE_ID' of 'ACTIVE_RULES' table", DropRuleIdColumnOfActiveRulesTable.class)
// Migration of DEPRECATED_RULE_KEYS FK to RULES, switch from rule_id to rule_uuid
.add(3696, "Add 'RULE_UUID' column for 'DEPRECATED_RULE_KEYS' table", AddRuleUuidColumnToDeprecatedRuleKeysTable.class)
.add(3697, "Populate 'RULE_UUID' column for 'DEPRECATED_RULE_KEYS' table", PopulateDeprecatedRuleKeysRuleUuidColumn.class)
.add(3698, "Make 'RULE_UUID' column not nullable for 'DEPRECATED_RULE_KEYS' table", MakeDeprecatedRuleKeysRuleUuidColumnNotNullable.class)
.add(3699, "Drop index on 'RULE_ID' of 'DEPRECATED_RULE_KEYS' table", DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTable.class)
.add(3700, "Add index to 'DEPRECATED_RULE_KEYS' table", AddIndexToDeprecatedRuleKeysTable.class)
.add(3701, "Drop column 'RULE_ID' of 'DEPRECATED_RULE_KEYS' table", DropRuleIdColumnOfDeprecatedRuleKeysTable.class)
// Migration of ISSUE FK to RULES, switch from rule_id to rule_uuid
.add(3702, "Add 'RULE_UUID' column for 'ISSUES' table", AddRuleUuidColumnToIssuesTable.class)
.add(3703, "Populate 'RULE_UUID' column for 'ISSUES' table", PopulateIssuesRuleUuidColumn.class)
.add(3704, "Drop index on 'RULE_ID' of 'ISSUES' table", DropIndexOnRuleIdColumnOfIssuesTable.class)
.add(3705, "Add index to 'ISSUES' table", AddIndexToIssuesTable.class)
.add(3706, "Drop column 'RULE_ID' of 'ISSUES' table", DropRuleIdColumnOfIssuesTable.class)
// continue with RULES table cleanup
.add(3707, "Drop primary key on 'ID' column of 'RULES' table", DropPrimaryKeyOnIdColumnOfRulesTable.class)
.add(3708, "Add primary key on 'UUID' column of 'RULES' table", AddPrimaryKeyOnUuidColumnOfRulesTable.class)
.add(3709, "Drop column 'ID' of 'RULES' table", DropIdColumnOfRulesTable.class)

;
}
}

+ 38
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/AddPrimaryKeyOnUuidColumnOfRulesTable.java 查看文件

@@ -0,0 +1,38 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DdlChange;
import org.sonar.server.platform.db.migration.version.v83.util.AddPrimaryKeyBuilder;

public class AddPrimaryKeyOnUuidColumnOfRulesTable extends DdlChange {

public AddPrimaryKeyOnUuidColumnOfRulesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddPrimaryKeyBuilder("rules", "uuid").build());
}

}

+ 58
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/AddUuidAndTemplateUuidColumnsToRules.java 查看文件

@@ -0,0 +1,58 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddUuidAndTemplateUuidColumnsToRules extends DdlChange {
private static final String TABLE = "rules";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("uuid")
.setIsNullable(true)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

private static final VarcharColumnDef templateUuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("template_uuid")
.setIsNullable(true)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddUuidAndTemplateUuidColumnsToRules(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddColumnsBuilder(getDialect(), TABLE)
.addColumn(uuidColumnDefinition)
.addColumn(templateUuidColumnDefinition)
.build());
}
}

+ 37
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/DropIdColumnOfRulesTable.java 查看文件

@@ -0,0 +1,37 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropIdColumnOfRulesTable extends DdlChange {

public DropIdColumnOfRulesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), "rules", "id").build());
}
}

+ 41
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/DropPrimaryKeyOnIdColumnOfRulesTable.java 查看文件

@@ -0,0 +1,41 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DdlChange;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;

public class DropPrimaryKeyOnIdColumnOfRulesTable extends DdlChange {

private final DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator;

public DropPrimaryKeyOnIdColumnOfRulesTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator) {
super(db);
this.dropPrimaryKeySqlGenerator = dropPrimaryKeySqlGenerator;
}

@Override
public void execute(Context context) throws SQLException {
context.execute(dropPrimaryKeySqlGenerator.generate("rules", "id", true));
}

}

+ 37
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/DropTemplateIdColumnOfRulesTable.java 查看文件

@@ -0,0 +1,37 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropTemplateIdColumnOfRulesTable extends DdlChange {

public DropTemplateIdColumnOfRulesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), "rules", "template_id").build());
}
}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/MakeRulesUuidColumnNotNullable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class MakeRulesUuidColumnNotNullable extends DdlChange {
private static final String TABLE = "rules";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("uuid")
.setIsNullable(false)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public MakeRulesUuidColumnNotNullable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AlterColumnsBuilder(getDialect(), TABLE)
.updateColumn(uuidColumnDefinition)
.build());
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesTemplateUuid.java 查看文件

@@ -0,0 +1,49 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class PopulateRulesTemplateUuid extends DataChange {

public PopulateRulesTemplateUuid(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();

massUpdate.select(
"select ru.id, rt.uuid from rules ru " +
"left join rules rt on rt.id = ru.template_id " +
"where ru.template_uuid is null and ru.template_id is not null");
massUpdate.update("update rules set template_uuid = ? where id = ?");

massUpdate.execute((row, update) -> {
update.setString(1, row.getString(2));
update.setLong(2, row.getLong(1));
return true;
});
}
}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesUuid.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class PopulateRulesUuid extends DataChange {

private final UuidFactory uuidFactory;

public PopulateRulesUuid(Database db, UuidFactory uuidFactory) {
super(db);
this.uuidFactory = uuidFactory;
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();

massUpdate.select("select id from rules where uuid is null");
massUpdate.update("update rules set uuid = ? where id = ?");

massUpdate.execute((row, update) -> {
update.setString(1, uuidFactory.create());
update.setLong(2, row.getLong(1));
return true;
});
}
}

+ 70
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddIndexToActiveRulesTable.java 查看文件

@@ -0,0 +1,70 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.Connection;
import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddIndexToActiveRulesTable extends DdlChange {
private static final String TABLE = "active_rules";
private static final String INDEX_NAME = "uniq_profile_rule_uuids";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(false)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();
private static final VarcharColumnDef profileUuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("profile_uuid")
.setIsNullable(false)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddIndexToActiveRulesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
if (!indexExists()) {
context.execute(new CreateIndexBuilder()
.setTable(TABLE)
.setName(INDEX_NAME)
.addColumn(profileUuidColumnDefinition)
.addColumn(uuidColumnDefinition)
.setUnique(true)
.build());
}
}

private boolean indexExists() throws SQLException {
try (Connection connection = getDatabase().getDataSource().getConnection()) {
return DatabaseUtils.indexExists(TABLE, INDEX_NAME, connection);
}
}

}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddRuleUuidColumnToActiveRulesTable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddRuleUuidColumnToActiveRulesTable extends DdlChange {
private static final String TABLE = "active_rules";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(true)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddRuleUuidColumnToActiveRulesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddColumnsBuilder(getDialect(), TABLE)
.addColumn(uuidColumnDefinition)
.build());
}
}

+ 43
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropIndexOnRuleIdColumnOfActiveRulesTable.java 查看文件

@@ -0,0 +1,43 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropIndexOnRuleIdColumnOfActiveRulesTable extends DdlChange {
private static final String TABLE_NAME = "active_rules";
private static final String INDEX_NAME = "uniq_profile_rule_ids";

public DropIndexOnRuleIdColumnOfActiveRulesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropIndexBuilder(getDialect())
.setTable(TABLE_NAME)
.setName(INDEX_NAME)
.build());
}

}

+ 37
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropRuleIdColumnOfActiveRulesTable.java 查看文件

@@ -0,0 +1,37 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropRuleIdColumnOfActiveRulesTable extends DdlChange {

public DropRuleIdColumnOfActiveRulesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), "active_rules", "rule_id").build());
}
}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/MakeActiveRulesRuleUuidColumnNotNullable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class MakeActiveRulesRuleUuidColumnNotNullable extends DdlChange {
private static final String TABLE = "active_rules";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(false)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public MakeActiveRulesRuleUuidColumnNotNullable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AlterColumnsBuilder(getDialect(), TABLE)
.updateColumn(uuidColumnDefinition)
.build());
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/PopulateActiveRulesRuleUuidColumn.java 查看文件

@@ -0,0 +1,49 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class PopulateActiveRulesRuleUuidColumn extends DataChange {

public PopulateActiveRulesRuleUuidColumn(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();

massUpdate.select("select ar.rule_id, ru.uuid " +
"from active_rules ar " +
"join rules ru on ar.rule_id = ru.id " +
"where ar.rule_uuid is null");
massUpdate.update("update active_rules set rule_uuid = ? where rule_id = ?");

massUpdate.execute((row, update) -> {
update.setString(1, row.getString(2));
update.setLong(2, row.getLong(1));
return true;
});
}
}

+ 63
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddIndexToDeprecatedRuleKeysTable.java 查看文件

@@ -0,0 +1,63 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.Connection;
import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddIndexToDeprecatedRuleKeysTable extends DdlChange {
private static final String TABLE = "deprecated_rule_keys";
private static final String INDEX_NAME = "rule_uuid_deprecated_rule_keys";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(false)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddIndexToDeprecatedRuleKeysTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
if (!indexExists()) {
context.execute(new CreateIndexBuilder()
.setTable(TABLE)
.setName(INDEX_NAME)
.addColumn(uuidColumnDefinition)
.build());
}
}

private boolean indexExists() throws SQLException {
try (Connection connection = getDatabase().getDataSource().getConnection()) {
return DatabaseUtils.indexExists(TABLE, INDEX_NAME, connection);
}
}

}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddRuleUuidColumnToDeprecatedRuleKeysTable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddRuleUuidColumnToDeprecatedRuleKeysTable extends DdlChange {
private static final String TABLE = "deprecated_rule_keys";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(true)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddRuleUuidColumnToDeprecatedRuleKeysTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddColumnsBuilder(getDialect(), TABLE)
.addColumn(uuidColumnDefinition)
.build());
}
}

+ 41
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTable.java 查看文件

@@ -0,0 +1,41 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTable extends DdlChange {

public DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropIndexBuilder(getDialect())
.setTable("deprecated_rule_keys")
.setName("rule_id_deprecated_rule_keys")
.build());
}

}

+ 37
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropRuleIdColumnOfDeprecatedRuleKeysTable.java 查看文件

@@ -0,0 +1,37 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropRuleIdColumnOfDeprecatedRuleKeysTable extends DdlChange {

public DropRuleIdColumnOfDeprecatedRuleKeysTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), "deprecated_rule_keys", "rule_id").build());
}
}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/MakeDeprecatedRuleKeysRuleUuidColumnNotNullable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class MakeDeprecatedRuleKeysRuleUuidColumnNotNullable extends DdlChange {
private static final String TABLE = "deprecated_rule_keys";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(false)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public MakeDeprecatedRuleKeysRuleUuidColumnNotNullable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AlterColumnsBuilder(getDialect(), TABLE)
.updateColumn(uuidColumnDefinition)
.build());
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/PopulateDeprecatedRuleKeysRuleUuidColumn.java 查看文件

@@ -0,0 +1,49 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class PopulateDeprecatedRuleKeysRuleUuidColumn extends DataChange {

public PopulateDeprecatedRuleKeysRuleUuidColumn(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();

massUpdate.select("select drk.rule_id, ru.uuid " +
"from deprecated_rule_keys drk " +
"join rules ru on drk.rule_id = ru.id " +
"where drk.rule_uuid is null");
massUpdate.update("update deprecated_rule_keys set rule_uuid = ? where rule_id = ?");

massUpdate.execute((row, update) -> {
update.setString(1, row.getString(2));
update.setLong(2, row.getLong(1));
return true;
});
}
}

+ 62
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/AddIndexToIssuesTable.java 查看文件

@@ -0,0 +1,62 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.issues;

import java.sql.Connection;
import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddIndexToIssuesTable extends DdlChange {
private static final String TABLE = "issues";
private static final String INDEX_NAME = "issues_rule_uuid";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(true)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddIndexToIssuesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
if (!indexExists()) {
context.execute(new CreateIndexBuilder()
.setTable(TABLE)
.setName(INDEX_NAME)
.addColumn(uuidColumnDefinition)
.build());
}
}

private boolean indexExists() throws SQLException {
try (Connection connection = getDatabase().getDataSource().getConnection()) {
return DatabaseUtils.indexExists(TABLE, INDEX_NAME, connection);
}
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/AddRuleUuidColumnToIssuesTable.java 查看文件

@@ -0,0 +1,49 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.issues;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddRuleUuidColumnToIssuesTable extends DdlChange {
private static final String TABLE = "issues";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(true)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddRuleUuidColumnToIssuesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddColumnsBuilder(getDialect(), TABLE)
.addColumn(uuidColumnDefinition)
.build());
}
}

+ 41
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/DropIndexOnRuleIdColumnOfIssuesTable.java 查看文件

@@ -0,0 +1,41 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.issues;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropIndexOnRuleIdColumnOfIssuesTable extends DdlChange {

public DropIndexOnRuleIdColumnOfIssuesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropIndexBuilder(getDialect())
.setTable("issues")
.setName("issues_rule_id")
.build());
}

}

+ 37
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/DropRuleIdColumnOfIssuesTable.java 查看文件

@@ -0,0 +1,37 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.issues;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropRuleIdColumnOfIssuesTable extends DdlChange {

public DropRuleIdColumnOfIssuesTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), "issues", "rule_id").build());
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/issues/PopulateIssuesRuleUuidColumn.java 查看文件

@@ -0,0 +1,49 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.issues;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class PopulateIssuesRuleUuidColumn extends DataChange {

public PopulateIssuesRuleUuidColumn(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();

massUpdate.select("select iss.rule_id, ru.uuid " +
"from issues iss " +
"join rules ru on iss.rule_id = ru.id " +
"where iss.rule_uuid is null");
massUpdate.update("update issues set rule_uuid = ? where rule_id = ?");

massUpdate.execute((row, update) -> {
update.setString(1, row.getString(2));
update.setLong(2, row.getLong(1));
return true;
});
}
}

+ 38
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/AddPrimaryKeyOnUuidAndOrganizationUuidColumnOfRulesMetadataTable.java 查看文件

@@ -0,0 +1,38 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DdlChange;
import org.sonar.server.platform.db.migration.version.v83.util.AddPrimaryKeyBuilder;

public class AddPrimaryKeyOnUuidAndOrganizationUuidColumnOfRulesMetadataTable extends DdlChange {

public AddPrimaryKeyOnUuidAndOrganizationUuidColumnOfRulesMetadataTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddPrimaryKeyBuilder("rules_metadata", "rule_uuid", "organization_uuid").build());
}

}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/AddRuleUuidColumnToRulesMetadataTable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddRuleUuidColumnToRulesMetadataTable extends DdlChange {
private static final String TABLE = "rules_metadata";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(true)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddRuleUuidColumnToRulesMetadataTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddColumnsBuilder(getDialect(), TABLE)
.addColumn(uuidColumnDefinition)
.build());
}
}

+ 41
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/DropPrimaryKeyOnIdColumnOfRulesMetadataTable.java 查看文件

@@ -0,0 +1,41 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DdlChange;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;

public class DropPrimaryKeyOnIdColumnOfRulesMetadataTable extends DdlChange {

private final DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator;

public DropPrimaryKeyOnIdColumnOfRulesMetadataTable(Database db, DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator) {
super(db);
this.dropPrimaryKeySqlGenerator = dropPrimaryKeySqlGenerator;
}

@Override
public void execute(Context context) throws SQLException {
context.execute(dropPrimaryKeySqlGenerator.generate("rules_metadata", "rule_id", false));
}

}

+ 37
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/DropRuleIdColumnOfRulesMetadataTable.java 查看文件

@@ -0,0 +1,37 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropRuleIdColumnOfRulesMetadataTable extends DdlChange {

public DropRuleIdColumnOfRulesMetadataTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), "rules_metadata", "rule_id").build());
}
}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/MakeRulesMetadataRuleUuidColumnNotNullable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class MakeRulesMetadataRuleUuidColumnNotNullable extends DdlChange {
private static final String TABLE = "rules_metadata";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(false)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public MakeRulesMetadataRuleUuidColumnNotNullable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AlterColumnsBuilder(getDialect(), TABLE)
.updateColumn(uuidColumnDefinition)
.build());
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesmetadata/PopulateRulesMetadataRuleUuidColumn.java 查看文件

@@ -0,0 +1,49 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesmetadata;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class PopulateRulesMetadataRuleUuidColumn extends DataChange {

public PopulateRulesMetadataRuleUuidColumn(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();

massUpdate.select("select rm.rule_id, ru.uuid " +
"from rules_metadata rm " +
"join rules ru on rm.rule_id = ru.id " +
"where rm.rule_uuid is null");
massUpdate.update("update rules_metadata set rule_uuid = ? where rule_id = ?");

massUpdate.execute((row, update) -> {
update.setString(1, row.getString(2));
update.setLong(2, row.getLong(1));
return true;
});
}
}

+ 81
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/AddIndexesToRulesParametersTable.java 查看文件

@@ -0,0 +1,81 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters;

import java.sql.Connection;
import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.db.DatabaseUtils;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddIndexesToRulesParametersTable extends DdlChange {
private static final String TABLE = "rules_parameters";
private static final String RULE_UUID_INDEX = "rules_parameters_rule_uuid";
private static final String RULE_UUID_NAME_UNIQUE_INDEX = "rules_parameters_unique";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(true)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

private static final VarcharColumnDef nameColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("name")
.setLimit(128)
.setIsNullable(false)
.build();

public AddIndexesToRulesParametersTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
if (indexDoesNotExist(RULE_UUID_INDEX)) {
context.execute(new CreateIndexBuilder()
.setTable(TABLE)
.setName(RULE_UUID_INDEX)
.addColumn(uuidColumnDefinition)
.build());
}

if (indexDoesNotExist(RULE_UUID_NAME_UNIQUE_INDEX)) {
context.execute(new CreateIndexBuilder()
.setTable(TABLE)
.setName(RULE_UUID_NAME_UNIQUE_INDEX)
.addColumn(uuidColumnDefinition)
.addColumn(nameColumnDefinition)
.setUnique(true)
.build());
}
}

private boolean indexDoesNotExist(String index) throws SQLException {
try (Connection connection = getDatabase().getDataSource().getConnection()) {
return !DatabaseUtils.indexExists(TABLE, index, connection);
}
}

}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/AddRuleUuidColumnToRulesParametersTable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AddRuleUuidColumnToRulesParametersTable extends DdlChange {
private static final String TABLE = "rules_parameters";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(true)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public AddRuleUuidColumnToRulesParametersTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AddColumnsBuilder(getDialect(), TABLE)
.addColumn(uuidColumnDefinition)
.build());
}
}

+ 45
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/DropIndexesOnRuleIdColumnOfRulesParametersTable.java 查看文件

@@ -0,0 +1,45 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropIndexBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropIndexesOnRuleIdColumnOfRulesParametersTable extends DdlChange {

public DropIndexesOnRuleIdColumnOfRulesParametersTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropIndexBuilder(getDialect())
.setTable("rules_parameters")
.setName("rules_parameters_rule_id")
.build());
context.execute(new DropIndexBuilder(getDialect())
.setTable("rules_parameters")
.setName("rules_parameters_unique")
.build());
}

}

+ 37
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/DropRuleIdColumnOfRulesParametersTable.java 查看文件

@@ -0,0 +1,37 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

public class DropRuleIdColumnOfRulesParametersTable extends DdlChange {

public DropRuleIdColumnOfRulesParametersTable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new DropColumnsBuilder(getDialect(), "rules_parameters", "rule_id").build());
}
}

+ 50
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/MakeRulesParametersRuleUuidColumnNotNullable.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;

public class MakeRulesParametersRuleUuidColumnNotNullable extends DdlChange {
private static final String TABLE = "rules_parameters";

private static final VarcharColumnDef uuidColumnDefinition = newVarcharColumnDefBuilder()
.setColumnName("rule_uuid")
.setIsNullable(false)
.setDefaultValue(null)
.setLimit(VarcharColumnDef.UUID_SIZE)
.build();

public MakeRulesParametersRuleUuidColumnNotNullable(Database db) {
super(db);
}

@Override
public void execute(Context context) throws SQLException {
context.execute(new AlterColumnsBuilder(getDialect(), TABLE)
.updateColumn(uuidColumnDefinition)
.build());
}
}

+ 49
- 0
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/rules/rulesparameters/PopulateRulesParametersRuleUuidColumn.java 查看文件

@@ -0,0 +1,49 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.rulesparameters;

import java.sql.SQLException;
import org.sonar.db.Database;
import org.sonar.server.platform.db.migration.step.DataChange;
import org.sonar.server.platform.db.migration.step.MassUpdate;

public class PopulateRulesParametersRuleUuidColumn extends DataChange {

public PopulateRulesParametersRuleUuidColumn(Database db) {
super(db);
}

@Override
protected void execute(Context context) throws SQLException {
MassUpdate massUpdate = context.prepareMassUpdate();

massUpdate.select("select rp.rule_id, ru.uuid " +
"from rules_parameters rp " +
"join rules ru on rp.rule_id = ru.id " +
"where rp.rule_uuid is null");
massUpdate.update("update rules_parameters set rule_uuid = ? where rule_id = ?");

massUpdate.execute((row, update) -> {
update.setString(1, row.getString(2));
update.setLong(2, row.getLong(1));
return true;
});
}
}

+ 50
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/AddPrimaryKeyOnUuidColumnOfRulesTableTest.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class AddPrimaryKeyOnUuidColumnOfRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(AddPrimaryKeyOnUuidColumnOfRulesTableTest.class, "schema.sql");

private MigrationStep underTest = new AddPrimaryKeyOnUuidColumnOfRulesTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertPrimaryKey("rules", "pk_rules", "uuid");
}

@Test
public void migration_is_not_re_entrant() throws SQLException {
underTest.execute();

assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
}
}

+ 66
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/AddUuidAndTemplateUuidColumnsToRulesTest.java 查看文件

@@ -0,0 +1,66 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import java.sql.Types;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.assertj.core.api.Assertions.assertThat;

public class AddUuidAndTemplateUuidColumnsToRulesTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(AddUuidAndTemplateUuidColumnsToRulesTest.class, "schema.sql");

private DdlChange underTest = new AddUuidAndTemplateUuidColumnsToRules(db.database());

@Before
public void setup() {
insertRule(1L);
insertRule(2L);
insertRule(3L);
insertRule(4L);
}

@Test
public void add_uuid_column() throws SQLException {
underTest.execute();

db.assertColumnDefinition("rules", "uuid", Types.VARCHAR, 40, true);

assertThat(db.countSql("select count(*) from rules"))
.isEqualTo(4);
}

private void insertRule(long id) {
db.executeInsert("rules",
"id", id,
"plugin_rule_key", "rk" + id,
"plugin_name", "rn" + id,
"scope", "MAIN",
"is_ad_hoc", false,
"is_external", false);
}
}

+ 50
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/DropIdColumnOfRulesTableTest.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DropIdColumnOfRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropIdColumnOfRulesTableTest.class, "schema.sql");

private DdlChange underTest = new DropIdColumnOfRulesTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertColumnDoesNotExist("rules", "id");
}

@Test
public void migration_is_not_re_entrant() throws SQLException {
underTest.execute();

assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
}
}

+ 56
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/DropPrimaryKeyOnIdColumnOfRulesTableTest.java 查看文件

@@ -0,0 +1,56 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DropPrimaryKeyOnIdColumnOfRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfRulesTableTest.class, "schema.sql");

private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfRulesTable(db.database(),
new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database())));

@Test
public void execute() throws SQLException {
db.assertTableExists("rules");
db.assertPrimaryKey("rules", "pk_rules", "id");

underTest.execute();

db.assertNoPrimaryKey("rules");
}

@Test
public void migration_is_not_re_entrant() throws SQLException {
underTest.execute();

assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
}
}

+ 50
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/DropTemplateIdColumnOfRulesTableTest.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DropTemplateIdColumnOfRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropTemplateIdColumnOfRulesTableTest.class, "schema.sql");

private DdlChange underTest = new DropTemplateIdColumnOfRulesTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertColumnDoesNotExist("rules", "template_id");
}

@Test
public void migration_is_not_re_entrant() throws SQLException {
underTest.execute();

assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
}
}

+ 43
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/MakeRulesUuidColumnNotNullableTest.java 查看文件

@@ -0,0 +1,43 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;

import static java.sql.Types.VARCHAR;

public class MakeRulesUuidColumnNotNullableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(MakeRulesUuidColumnNotNullableTest.class, "schema.sql");

private MigrationStep underTest = new MakeRulesUuidColumnNotNullable(db.database());

@Test
public void uuid_column_is_not_null() throws SQLException {
underTest.execute();

db.assertColumnDefinition("rules", "uuid", VARCHAR, null, false);
}
}

+ 74
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesTemplateUuidTest.java 查看文件

@@ -0,0 +1,74 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import javax.annotation.Nullable;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DataChange;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

public class PopulateRulesTemplateUuidTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(PopulateRulesTemplateUuidTest.class, "schema.sql");

private DataChange underTest = new PopulateRulesTemplateUuid(db.database());

@Before
public void setup() {
insertRule(1L, "uuid-1", 1L);
insertRule(2L, "uuid-2", 1L);
insertRule(3L, "uuid-3", null);
insertRule(4L, "uuid-4", 2L);
}

@Test
public void add_rule_uuid_column() throws SQLException {
underTest.execute();

assertThat(db.countSql("select count(*) from rules"))
.isEqualTo(4);
assertThat(db.select("select uuid, template_id, template_uuid from rules"))
.extracting(m -> m.get("UUID"), m -> m.get("TEMPLATE_ID"), m -> m.get("TEMPLATE_UUID"))
.containsExactlyInAnyOrder(
tuple("uuid-1", 1L, "uuid-1"),
tuple("uuid-2", 1L, "uuid-1"),
tuple("uuid-3", null, null),
tuple("uuid-4", 2L, "uuid-2"));
}

private void insertRule(long id, String uuid, @Nullable Long templateId) {
db.executeInsert("rules",
"id", id,
"uuid", uuid,
"template_id", templateId,
"plugin_rule_key", "rk" + id,
"plugin_name", "rn" + id,
"scope", "MAIN",
"is_ad_hoc", false,
"is_external", false);
}
}

+ 75
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/PopulateRulesUuidTest.java 查看文件

@@ -0,0 +1,75 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules;

import java.sql.SQLException;
import java.util.Objects;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.core.util.UuidFactory;
import org.sonar.core.util.UuidFactoryFast;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DataChange;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.ArgumentMatchers.anyString;

public class PopulateRulesUuidTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(PopulateRulesUuidTest.class, "schema.sql");

private UuidFactory uuidFactory = UuidFactoryFast.getInstance();
private DataChange underTest = new PopulateRulesUuid(db.database(), uuidFactory);

@Before
public void setup() {
insertRule(1L);
insertRule(2L);
insertRule(3L);
insertRule(4L);
}

@Test
public void add_rule_uuid_column() throws SQLException {
underTest.execute();

assertThat(db.countSql("select count(*) from rules"))
.isEqualTo(4);
assertThat(db.select("select uuid from rules")
.stream()
.map(row -> row.get("UUID"))
.filter(Objects::isNull)
.collect(Collectors.toList())).isEmpty();
}

private void insertRule(long id) {
db.executeInsert("rules",
"id", id,
"plugin_rule_key", "rk" + id,
"plugin_name", "rn" + id,
"scope", "MAIN",
"is_ad_hoc", false,
"is_external", false);
}
}

+ 51
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddIndexToActiveRulesTableTest.java 查看文件

@@ -0,0 +1,51 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;

public class AddIndexToActiveRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(AddIndexToActiveRulesTableTest.class, "schema.sql");

private MigrationStep underTest = new AddIndexToActiveRulesTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertUniqueIndex("active_rules", "uniq_profile_rule_uuids", "profile_uuid", "rule_uuid");
}

@Test
public void migration_is_re_entrant() throws SQLException {
underTest.execute();

// re-entrant
underTest.execute();

db.assertUniqueIndex("active_rules", "uniq_profile_rule_uuids", "profile_uuid", "rule_uuid");
}
}

+ 79
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/AddRuleUuidColumnToActiveRulesTableTest.java 查看文件

@@ -0,0 +1,79 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import java.sql.Types;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.assertj.core.api.Assertions.assertThat;

public class AddRuleUuidColumnToActiveRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(AddRuleUuidColumnToActiveRulesTableTest.class, "schema.sql");

private DdlChange underTest = new AddRuleUuidColumnToActiveRulesTable(db.database());

@Before
public void setup() {
insertRule(1L, "uuid-rule-1");
insertRule(2L, "uuid-rule-2");
insertRule(3L, "uuid-rule-3");
insertRule(4L, "uuid-rule-4");

insertActiveRule("uuid-ar-1", 1L, "uuid-profile-1");
insertActiveRule("uuid-ar-2", 1L, "uuid-profile-2");
insertActiveRule("uuid-ar-3", 2L, "uuid-profile-1");
}

@Test
public void add_rule_uuid_column() throws SQLException {
underTest.execute();

db.assertColumnDefinition("active_rules", "rule_uuid", Types.VARCHAR, 40, true);
assertThat(db.countSql("select count(*) from active_rules"))
.isEqualTo(3);
}

private void insertRule(long id, String uuid) {
db.executeInsert("rules",
"id", id,
"uuid", uuid,
"plugin_rule_key", "rk" + id,
"plugin_name", "rn" + id,
"scope", "MAIN",
"is_ad_hoc", false,
"is_external", false);
}

private void insertActiveRule(String uuid, long ruleId, String profileUuid) {
db.executeInsert("active_rules",
"uuid", uuid,
"rule_id", ruleId,
"profile_uuid", profileUuid,
"failure_level", 1);
}

}

+ 54
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropIndexOnRuleIdColumnOfActiveRulesTableTest.java 查看文件

@@ -0,0 +1,54 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;

public class DropIndexOnRuleIdColumnOfActiveRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropIndexOnRuleIdColumnOfActiveRulesTableTest.class, "schema.sql");

private MigrationStep underTest = new DropIndexOnRuleIdColumnOfActiveRulesTable(db.database());

@Test
public void execute() throws SQLException {
db.assertTableExists("active_rules");
db.assertUniqueIndex("active_rules", "uniq_profile_rule_ids", "profile_uuid", "rule_id");

underTest.execute();

db.assertIndexDoesNotExist("active_rules", "uniq_profile_rule_ids");
}

@Test
public void migration_is_re_entrant() throws SQLException {
underTest.execute();

// re-entrant
underTest.execute();

db.assertIndexDoesNotExist("active_rules", "uniq_profile_rule_ids");
}
}

+ 50
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/DropRuleIdColumnOfActiveRulesTableTest.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DropRuleIdColumnOfActiveRulesTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropRuleIdColumnOfActiveRulesTableTest.class, "schema.sql");

private DdlChange underTest = new DropRuleIdColumnOfActiveRulesTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertColumnDoesNotExist("active_rules", "id");
}

@Test
public void migration_is_not_re_entrant() throws SQLException {
underTest.execute();

assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
}
}

+ 43
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/MakeActiveRulesRuleUuidColumnNotNullableTest.java 查看文件

@@ -0,0 +1,43 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;

import static java.sql.Types.VARCHAR;

public class MakeActiveRulesRuleUuidColumnNotNullableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(MakeActiveRulesRuleUuidColumnNotNullableTest.class, "schema.sql");

private MigrationStep underTest = new MakeActiveRulesRuleUuidColumnNotNullable(db.database());

@Test
public void uuid_column_is_not_null() throws SQLException {
underTest.execute();

db.assertColumnDefinition("active_rules", "rule_uuid", VARCHAR, null, false);
}
}

+ 84
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/activerules/PopulateActiveRulesRuleUuidColumnTest.java 查看文件

@@ -0,0 +1,84 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.activerules;

import java.sql.SQLException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DataChange;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

public class PopulateActiveRulesRuleUuidColumnTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(PopulateActiveRulesRuleUuidColumnTest.class, "schema.sql");

private DataChange underTest = new PopulateActiveRulesRuleUuidColumn(db.database());

@Before
public void setup() {
insertRule(1L, "uuid-rule-1");
insertRule(2L, "uuid-rule-2");
insertRule(3L, "uuid-rule-3");
insertRule(4L, "uuid-rule-4");

insertActiveRule("uuid-ar-1", 1L, "uuid-profile-1");
insertActiveRule("uuid-ar-2", 1L, "uuid-profile-2");
insertActiveRule("uuid-ar-3", 2L, "uuid-profile-1");
}

@Test
public void add_rule_uuid_column() throws SQLException {
underTest.execute();

assertThat(db.countSql("select count(*) from active_rules"))
.isEqualTo(3);
assertThat(db.select("select uuid, rule_id, rule_uuid from active_rules"))
.extracting(m -> m.get("UUID"), m -> m.get("RULE_ID"), m -> m.get("RULE_UUID"))
.containsExactlyInAnyOrder(
tuple("uuid-ar-1", 1L, "uuid-rule-1"),
tuple("uuid-ar-2", 1L, "uuid-rule-1"),
tuple("uuid-ar-3", 2L, "uuid-rule-2"));
}

private void insertRule(long id, String uuid) {
db.executeInsert("rules",
"id", id,
"uuid", uuid,
"plugin_rule_key", "rk" + id,
"plugin_name", "rn" + id,
"scope", "MAIN",
"is_ad_hoc", false,
"is_external", false);
}

private void insertActiveRule(String uuid, long ruleId, String profileUuid) {
db.executeInsert("active_rules",
"uuid", uuid,
"rule_id", ruleId,
"profile_uuid", profileUuid,
"failure_level", 1);
}

}

+ 51
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddIndexToDeprecatedRuleKeysTableTest.java 查看文件

@@ -0,0 +1,51 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;

public class AddIndexToDeprecatedRuleKeysTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(AddIndexToDeprecatedRuleKeysTableTest.class, "schema.sql");

private MigrationStep underTest = new AddIndexToDeprecatedRuleKeysTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertIndex("deprecated_rule_keys", "rule_uuid_deprecated_rule_keys", "rule_uuid");
}

@Test
public void migration_is_re_entrant() throws SQLException {
underTest.execute();

// re-entrant
underTest.execute();

db.assertIndex("deprecated_rule_keys", "rule_uuid_deprecated_rule_keys", "rule_uuid");
}
}

+ 80
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/AddRuleUuidColumnToDeprecatedRuleKeysTableTest.java 查看文件

@@ -0,0 +1,80 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import java.sql.Types;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.assertj.core.api.Assertions.assertThat;

public class AddRuleUuidColumnToDeprecatedRuleKeysTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(AddRuleUuidColumnToDeprecatedRuleKeysTableTest.class, "schema.sql");

private DdlChange underTest = new AddRuleUuidColumnToDeprecatedRuleKeysTable(db.database());

@Before
public void setup() {
insertRule(1L, "uuid-rule-1");
insertRule(2L, "uuid-rule-2");
insertRule(3L, "uuid-rule-3");

insertDeprecatedRuleKeyRow("uuid-drk-1", 1L);
insertDeprecatedRuleKeyRow("uuid-drk-2", 1L);
insertDeprecatedRuleKeyRow("uuid-drk-3", 2L);
}

@Test
public void add_rule_uuid_column() throws SQLException {
underTest.execute();

db.assertColumnDefinition("deprecated_rule_keys", "rule_uuid", Types.VARCHAR, 40, true);
assertThat(db.countSql("select count(*) from deprecated_rule_keys"))
.isEqualTo(3);
}

private void insertRule(long id, String uuid) {
db.executeInsert("rules",
"id", id,
"uuid", uuid,
"plugin_rule_key", "rk" + id,
"plugin_name", "rn" + id,
"scope", "MAIN",
"is_ad_hoc", false,
"is_external", false);
}

private void insertDeprecatedRuleKeyRow(String uuid, long ruleId) {
db.executeInsert("deprecated_rule_keys",
"uuid", uuid,
"rule_id", ruleId,
"old_repository_key", "old-repo-key" + uuid,
"old_rule_key", "old-rule-key" + uuid,
"created_at", System2.INSTANCE.now());
}

}

+ 54
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTableTest.java 查看文件

@@ -0,0 +1,54 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;

public class DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTableTest.class, "schema.sql");

private MigrationStep underTest = new DropIndexOnRuleIdColumnOfDeprecatedRuleKeysTable(db.database());

@Test
public void execute() throws SQLException {
db.assertTableExists("deprecated_rule_keys");
db.assertIndex("deprecated_rule_keys", "rule_id_deprecated_rule_keys", "rule_id");

underTest.execute();

db.assertIndexDoesNotExist("deprecated_rule_keys", "rule_id_deprecated_rule_keys");
}

@Test
public void migration_is_re_entrant() throws SQLException {
underTest.execute();

// re-entrant
underTest.execute();

db.assertIndexDoesNotExist("deprecated_rule_keys", "rule_id_deprecated_rule_keys");
}
}

+ 50
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/DropRuleIdColumnOfDeprecatedRuleKeysTableTest.java 查看文件

@@ -0,0 +1,50 @@
/*
* SonarQube
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.platform.db.migration.version.v83.rules.deprecatedrulekeys;

import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.DdlChange;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DropRuleIdColumnOfDeprecatedRuleKeysTableTest {

@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropRuleIdColumnOfDeprecatedRuleKeysTableTest.class, "schema.sql");

private DdlChange underTest = new DropRuleIdColumnOfDeprecatedRuleKeysTable(db.database());

@Test
public void execute() throws SQLException {
underTest.execute();

db.assertColumnDoesNotExist("deprecated_rule_keys", "id");
}

@Test
public void migration_is_not_re_entrant() throws SQLException {
underTest.execute();

assertThatThrownBy(() -> underTest.execute()).isInstanceOf(IllegalStateException.class);
}
}

+ 0
- 0
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/rules/deprecatedrulekeys/MakeDeprecatedRuleKeysRuleUuidColumnNotNullableTest.java 查看文件


部分文件因文件數量過多而無法顯示

Loading…
取消
儲存