SonarSource/sonarqube · error · java.lang.IllegalStateException

Unsupported dialect for drop of constraint:

Error message

Unsupported dialect for drop of constraint: 

What it means

DropConstraintBuilder.build() generates the ALTER TABLE ... DROP CONSTRAINT statement only for the four supported dialects (MsSql, Oracle, PostgreSql, H2). If the configured Dialect's id is none of these, createSqlStatement throws an IllegalStateException naming the dialect. This is a defensive guard: Sonar's migration SQL builders only support the dialects they ship drivers/support for.

Source

Thrown at server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropConstraintBuilder.java:70

  public DropConstraintBuilder setName(String s) {
    if (s.startsWith("pk_")) {
      throw new IllegalArgumentException("This builder should not be used with primary keys");
    }
    this.constraintName = s;
    return this;
  }

  public List<String> build() {
    validateTableName(tableName);
    validateIndexName(constraintName);
    return singletonList(createSqlStatement());
  }

  private String createSqlStatement() {
    return switch (dialect.getId()) {
      case MsSql.ID, Oracle.ID, PostgreSql.ID, H2.ID -> "ALTER TABLE " + tableName + " DROP CONSTRAINT " + constraintName;
      default -> throw new IllegalStateException("Unsupported dialect for drop of constraint: " + dialect);
    };
  }
}

View on GitHub (pinned to 184c821202)

Solutions

  1. Check sonar.jdbc.dialect / sonar.properties — run migrations only on a supported database (MS SQL, Oracle, PostgreSQL, H2).
  2. If a new dialect was added, update DropConstraintBuilder.createSqlStatement's switch to include its ID.
  3. Fix test harnesses that construct a fake Dialect with an unrecognized id.

Example fix

// before
default -> throw new IllegalStateException("Unsupported dialect for drop of constraint: " + dialect);
// after
case MySql.ID -> "ALTER TABLE " + tableName + " DROP FOREIGN KEY " + constraintName;
default -> throw new IllegalStateException("Unsupported dialect for drop of constraint: " + dialect);
Defensive patterns

Strategy: validation

Validate before calling

// Java
import static org.sonar.db.dialect.DialectId.*;
boolean supported = List.of(MsSql.ID, Oracle.ID, PostgreSql.ID, H2.ID).contains(dialect.getId());
if (!supported) throw new IllegalArgumentException("Drop constraint not supported for dialect " + dialect.getId());

Type guard

boolean supportsDropConstraint(Dialect d) { return Set.of(MsSql.ID, Oracle.ID, PostgreSql.ID, H2.ID).contains(d.getId()); }

Try / catch

try {
  statements = new DropConstraintBuilder(dialect, table, constraint).build();
} catch (IllegalStateException e) {
  LOG.error("Dialect {} cannot drop constraints: {}", dialect.getId(), e.getMessage());
}

Prevention

When it happens

Trigger: Calling DropConstraintBuilder.build() (via MassUpdate/DbVersion steps) with a Dialect whose getId() returns anything other than "mssql", "oracle", "postgresql", or "h2" — e.g. a custom or newer dialect id.

Common situations: Running a custom SonarQube build against an experimental/new database dialect; a plugin or test stub supplying a mock Dialect with an unknown id; a dialect registry updated without updating all SQL builders.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/2642439a7ec5daec. Report an issue: GitHub.