beads/internal/storage/sqlkit/strategy.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

68 lines
3 KiB
Go

// Package sqlkit is the single, shared SQL-family implementation of bd's storage
// semantics. Every SQL backend (Dolt, Postgres, SQLite, MySQL, …) is a strategy
// bundle over this one implementation:
//
// - Dialect — the SQL flavor (a translating driver or native emit)
// - ReadinessStrategy — how the is_blocked projection is kept current
// - ClaimStrategy — how a ready issue is atomically claimed
//
// A concrete backend embeds *Store, inherits every method, and overrides only
// the handful it specializes (a "method override") or adds capability-interface
// methods for operations that don't exist in the core contract (e.g. Dolt
// history). The query/graph/ordering/id-mint/cycle semantics live here once, in
// internal/storage/issueops, and are never re-derived per backend.
package sqlkit
import (
"context"
"database/sql"
"github.com/steveyegge/beads/internal/storage/issueops"
"github.com/steveyegge/beads/internal/types"
)
// Dialect opens the backend's *sql.DB and names it. The shared layer emits
// canonical (MySQL-dialect) SQL; a Dialect whose driver translates that text to
// the backend's flavor is how a non-MySQL SQL engine joins the family without
// changing the shared layer (see internal/storage/pgdialect for Postgres).
type Dialect interface {
Name() string
Open(ctx context.Context) (*sql.DB, error)
}
// ReadinessStrategy keeps the denormalized is_blocked column consistent after a
// mutation. It runs inside the same write transaction, so the mutation and its
// reprojection commit atomically.
type ReadinessStrategy interface {
AfterWrite(ctx context.Context, tx issueops.DBTX) error
}
// ClaimStrategy selects and claims one ready issue. The wedge uses the shared
// read-then-update path (default); a backend may override with, e.g.,
// SELECT … FOR UPDATE SKIP LOCKED for lock-free multi-writer claims. Wired in
// the claim slice.
type ClaimStrategy interface {
ClaimReady(ctx context.Context, tx issueops.DBTX, filter types.WorkFilter, actor string) (*types.Issue, error)
}
// NoopReadiness performs no reprojection. It is the default: every shared
// issueops mutation helper already maintains the is_blocked column targeted and
// in-transaction (exactly like the Dolt reference, which wires no readiness
// strategy at all), so a per-write full-graph recompute is pure redundant cost.
// A backend opts IN to extra reprojection by supplying SyncReadiness explicitly.
type NoopReadiness struct{}
func (NoopReadiness) AfterWrite(_ context.Context, _ issueops.DBTX) error {
return nil
}
// SyncReadiness recomputes is_blocked synchronously, in-transaction, on every
// mutation via the full-graph fixpoint. Because the issueops helpers already
// keep is_blocked current, this is only useful as an explicit opt-in repair
// projection; it is no longer the nil-default.
type SyncReadiness struct{}
func (SyncReadiness) AfterWrite(ctx context.Context, tx issueops.DBTX) error {
_, err := issueops.RecomputeAllIsBlockedInTx(ctx, tx)
return err
}