{"record":{"id":"178d8debd3fbbcbe","repo":"SonarSource/sonarqube","slug":"failed-to-insert-row-with-value-s-in-table-s","errorCode":null,"errorMessage":"Failed to insert row with value %s in table %s","messagePattern":"Failed to insert row with value (.+?) in table (.+?)","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"critical","filePath":"server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImpl.java","lineNumber":93,"sourceCode":"      return Optional.empty();\n    } catch (SQLException e) {\n      throw new IllegalStateException(\"Failed to read content of table \" + SCHEMA_MIGRATIONS_TABLE, e);\n    }\n  }\n\n  @Override\n  public void done(RegisteredMigrationStep dbMigration) {\n    long migrationNumber = dbMigration.getMigrationNumber();\n    try (Connection connection = database.getDataSource().getConnection();\n      PreparedStatement statement = connection.prepareStatement(\"insert into schema_migrations(version) values (?)\")) {\n\n      statement.setString(1, String.valueOf(migrationNumber));\n      statement.execute();\n      if (!connection.getAutoCommit()) {\n        connection.commit();\n      }\n    } catch (SQLException e) {\n      throw new IllegalStateException(String.format(\"Failed to insert row with value %s in table %s\", migrationNumber, SCHEMA_MIGRATIONS_TABLE), e);\n    }\n  }\n\n  @Override\n  public long getInitialDbVersion() {\n    return initialDbVersion;\n  }\n\n  private static List<Long> selectVersions(Connection connection) throws SQLException {\n    try (Statement statement = connection.createStatement();\n      ResultSet resultSet = statement.executeQuery(\"select version from \" + SCHEMA_MIGRATIONS_TABLE)) {\n      List<Long> res = new ArrayList<>();\n      while (resultSet.next()) {\n        res.add(resultSet.getLong(1));\n      }\n      return res.stream()\n        .sorted(Comparator.naturalOrder())\n        .toList();","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/history/MigrationHistoryImpl.java#L75-L111","documentation":"MigrationHistoryImpl.done records a completed migration by inserting its number into SCHEMA_MIGRATIONS and committing. An SQLException during the insert/commit is wrapped in IllegalStateException \"Failed to insert row with value %s in table %s\". The migration itself ran, but history could not be persisted, so the migration may be re-applied or the platform halted.","triggerScenarios":"The INSERT into SCHEMA_MIGRATIONS (or its commit) throws SQLException after a migration step completes — permissions, table missing, constraint conflict, or connection loss during the write.","commonSituations":"DB user lacks INSERT privileges; SCHEMA_MIGRATIONS dropped or renamed; duplicate migration number from a re-run; connection dropped mid-migration on flaky networks; read-only replicas or restricted accounts used during migration.","solutions":["Check the wrapped SQLException cause to distinguish permissions, missing table, constraint violation, or connection failure.","Grant the migration user INSERT (ideally full DDL/DML ownership) on the schema and SCHEMA_MIGRATIONS.","If the table is missing or corrupted, restore it from backup or recreate the schema for a fresh migration run.","Retry the start after fixing the cause; unrecorded migrations will be re-applied safely if they are idempotent, otherwise restore the DB snapshot taken before migration."],"exampleFix":"// before — read-only user used for migration\nsonar.jdbc.username=sonar_readonly\n\n// after — owner account with write/DDL rights\nsonar.jdbc.username=sonar_owner","handlingStrategy":"try-catch","validationCode":"SELECT has_table_privilege('sonar_user', 'schema_migrations', 'INSERT') AS can_insert;\nSELECT NOT pg_is_in_recovery() AS is_writable_primary;","typeGuard":null,"tryCatchPattern":"try {\n  history.done(migrationStep);\n} catch (IllegalStateException e) {\n  LOGGER.error(\"Failed to record migration {}; cause={}\", migrationStep.getMigrationNumber(), e.getCause());\n  throw e;\n}","preventionTips":["Use a writable, privileged account for migrations (not read-only replicas).","Snapshot the database before migration runs so failures can be rolled back.","Monitor connection stability during long migrations to avoid mid-write disconnects.","Verify SCHEMA_MIGRATIONS exists and is writable as part of pre-upgrade checks."],"tags":["database","migration","sql","sonarqube","write-failure"],"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-14T05:17:10.506Z"}