{"record":{"id":"4b0a9a0c976d14f4","repo":"SonarSource/sonarqube","slug":"fail-to-execute-s-n-caught-s-error-original-w","errorCode":null,"errorMessage":"Fail to execute %s %n (caught %s error, original was %s)","messagePattern":"Fail to execute (.+?) %n \\(caught (.+?) error, original was (.+?)\\)","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":106,"sourceCode":"\n    private void execute(String original, String sql, int errorCount) {\n      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","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/step/DdlChange.java#L88-L124","documentation":"DdlChange.execute wraps SQLExceptions from running a DDL statement in an IllegalStateException. When the SQL was auto-corrected or retried (e.g. Oracle ORA-01442 column-already-NOT-NULL handling strips NOT NULL and retries), the message includes the retry count and the original SQL, formatted as \"Fail to execute <sql> (caught <n> error, original was <original>)\".","triggerScenarios":"A DDL step's SQL throws a SQLException that the retry logic (errorCount > 0 or modified SQL) could not ultimately resolve — the second/final attempt also failed.","commonSituations":"Oracle migrations where a column is already NOT NULL and the retry with stripped NOT NULL still fails; syntax or permission errors on partially-migrated schemas; schema drift between environments.","solutions":["Read the wrapped cause (the SQLException and its vendor code) to find the real database error.","Inspect the printed original vs executed SQL to spot what the retry logic changed.","Check DB user privileges and whether the migration is being re-run on a schema already migrated by a newer version."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Pre-check for Oracle NOT NULL redundancy to avoid the retry path failing again\n// SELECT NULLABLE FROM user_tab_columns WHERE table_name=? AND column_name=?  -> if 'Y', skip NOT NULL add","typeGuard":null,"tryCatchPattern":"try {\n  ddl.execute(context);\n} catch (IllegalStateException e) {\n  Throwable root = e.getCause();\n  if (root instanceof SQLException se) {\n    LOG.error(\"DDL failed (vendor code {}): {}\", se.getErrorCode(), se.getMessage());\n  }\n  throw e;\n}","preventionTips":["Inspect the wrapped SQLException vendor code, not just the wrapper message.","Compare the 'original was' SQL against the executed SQL to see retries applied.","Restore schema to a clean pre-migration state before re-running 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"}