{"record":{"id":"bd2541eb5922d1dc","repo":"gastownhall/beads","slug":"failed-to-query-orphaned-dependencies-w","errorCode":null,"errorMessage":"failed to query orphaned dependencies: %w","messagePattern":"failed to query orphaned dependencies: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/doctor/fix/validation.go","lineNumber":53,"sourceCode":"\t}\n\tdefer db.Close()\n\n\tif skip, err := guardFixTarget(\"Orphaned dependencies fix\", db, beadsDir, cfg); skip {\n\t\treturn err\n\t}\n\n\t// Find orphaned dependencies (exclude external: cross-rig tracking refs, #1593)\n\t//nolint:gosec // G202: fixDependencyUnionSQL returns a fixed internal SELECT fragment.\n\tquery := `\n\t\tSELECT d.dep_table, d.issue_id, d.depends_on_id\n\t\tFROM (` + fixDependencyUnionSQL() + `) d\n\t\tWHERE NOT EXISTS (SELECT 1 FROM issues i WHERE i.id = d.depends_on_id)\n\t\t  AND NOT EXISTS (SELECT 1 FROM wisps w WHERE w.id = d.depends_on_id)\n\t\t  AND d.depends_on_id NOT LIKE 'external:%'\n\t`\n\trows, err := db.Query(query)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to query orphaned dependencies: %w\", err)\n\t}\n\tdefer rows.Close()\n\n\ttype orphan struct {\n\t\tdepTable    string\n\t\tissueID     string\n\t\tdependsOnID string\n\t}\n\tvar orphans []orphan\n\n\tfor rows.Next() {\n\t\tvar o orphan\n\t\tif err := rows.Scan(&o.depTable, &o.issueID, &o.dependsOnID); err == nil {\n\t\t\torphans = append(orphans, o)\n\t\t}\n\t}\n\tif err := rows.Err(); err != nil {\n\t\treturn fmt.Errorf(\"row iteration error: %w\", err)","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/doctor/fix/validation.go#L35-L71","documentation":"OrphanedDependencies in cmd/bd/doctor/fix/validation.go runs a SELECT that unions the dependencies and wisp_dependencies tables and keeps rows whose depends_on_id has no matching issue or wisp. When db.Query itself fails (connection drop, missing table, SQL syntax/permission problem), the function wraps the driver error with this message and aborts the fix. It is a pre-fix data-gathering failure, not a data corruption report.","triggerScenarios":"db.Query returns an error on the orphan-detection SELECT at validation.go:51 — e.g. the Dolt/MySQL connection dropped between openDoltDB and the query, a dependency table is missing or locked, or the query was rejected (syntax/permission/timeout).","commonSituations":"Dolt server restarted or killed mid-run; database files missing/corrupt so `dependencies` or `wisp_dependencies` tables don't exist; connection to a remote Dolt server times out; wrong working beadsDir so the query hits a partial/empty database; SQL-layer permission errors after a Dolt upgrade.","solutions":["Check that the Dolt server (or embedded database) backing the workspace is up and reachable; retry `bd doctor --fix`.","Verify the tables exist: run `bd dolt sql -q \"SHOW TABLES\"` and confirm `dependencies` and `wisp_dependencies` are present; restore or recreate the database if not.","Inspect the wrapped driver error (%w) for the root cause (timeout vs. missing table vs. permission) and address that specific condition.","If corruption is suspected, back up .beads/ and re-init or restore from a Dolt branch/backup before running fixes again."],"exampleFix":"// before: fix aborts on transient connection drop\nrows, err := db.Query(query)\nif err != nil {\n\treturn fmt.Errorf(\"failed to query orphaned dependencies: %w\", err)\n}\n// after: ensure server running / tables present, then retry\n// dolt server &  # or: bd dolt sql -q \"SHOW TABLES\" to confirm schema\nrows, err := db.Query(query)\nif err != nil {\n\treturn fmt.Errorf(\"failed to query orphaned dependencies: %w\", err) // now succeeds\n}","handlingStrategy":"try-catch","validationCode":"// before invoking the fix, confirm the database is queryable\nrows, err := db.Query(\"SELECT 1 FROM dependencies LIMIT 1\")\nif err != nil {\n\treturn fmt.Fprintf(os.Stderr, \"database not ready: %v\\n\", err)\n}\nrows.Close()","typeGuard":"var target interface{ Error() string } = err\nvar sqlErr *driver.Error\nif errors.As(target, &sqlErr) {\n\t// driver-level failure (connection/table) rather than fix-specific problem\n}","tryCatchPattern":"if err := fix.OrphanedDependencies(path, verbose); err != nil {\n\tif errors.Is(err, driver.ErrBadConn) || strings.Contains(err.Error(), \"connection refused\") {\n\t\t// restart dolt server, then retry once\n\t} else {\n\t\tlog.Fatalf(\"orphan fix aborted: %v\", err)\n\t}\n}","preventionTips":["Run `bd doctor` health checks (without --fix) first to confirm the database is reachable.","Keep the Dolt server process monitored/restarted under a supervisor so it is alive during fixes.","Pin compatible Dolt server and client-driver versions to avoid schema/driver surprises.","Back up .beads/ before running any --fix operation."],"tags":["go","database","dolt","sql","doctor"],"backgroundTag":"sql-query-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}