/** * Post-migration self-heal (FEAT-495 item 1): schema_version recording a * migration as applied does not prove its DDL actually landed on THIS * database. SQLite has no `ALTER TABLE ... ADD COLUMN IF NOT EXISTS`, so a * partially-applied migration (e.g. a crash between two ALTER TABLE * statements in the same file, before the transaction wrapping migrate()'s * loop commits) leaves a column permanently missing with no automatic * recovery — otherwise unrecoverable without an operator hand-editing the * schema. * * Re-derives every `ALTER TABLE ADD COLUMN ...` statement * ever shipped by scanning ALL migration files on disk (not just ones * applied this boot — the gap could have been introduced by a migration * from months ago), checks each target column's real presence via * `pragma_table_info`, and re-issues the exact original ALTER TABLE text for * any that's missing. A table the ALTER TABLE targets no longer existing at * all (dropped by a later migration — migrations/040-drop-dead-tables.sql) * is skipped, not an error: its columns were deliberately removed along with * it, there is nothing to heal. * * Called unconditionally from migrate() (this repo's one boot-time DDL * entrypoint every caller already goes through), NOT gated behind doctor — * doctor never runs unattended (see CLAUDE.md's "Known open gaps"), so a * self-heal that only doctor could trigger would never fire on a real * service boot. */ import type { DB } from './db.js'; export interface AddColumnStatement { migrationFile: string; table: string; column: string; /** Verbatim `ALTER TABLE ... ;` text from the migration file, ready to re-execute as-is. */ statement: string; } /** Extract every ADD COLUMN statement from one migration file's raw SQL text. */ export declare function extractAddColumnStatements(sql: string, migrationFile: string): AddColumnStatement[]; export interface SelfHealAction { migrationFile: string; table: string; column: string; } /** * Run the self-heal pass: for every ADD COLUMN statement across every * migration file on disk, re-apply it if (and only if) the target table * exists and the target column is missing. Returns the list of actions * actually taken (empty when the schema is fully healthy, the common case). */ export declare function selfHealMissingColumns(db: DB): SelfHealAction[]; /** Append one row per healed column, so doctor can show what was healed and when — even after the gap is already closed. */ export declare function recordSelfHealActions(db: DB, actions: SelfHealAction[], healedAt?: number): void; export interface SelfHealLogRow { migrationFile: string; table: string; column: string; healedAt: number; } /** Most recent N healed-column rows, newest first. Empty when self-heal has never fired. */ export declare function recentSelfHealActions(db: DB, limit?: number): SelfHealLogRow[];