beads/internal/storage/sqlkit/dependencies.go
Test User 1fc38ba778 feat: choose your own storage backend (Postgres / MySQL / SQLite) — rebased on main
Rebase of PR #4601 (feat/choose-your-own-backend) onto current origin/main,
squashed to a single commit because the original 73-commit history built and then
removed the uowStore derisk spike incrementally, which does not replay cleanly
over the 125 commits main has since advanced (notably #4679, the ready-work
counts fix, which merged to main and is now inherited rather than re-applied).
The full pre-rebase history remains on the original branch tip for reference.

Contents (multi-backend feature + the six review follow-ups):
- bd init --backend=postgres|mysql|sqlite provisioning behind the Dolt-parity
  seam; the three SQL backends share *sqlkit.Store over pluggable dialects and
  carry a generated typed-unsupported shell for the non-core surface.
- Passwords in init DSNs are redacted before persisting to metadata.json (driver
  parser + re-parse verification), with a negative-test matrix proving no secret
  survives (pgdialect + mysql).
- The uowStore derisk spike and its report artifacts are removed; the shell
  generator now lives in internal/storage/unsupportedgen (neutral name).
- Compile-time guard `var _ storage.DoltStorage = (*Store)(nil)` on each backend
  plus a regen-idempotence test keep the generated skip lists honest.
- sqlkit exposes CountReadyWork so every SQL backend satisfies
  storage.ReadyWorkCounter and inherits the shared page-pushed ready-counts path.
- Commercial/product names scrubbed from the feature's own code for vendor
  neutrality.

Rebase reconciliations: adopted main's metadata-load hardening in the store
factories alongside the backend dispatch; combined the config interface
(GetMetadata + ReconcileVersion) and the conformance registrations (ReadyCounts +
Claim/lease); took main's versions of the shared-infra concurrency (uow
doltserver_tx) and delete-cascade (domain/issue_delete) evolutions; implemented
the interface's new UnclaimIssue on sqlkit (delegating to issueops) and added it
to the skip lists.

Verified: go build (cgo + CGO_ENABLED=0) and go vet clean; unit tests
(sqlbuild/issueops/pgdialect/mysql/unsupportedgen) and the SQLite conformance
suite green (RunAll, incl. ReadyCounts equivalence and Claim/lease on the real
sqlkit backend).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 17:44:37 +00:00

161 lines
6 KiB
Go

