{"record":{"id":"0a6d97d946e354d4","repo":"paperclipai/paperclip","slug":"failed-to-apply-pending-migrations-finalstate-p","errorCode":null,"errorMessage":"Failed to apply pending migrations: ${finalState.pendingMigrations.join(\", \")}","messagePattern":"Failed to apply pending migrations: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/db/src/client.ts","lineNumber":858,"sourceCode":"    const folderMillisByFile = new Map(journalEntries.map((entry) => [entry.fileName, entry.folderMillis]));\n    const migrationTableSchema = await discoverMigrationTableSchema(sql);\n    if (!migrationTableSchema) {\n      return { repairedMigrations, remainingMigrations: state.pendingMigrations };\n    }\n\n    const columnNames = await getMigrationTableColumnNames(sql, migrationTableSchema);\n    const qualifiedTable = `${quoteIdentifier(migrationTableSchema)}.${quoteIdentifier(DRIZZLE_MIGRATIONS_TABLE)}`;\n\n    for (const migrationFile of state.pendingMigrations) {\n      const migrationContent = await readMigrationFileContent(migrationFile);\n      const alreadyApplied = await migrationContentAlreadyApplied(sql, migrationContent);\n      if (!alreadyApplied) break;\n\n      const hash = createHash(\"sha256\").update(migrationContent).digest(\"hex\");\n      const folderMillis = folderMillisByFile.get(migrationFile) ?? Date.now();\n      const existingByHash = columnNames.has(\"hash\")\n        ? await sql.unsafe<{ created_at: string | number | null }[]>(\n            `SELECT created_at FROM ${qualifiedTable} WHERE hash = ${quoteLiteral(hash)} ORDER BY created_at DESC LIMIT 1`,\n          )\n        : [];\n      const existingByName = columnNames.has(\"name\")\n        ? await sql.unsafe<{ created_at: string | number | null }[]>(\n            `SELECT created_at FROM ${qualifiedTable} WHERE name = ${quoteLiteral(migrationFile)} ORDER BY created_at DESC LIMIT 1`,\n          )\n        : [];\n      if (existingByHash.length > 0 || existingByName.length > 0) {\n        if (columnNames.has(\"created_at\")) {\n          const existingHashCreatedAt = Number(existingByHash[0]?.created_at ?? -1);\n          if (existingByHash.length > 0 && Number.isFinite(existingHashCreatedAt) && existingHashCreatedAt < folderMillis) {\n            await sql.unsafe(\n              `UPDATE ${qualifiedTable} SET created_at = ${quoteLiteral(String(folderMillis))} WHERE hash = ${quoteLiteral(hash)} AND created_at < ${quoteLiteral(String(folderMillis))}`,\n            );\n          }\n\n          const existingNameCreatedAt = Number(existingByName[0]?.created_at ?? -1);\n          if (existingByName.length > 0 && Number.isFinite(existingNameCreatedAt) && existingNameCreatedAt < folderMillis) {","sourceCodeStart":840,"sourceCodeEnd":876,"githubUrl":"https://github.com/paperclipai/paperclip/blob/01ad8584922b5d85292b1723cae71fa0d9b07a19/packages/db/src/client.ts#L840-L876","documentation":"Terminal failure of the runtime migration path: manually applying the pending migrations did not bring the database to an up-to-date state (the still-pending list is embedded), so the final verification aborts startup rather than running against a partially migrated schema.","triggerScenarios":"Thrown at packages/db/src/client.ts:778 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the failing migrations and fix them, then re-run migration.","Check database logs for the underlying SQL error."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"01ad8584922b5d85292b1723cae71fa0d9b07a19","analyzedAt":"2026-08-18T22:49:45.177Z","contentChangedAt":"2026-08-18T22:49:45.177Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}