{"record":{"id":"bd111a14f914796d","repo":"musistudio/claude-code-router","slug":"database-disk-image-is-malformed-integrity-check","errorCode":null,"errorMessage":"database disk image is malformed: integrity_check returned ${String(status || \"no result\")}","messagePattern":"database disk image is malformed: integrity_check returned (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"packages/core/src/plugins/backend-service.ts","lineNumber":315,"sourceCode":"    return null;\n  }\n  if (typeof value === \"bigint\" || typeof value === \"number\" || typeof value === \"string\") {\n    return value;\n  }\n  if (Buffer.isBuffer(value)) {\n    return value;\n  }\n  if (value instanceof Uint8Array) {\n    return Buffer.from(value);\n  }\n  return String(value);\n}\n\nfunction assertSqliteDatabaseIntegrity(database: SqlDatabase): void {\n  const result = database.exec(\"PRAGMA integrity_check;\");\n  const status = result[0]?.values?.[0]?.[0];\n  if (status !== \"ok\") {\n    throw new Error(`database disk image is malformed: integrity_check returned ${String(status || \"no result\")}`);\n  }\n}\n\nfunction isSqliteOpenCorruptionError(error: unknown): boolean {\n  const message = formatError(error).toLowerCase();\n  return message.includes(\"database disk image is malformed\") ||\n    message.includes(\"integrity_check\") ||\n    message.includes(\"file is not a database\") ||\n    message.includes(\"not an sqlite database\");\n}\n\nfunction nextCorruptSqliteBackupPath(dbFile: string): string {\n  const timestamp = new Date().toISOString().replace(/[:.]/g, \"-\");\n  const base = `${dbFile}.corrupt-${timestamp}`;\n  if (!existsSync(base)) {\n    return base;\n  }\n  for (let index = 1; index < 1000; index += 1) {","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/musistudio/claude-code-router/blob/99f24806c6a2c660b16e53e95211c517448a6c90/packages/core/src/plugins/backend-service.ts#L297-L333","documentation":"assertSqliteDatabaseIntegrity runs PRAGMA integrity_check on the opened database; a status other than \"ok\" (or no result) means the file is corrupted at the page level. It is used by openSqliteDatabaseWithRecovery to decide whether recovery is needed.","triggerScenarios":"Opening a plugin SQLite DB whose file is truncated, has bad pages, or was written by a mismatched SQLite version / interrupted write.","commonSituations":"Power loss or process kill mid-write, disk-full events, syncing the DB file via cloud storage, or 32/64-bit SQLite library mixing.","solutions":["Restore the DB from backup or delete it to let the app recreate it","If the library offers recovery (openSqliteDatabaseWithRecovery), let it rebuild/migrate","Check disk health and stop syncing the raw DB file with dropbox-like tools"],"exampleFix":"// before\nconst db = openDatabase(path);\n// after\nconst db = openSqliteDatabaseWithRecovery(path); // detects corruption and recovers","handlingStrategy":"fallback","validationCode":"null","typeGuard":"const isSqliteCorruption = (e: unknown): boolean => formatError(e).toLowerCase().includes(\"database disk image is malformed\");","tryCatchPattern":"try { db = openDatabase(path); assertSqliteDatabaseIntegrity(db); } catch (e) { if (isSqliteOpenCorruptionError(e)) { await restoreFromBackup(path); db = openDatabase(path); } else throw e; }","preventionTips":["Back up SQLite files regularly","Never cloud-sync live DB files","Use the recovery-aware open path"],"tags":["sqlite","corruption","database"],"backgroundTag":"sqlite-database-corruption","analyzedSha":"99f24806c6a2c660b16e53e95211c517448a6c90","analyzedAt":"2026-08-27T04:11:01.184Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}