{"record":{"id":"9810208d1484cb57","repo":"SonarSource/sonarqube","slug":"fail-to-execute-s","errorCode":null,"errorMessage":"Fail to execute %s","messagePattern":"Fail to execute (.+?)","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DdlChange.java","lineNumber":108,"sourceCode":"      try (Statement stmt = writeConnection.createStatement()) {\n        stmt.execute(sql);\n        writeConnection.commit();\n      } catch (SQLException e) {\n        if (errorCount < ERROR_HANDLING_THRESHOLD) {\n          String message = e.getMessage();\n          if (message.contains(\"ORA-01451\")) {\n            String newSql = nullPattern.matcher(sql).replaceFirst(\"\");\n            execute(original, newSql, errorCount + 1);\n            return;\n          } else if (message.contains(\"ORA-01442\")) {\n            String newSql = notNullPattern.matcher(sql).replaceFirst(\"\");\n            execute(original, newSql, errorCount + 1);\n            return;\n          }\n        }\n        throw new IllegalStateException(messageForIseOf(original, sql, errorCount), e);\n      } catch (Exception e) {\n        throw new IllegalStateException(messageForIseOf(original, sql, errorCount), e);\n      }\n    }\n\n    private static String messageForIseOf(String original, String sql, int errorCount) {\n      if (!original.equals(sql) || errorCount > 0) {\n        return format(\"Fail to execute %s %n (caught %s error, original was %s)\", sql, errorCount, original);\n      } else {\n        return format(\"Fail to execute %s\", sql);\n      }\n    }\n\n    @Override\n    public void execute(String... sqls) {\n      execute(asList(sqls));\n    }\n\n    @Override\n    public void execute(List<String> sqls) {","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DdlChange.java#L90-L126","documentation":"The non-retry path of DdlChange.execute: a plain Exception/SQLException from executing DDL on the first attempt, with no auto-correction applied, is wrapped in IllegalStateException with message \"Fail to execute <sql>\". The exception preserves the failing SQL as the message so developers can see exactly which statement broke.","triggerScenarios":"Any DDL statement from a migration step (CreateTableBuilder, CreateIndexBuilder, etc. via SqlStatement.execute) fails on first execution — e.g. table already exists, duplicate index, insufficient privileges.","commonSituations":"Re-running migrations against a dirty schema; migrating a database restored from a different SonarQube version; database permissions/locks preventing DDL.","solutions":["Look at the wrapped cause for the vendor error code (ORA-*, SQL Server error, etc.) to diagnose the root issue.","Verify schema state matches the expected pre-migration state (no partial/duplicate objects).","Ensure the DB user has DDL rights; drop/rename conflicting objects if a stale one blocks the migration."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Check object existence before executing DDL\n// H2/PG: SELECT 1 FROM information_schema.tables WHERE table_name = ?\n// Oracle: SELECT 1 FROM user_tables WHERE table_name = ?","typeGuard":null,"tryCatchPattern":"try {\n  ddlChange.execute(context);\n} catch (IllegalStateException e) {\n  if (e.getCause() instanceof SQLException se && se.getErrorCode() == ORA_NAME_ALREADY_USED) {\n    LOG.warn(\"Object already exists, continuing migration\");\n  } else {\n    throw e;\n  }\n}","preventionTips":["Never re-run migrations on a partially migrated schema; check SCHEMA_MIGRATIONS first.","Grant the migration DB user DDL privileges.","Use IF EXISTS dialects (H2/PG/MsSql) or existence checks in custom steps."],"tags":["database","migration","ddl","sql-execution-failed"],"backgroundTag":"database-write-failed","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}