{"record":{"id":"4e1e03c7f5912245","repo":"gastownhall/beads","slug":"find-initial-commit-w","errorCode":null,"errorMessage":"find initial commit: %w","messagePattern":"find initial commit: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/versioncontrolops/flatten.go","lineNumber":30,"sourceCode":"//  3. Soft-reset to the initial (oldest) commit, collapsing all history\n//  4. Stage all + commit as a single snapshot\n//  5. Checkout main\n//  6. Hard-reset main to the flattened branch\n//  7. Delete temp branch\n//\n// Callers should run PruneRemoteRefs and then DoltGC afterward to reclaim disk\n// space from orphaned history — remote-tracking refs still anchor the\n// pre-flatten chain, and GC alone reclaims nothing while they exist (bd-agctw).\n//\n// conn must be a single database connection (not a pooled *sql.DB) since the\n// stored procedures rely on session-scoped state (current branch, working set).\nfunc Flatten(ctx context.Context, conn DBConn) error {\n\t// Find the initial commit hash (oldest ancestor).\n\tvar initialHash string\n\tif err := conn.QueryRowContext(ctx,\n\t\t\"SELECT commit_hash FROM dolt_log ORDER BY date ASC LIMIT 1\",\n\t).Scan(&initialHash); err != nil {\n\t\treturn fmt.Errorf(\"find initial commit: %w\", err)\n\t}\n\n\t// Count commits to check if flatten is needed.\n\tvar commitCount int\n\tif err := conn.QueryRowContext(ctx,\n\t\t\"SELECT COUNT(*) FROM dolt_log\",\n\t).Scan(&commitCount); err != nil {\n\t\treturn fmt.Errorf(\"count commits: %w\", err)\n\t}\n\tif commitCount <= 1 {\n\t\treturn nil // already flat\n\t}\n\n\texecSQL := func(name, query string, args ...interface{}) error {\n\t\tif _, err := conn.ExecContext(ctx, query, args...); err != nil {\n\t\t\treturn fmt.Errorf(\"flatten step %q: %w\", name, err)\n\t\t}\n\t\treturn nil","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/versioncontrolops/flatten.go#L12-L48","documentation":"Flatten squashes all Dolt commit history into a single commit; it starts by selecting the oldest commit hash from dolt_log. If that query/Scan fails — most commonly sql.ErrNoRows because the database has no commit history, or a connection/availability failure — the error is wrapped as \"find initial commit: %w\".","triggerScenarios":"Calling Flatten(ctx, conn) on a freshly initialized database with zero commits in dolt_log; a broken/pooled connection (Flatten requires a single session-scoped connection); context cancellation; dolt_log unreadable.","commonSituations":"Running flatten before the first commit was ever made in a new beads/Dolt database; passing a pooled *sql.DB instead of a single connection so Dolt stored-procedure session state breaks; server outage.","solutions":["Ensure the database has at least one commit (make an initial DOLT_COMMIT) before flattening.","Check the wrapped error: sql.ErrNoRows means empty history; others indicate connection problems.","Pass a single dedicated DB connection, not a pooled *sql.DB — Flatten relies on session-scoped state.","Run FlattenDryRun first to get the commit count and initial hash without side effects."],"exampleFix":"// before\nversioncontrolops.Flatten(ctx, sqlDB) // pooled DB, possibly empty history\n// after\ncount, _, err := versioncontrolops.FlattenDryRun(ctx, conn)\nif err != nil { return err }\nif count == 0 { return fmt.Errorf(\"nothing to flatten: no commits\") }\nversioncontrolops.Flatten(ctx, singleConn)","handlingStrategy":"validation","validationCode":"count, _, err := versioncontrolops.FlattenDryRun(ctx, conn)\nif err != nil { return err }\nif count == 0 {\n    return fmt.Errorf(\"cannot flatten: database has no commits\")\n}","typeGuard":null,"tryCatchPattern":"err := versioncontrolops.Flatten(ctx, conn)\nif err != nil && strings.Contains(err.Error(), \"find initial commit\") {\n    if errors.Is(err, sql.ErrNoRows) || strings.Contains(err.Error(), \"no rows\") {\n        return fmt.Errorf(\"empty history: make an initial commit first\")\n    }\n    return err\n}","preventionTips":["Run FlattenDryRun first to verify history is non-empty.","Pass a single dedicated connection, never a pooled *sql.DB.","Only flatten databases that have completed at least one commit.","Keep a backup/remote push before destructive flatten operations."],"tags":["dolt","sql","history","flatten"],"backgroundTag":"empty-commit-history","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}