{"record":{"id":"81c3b1d576e481e2","repo":"paperclipai/paperclip","slug":"label-had-drifted-migration-history-repaired-m","errorCode":null,"errorMessage":"${label} had drifted migration history; repaired migration journal entries from existing schema state.","messagePattern":"(.+?) had drifted migration history; repaired migration journal entries from existing schema state\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"server/src/index.ts","lineNumber":208,"sourceCode":"      prompt.close();\n    }\n  }\n  \n  type EnsureMigrationsOptions = {\n    autoApply?: boolean;\n  };\n  \n  async function ensureMigrations(\n    connectionString: string,\n    label: string,\n    opts?: EnsureMigrationsOptions,\n  ): Promise<MigrationSummary> {\n    const autoApply = opts?.autoApply === true;\n    let state = await inspectMigrations(connectionString);\n    if (state.status === \"needsMigrations\" && state.reason === \"pending-migrations\") {\n      const repair = await reconcilePendingMigrationHistory(connectionString);\n      if (repair.repairedMigrations.length > 0) {\n        logger.warn(\n          { repairedMigrations: repair.repairedMigrations },\n          `${label} had drifted migration history; repaired migration journal entries from existing schema state.`,\n        );\n        state = await inspectMigrations(connectionString);\n        if (state.status === \"upToDate\") return \"already applied\";\n      }\n    }\n    if (state.status === \"upToDate\") return \"already applied\";\n    if (state.status === \"needsMigrations\" && state.reason === \"no-migration-journal-non-empty-db\") {\n      logger.warn(\n        { tableCount: state.tableCount },\n        `${label} has existing tables but no migration journal. Run migrations manually to sync schema.`,\n      );\n      const apply = autoApply ? true : await promptApplyMigrations(state.pendingMigrations);\n      if (!apply) {\n        throw new Error(\n          `${label} has pending migrations (${formatPendingMigrationSummary(state.pendingMigrations)}). ` +\n            \"Refusing to start against a stale schema. Run pnpm db:migrate or set PAPERCLIP_MIGRATION_AUTO_APPLY=true.\",","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/paperclipai/paperclip/blob/a7e689b3c35347b529cb9f54c9b9a8575a3dcab6/server/src/index.ts#L190-L226","documentation":"At startup, ensureMigrations inspects the database; on 'pending-migrations' it first runs reconcilePendingMigrationHistory, which repairs drizzle migration journal entries from the actual schema state when tables already exist (journal lost, partially written, or out of sync). This warning records which journal entries were repaired, then re-inspects and continues if the DB is now up to date.","triggerScenarios":"A database whose schema already contains the 'pending' migrations but whose drizzle journal does not record them — restored snapshots with journal drift, migrations applied by another tool, or a partially damaged journal table.","commonSituations":"Backup/restore pipelines that lose or truncate the journal; DBs touched by manual DDL or external tooling; copying prod to staging; older Paperclip versions that wrote the journal differently.","solutions":["Usually benign — the repair realigned the journal with the existing schema; verify startup continued to 'already applied'.","If it recurs, diff the schema against `pnpm db:generate` output and ensure exactly one tool applies migrations.","Take a snapshot before restarts for databases with a history of drift.","For DBs with tables but no journal at all, follow the separate 'no migration journal' path (baseline or migrate manually)."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Pre-deploy: require a clean migration state against a prod snapshot.\nconst state = await inspectMigrations(connectionString);\nif (state.status === 'needsMigrations') {\n  throw new Error(`Migration state needs attention (${state.reason}); run pnpm db:migrate deliberately`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Let exactly one tool apply migrations; forbid manual DDL on managed schemas.","Include the drizzle journal table in backup/restore scope.","Alert on any startup 'repaired migration journal' warn — repeated repairs indicate an unmanaged writer.","Snapshot the database before server upgrades so repairs are reversible."],"tags":["database","migrations","drizzle","journal","drift"],"backgroundTag":"migration-history-drift","analyzedSha":"a7e689b3c35347b529cb9f54c9b9a8575a3dcab6","analyzedAt":"2026-08-18T22:49:45.177Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}