{"record":{"id":"4d56993151d3f661","repo":"gastownhall/beads","slug":"get-current-branch-w","errorCode":null,"errorMessage":"get current branch: %w","messagePattern":"get current branch: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/versioncontrolops/branches.go","lineNumber":31,"sourceCode":"\t}\n\tdefer rows.Close()\n\n\tvar branches []string\n\tfor rows.Next() {\n\t\tvar name string\n\t\tif err := rows.Scan(&name); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"scan branch: %w\", err)\n\t\t}\n\t\tbranches = append(branches, name)\n\t}\n\treturn branches, rows.Err()\n}\n\n// CurrentBranch returns the name of the active branch.\nfunc CurrentBranch(ctx context.Context, db DBConn) (string, error) {\n\tvar branch string\n\tif err := db.QueryRowContext(ctx, \"SELECT active_branch()\").Scan(&branch); err != nil {\n\t\treturn \"\", fmt.Errorf(\"get current branch: %w\", err)\n\t}\n\treturn branch, nil\n}\n\n// CreateBranch creates a new Dolt branch from the current HEAD.\nfunc CreateBranch(ctx context.Context, db DBConn, name string) error {\n\tif _, err := db.ExecContext(ctx, \"CALL DOLT_BRANCH(?)\", name); err != nil {\n\t\treturn fmt.Errorf(\"create branch %s: %w\", name, err)\n\t}\n\treturn nil\n}\n\n// DeleteBranch force-deletes a Dolt branch.\nfunc DeleteBranch(ctx context.Context, db DBConn, name string) error {\n\tif _, err := db.ExecContext(ctx, \"CALL DOLT_BRANCH('-D', ?)\", name); err != nil {\n\t\treturn fmt.Errorf(\"delete branch %s: %w\", name, err)\n\t}\n\treturn nil","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/versioncontrolops/branches.go#L13-L49","documentation":"CurrentBranch queries Dolt's active_branch() SQL function and wraps any driver error with 'get current branch: %w'. This wrapper marks failures to read the session's active branch name — typically a connection problem, a non-Dolt database, or a database with no branch context. It preserves the underlying driver error for inspection via errors.Is/As.","triggerScenarios":"Calling versioncontrolops.CurrentBranch with a DBConn whose underlying query 'SELECT active_branch()' fails: closed/lost connection, server not a Dolt server, or Dolt version lacking active_branch().","commonSituations":"Dolt server restarted or network dropped mid-session; pointing at a plain MySQL database instead of Dolt; running an old Dolt version without active_branch(); connection pool returned a stale connection.","solutions":["Verify the database is actually a Dolt database (SELECT active_branch() works on Dolt only) and the Dolt version is recent enough","Check the embedded driver error (errors.Is/As) for connection/refused causes and reconnect with a fresh DBConn","Validate connectivity with a trivial ping query before calling CurrentBranch"],"exampleFix":"// before\nbranch, err := versioncontrolops.CurrentBranch(ctx, staleDB)\n// after\nif err := db.PingContext(ctx); err != nil {\n    db, err = openDolt(ctx) // reopen after detecting dead connection\n}\nbranch, err := versioncontrolops.CurrentBranch(ctx, db)","handlingStrategy":"try-catch","validationCode":"var one string\nif err := db.QueryRowContext(ctx, \"SELECT 1\").Scan(&one); err != nil {\n    // connection is not usable; skip branch query\n}","typeGuard":null,"tryCatchPattern":"branch, err := versioncontrolops.CurrentBranch(ctx, db)\nif err != nil {\n    var driverErr error\n    if errors.As(err, &driverErr) {\n        log.Printf(\"current branch query failed: %v\", driverErr) // decide reconnect vs fatal\n    }\n    return fmt.Errorf(\"branch detection unavailable: %w\", err)\n}","preventionTips":["Ping the connection before session-scoped Dolt queries","Confirm the backend is Dolt (active_branch() exists) not plain MySQL","Keep Dolt server/engine up to date","Reuse a healthy dedicated connection for session-scoped calls"],"tags":["dolt","sql","database","branch"],"backgroundTag":"dolt-active-branch-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}