package sqlkit
import (
"context"
"database/sql"
"strings"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
// AddDependency adds a dependency between two issues. Routing (which
// dependency table the edge lives in, and how the target is classified) is
// resolved inside the mutation tx via IsActiveWispInTx; issueops.AddDependencyInTx
// validates, cycle-checks, and inserts. Running everything in withMutationTx
// commits the edge and its is_blocked reprojection atomically.
func (s *Store) AddDependency(ctx context.Context, dep *types.Dependency, actor string) error {
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
isCrossPrefix := types.ExtractPrefix(dep.IssueID) != types.ExtractPrefix(dep.DependsOnID)
writeTable, sourceTable := "dependencies", "issues"
if issueops.IsActiveWispInTx(ctx, tx, dep.IssueID) {
writeTable, sourceTable = "wisp_dependencies", "wisps"
}
targetTable := "issues"
kind := issueops.DepTargetIssue
switch {
case isCrossPrefix, strings.HasPrefix(dep.DependsOnID, "external:"):
kind = issueops.DepTargetExternal
default:
if issueops.IsActiveWispInTx(ctx, tx, dep.DependsOnID) {
targetTable = "wisps"
kind = issueops.DepTargetWisp
}
}
return issueops.AddDependencyInTx(ctx, tx, dep, actor, issueops.AddDependencyOpts{
SourceTable: sourceTable,
TargetTable: targetTable,
WriteTable: writeTable,
IsCrossPrefix: isCrossPrefix,
TargetKind: &kind,
})
})
}
// RemoveDependency removes a dependency between two issues. issueops handles
// wisp routing internally, so both source classes collapse to one call. actor
// is unused (kept to satisfy the storage.Storage signature).
func (s *Store) RemoveDependency(ctx context.Context, issueID, dependsOnID string, actor string) error {
return s.withMutationTx(ctx, func(tx *sql.Tx) error {
return issueops.RemoveDependencyInTx(ctx, tx, issueID, dependsOnID)
})
}
// GetDependencies retrieves issues that this issue depends on.
func (s *Store) GetDependencies(ctx context.Context, issueID string) ([]*types.Issue, error) {
var result []*types.Issue
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDependenciesInTx(ctx, tx, issueID)
return err
})
return result, err
}
// GetDependents retrieves issues that depend on this issue.
func (s *Store) GetDependents(ctx context.Context, issueID string) ([]*types.Issue, error) {
var result []*types.Issue
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDependentsInTx(ctx, tx, issueID)
return err
})
return result, err
}
// GetDependenciesWithMetadata returns dependencies with their edge metadata.
// issueops queries both dependencies and wisp_dependencies and hydrates the
// targets, so there is no store-level wisp branch or inline SQL.
func (s *Store) GetDependenciesWithMetadata(ctx context.Context, issueID string) ([]*types.IssueWithDependencyMetadata, error) {
var result []*types.IssueWithDependencyMetadata
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDependenciesWithMetadataInTx(ctx, tx, issueID)
return err
})
return result, err
}
// GetDependentsWithMetadata returns dependents with their edge metadata.
func (s *Store) GetDependentsWithMetadata(ctx context.Context, issueID string) ([]*types.IssueWithDependencyMetadata, error) {
var result []*types.IssueWithDependencyMetadata
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDependentsWithMetadataInTx(ctx, tx, issueID)
return err
})
return result, err
}
// GetDependencyTree returns a dependency tree for visualization.
func (s *Store) GetDependencyTree(ctx context.Context, issueID string, maxDepth int, showAllPaths bool, reverse bool) ([]*types.TreeNode, error) {
var result []*types.TreeNode
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDependencyTreeInTx(ctx, tx, issueID, maxDepth, showAllPaths, reverse)
return err
})
return result, err
}
// CountDependents returns the number of issues that depend on issueID.
// Counts both dependency tables so the total matches GetDependentsWithMetadata:
// a dependent may be a permanent issue (edge in `dependencies`) or a wisp (edge
// in `wisp_dependencies`, routed there by WispTableRouting on the source).
//
// Both tables' targets are resolved via issueops.DepTargetExpr (the split
// physical columns) rather than the STORED generated depends_on_id, which a
// count(*) can fail to resolve under the pure-Go GMS analyzer.
func (s *Store) CountDependents(ctx context.Context, issueID string) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var perm, wisp int64
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM dependencies WHERE `+issueops.DepTargetExpr+` = ?`, issueID).Scan(&perm); err != nil {
return err
}
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM wisp_dependencies WHERE `+issueops.DepTargetExpr+` = ?`, issueID).Scan(&wisp); err != nil {
return err
}
n = perm + wisp
return nil
})
return n, err
}
// CountDependencies returns the number of issues that issueID depends on.
// Counts both dependency tables so the total matches GetDependenciesWithMetadata:
// a wisp's outgoing edges live in `wisp_dependencies`, a permanent issue's in
// `dependencies`. Counted as two separate queries summed in Go (see
// CountDependents for why a single combined query is avoided).
func (s *Store) CountDependencies(ctx context.Context, issueID string) (int64, error) {
var n int64
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var perm, wisp int64
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM dependencies WHERE issue_id = ?`, issueID).Scan(&perm); err != nil {
return err
}
if err := tx.QueryRowContext(ctx,
`SELECT count(*) FROM wisp_dependencies WHERE issue_id = ?`, issueID).Scan(&wisp); err != nil {
return err
}
n = perm + wisp
return nil
})
return n, err
}