{"record":{"id":"814a4f5027bf9997","repo":"gastownhall/beads","slug":"counting-nonlocal-frozen-rows-w","errorCode":null,"errorMessage":"counting nonlocal frozen rows: %w","messagePattern":"counting nonlocal frozen rows: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/schema/migration_repairs.go","lineNumber":99,"sourceCode":"// if the edit staged nothing.\nfunc commitNonlocalRepair(ctx context.Context, db DBConn, message string) error {\n\tif err := DrainCall(ctx, db, \"CALL DOLT_ADD(?)\", nonlocalTablesName); err != nil {\n\t\treturn fmt.Errorf(\"staging %s: %w\", nonlocalTablesName, err)\n\t}\n\treturn DrainCall(ctx, db, \"CALL DOLT_COMMIT('-m', ?, '--skip-empty')\", message)\n}\n\n// anyNonlocalFrozenRowPresent reports whether any of 0040's four\n// dolt_nonlocal_tables rows currently exists (in the working set), the signal\n// the version-40/41 repairs use to decide whether a heal is needed. Guarding on\n// it keeps both repairs a strict no-op on the common (non-partial) path, so a\n// fresh init reaches 0040/0041 having done no repair work at all — the repairs\n// only ever touch a database that actually took the partial-apply brick.\nfunc anyNonlocalFrozenRowPresent(ctx context.Context, db DBConn) (bool, error) {\n\tvar count int\n\tif err := db.QueryRowContext(ctx,\n\t\t\"SELECT COUNT(*) FROM dolt_nonlocal_tables WHERE table_name IN \"+nonlocalFrozenRowsInList).Scan(&count); err != nil {\n\t\treturn false, fmt.Errorf(\"counting nonlocal frozen rows: %w\", err)\n\t}\n\treturn count > 0, nil\n}\n\n// repairPartial0040NonlocalInsert heals a partially-applied migration 0040 so\n// its shipped body can replay. 0040 bare-INSERTs four dolt_nonlocal_tables rows,\n// each paired with its own CALL DOLT_COMMIT. Over a shared sql-server a transient\n// (\"busy buffer\" -> \"bad connection\") can leave some rows committed while the\n// schema_migrations version row never records, so the init retry loop re-runs\n// 0040 from the top and the bare INSERT dies on \"duplicate primary key given:\n// [wisps]\", bricking the database. 0040 is a shipped, content-hashed migration\n// and cannot be edited (see the file header), so instead clear any of those four\n// rows before the replay and commit the removal, leaving 0040's INSERT+COMMIT\n// pairs a clean, real diff. No-op when 0040 never partially applied.\nfunc repairPartial0040NonlocalInsert(ctx context.Context, db DBConn) error {\n\tpresent, err := anyNonlocalFrozenRowPresent(ctx, db)\n\tif err != nil {\n\t\treturn err","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/schema/migration_repairs.go#L81-L117","documentation":"anyNonlocalFrozenRowPresent counts the four frozen dolt_nonlocal_tables rows (wisps, wisp_*, repo_mtimes, local_metadata) to decide whether a 0040/0041 heal is needed. This error wraps a SQL failure of that COUNT query — typically the table missing entirely or a connection error. Without the count the repair cannot tell if the database took the partial-apply brick.","triggerScenarios":"SELECT COUNT(*) FROM dolt_nonlocal_tables fails because the table does not exist (fresh/pre-0040 database or drifted clone), or the connection to the Dolt server drops mid-query during bd init.","commonSituations":"Upgrading a very old or drifted clone whose schema predates 0040; shared sql-server outage during init; manually deleted schema tables.","solutions":["Retry bd init after confirming the Dolt server is reachable","If the wrapped error is \"table not found: dolt_nonlocal_tables\", the database predates 0040 and repair should be a no-op — verify the migration cursor","Run bd doctor to assess database health","If the table should exist but is missing, restore from a clone/backup"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// confirm dolt_nonlocal_tables exists before repair logic runs\nvar n int\nerr := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES\n  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'dolt_nonlocal_tables'`).Scan(&n)\n// n == 0 means pre-0040 database: repair is legitimately a no-op","typeGuard":null,"tryCatchPattern":"present, err := anyNonlocalFrozenRowPresent(ctx, db)\nif err != nil {\n    if isTableNotFound(err) {\n        return nil // pre-0040 database: nothing to repair\n    }\n    return err\n}","preventionTips":["Confirm the Dolt sql-server is reachable before starting migrations","Treat \"table not found: dolt_nonlocal_tables\" on pre-0040 databases as an expected no-op, not a fault","Use bd doctor to check database health before upgrading","Retry on transient connection errors before assuming corruption"],"tags":["dolt","migration","sql","storage"],"backgroundTag":"table-not-found","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}