Violation of PRIMARY KEY constraint 'pk_rule_tags'. Non-system tag clashes with new system tag

Must-share information (formatted with Markdown):

  • SonarQube Server - Data Center Edition v10.8.1 (101195)
  • Startup the server with our custom plugin
  • what have you tried so far to achieve this: analyzed log file, analyzed Sonar source code, reproduced in local Sonar

The log file shows:
Violation of PRIMARY KEY constraint 'pk_rule_tags'. Cannot insert duplicate key in object 'dbo.rule_tags'

The scenario is as follows:

  • A tag named ‘x’ is added to rule Y in the Sonar UI (non-system tag);
  • The rule definition of Y is extended with the tag ‘x’ in our plugin (system tag);
  • The new plugin is loaded in SonarQube;
  • Startup fails with the above error.

In the source code we find to be suspicious (but it may not be related):
RulesRegistrant.java

 private void removeRule(DbSession session, RulesRegistrationContext recorder, RuleDto rule) {
    LOG.info(format("Disable rule %s", rule.getKey()));
    rule.setStatus(RuleStatus.REMOVED);
    rule.setSystemTags(emptySet());
    update(session, rule);
    // FIXME resetting the tags for all organizations must be handled a different way
    // rule.setTags(Collections.emptySet());
    // update(session, rule.getMetadata());
    recorder.removed(rule);
    if (recorder.getRemoved().count() % 100 == 0) {
      session.commit();
    }
  }

This issue seems to be a bug to us. Do you agree?

Besides the bug fix, we want to have a quick fix.
In our local test, we removed the error-generating row in rule_tags:
delete from rule_tags where value='X' and rule_uuid='UUID-Y';
and this seems to work. Startup succeeds again.
Can you confirm this as a quick fix?

Help much appreciated. Thanks!

Related issue: Upgrade to 10.4.1: ERROR: duplicate key value violates unique constraint "pk_rule_tags" - #9 by Nicolas_Alcaraz

Hi, after 7 days I still see no response. Are Sonar developers present in this forum? Or do I need to post my issue elsewhere?

Hi,

Sorry for the delay. Since you have access to commercial support and your colleague raised a ticket in the support system, there was a bit of confusion since we don’t typically “compete” over topics between the two channels.

Not… really. I’d more call it a design oversight. And since this is the sole complaint I’ve ever seen about it, not one that’s likely to be addressed.

 
HTH,
Ann

Thanks for your reply, Ann. I understand your confusion, no problem. Since I have no access to the commercial support (yet), I chose direct communication, usually better than by proxy.

Well, I understand your hesitation, however, for starters, there are more reports on this issue, see the link above. Furthermore, I think the scenario to get Sonar failure is not very strange:

  1. a (custom) rule is found to lack a tag, sustainability-medium in our case
  2. it is added by hand/UI: a non-system tag
  3. the next ruleset plugin version has the tag correctly defined in the rule: system tag
  4. Boom! Sonar startup failure with duplicate key violation

IMHO If I would develop a product with this defect in it, I would want to fix it, the sooner the better. I care about quality :wink:

Fortunately, we found a work-around:

  1. use a ruleset where the rule does not have the system tag
  2. startup Sonar succeeds, that rule is available again
  3. remove the non-system tag by hand/UI
  4. now we can start using the ruleset plugin with the proper system tag on the rule

This is how we are going to try to fix production this week.

Thanks!
Jeroen