beads/internal/storage/sqlbuild/sort.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

171 lines
4.9 KiB
Go

package sqlbuild
import (
"fmt"
"strings"
"time"
"github.com/steveyegge/beads/internal/types"
)
// SortDef maps a user-facing sort key to its column and default direction.
type SortDef struct {
Column string
DefaultDir string
}
// SortDefs is the canonical sort-key table for issue list/search ordering.
var SortDefs = map[string]SortDef{
"": {"priority", "ASC"},
"priority": {"priority", "ASC"},
"created": {"created_at", "DESC"},
"updated": {"updated_at", "DESC"},
"closed": {"closed_at", "DESC"},
"status": {"status", "ASC"},
"type": {"issue_type", "ASC"},
"assignee": {"assignee", "ASC"},
"title": {"title", "ASC"},
}
// UnionSortColumnsSQL projects every sortable column under a stable sort_*
// alias so a UNION ALL outer query can ORDER BY any sort key.
const UnionSortColumnsSQL = `priority AS sort_priority,
created_at AS sort_created,
updated_at AS sort_updated,
closed_at AS sort_closed,
status AS sort_status,
issue_type AS sort_type,
assignee AS sort_assignee,
LOWER(title) AS sort_title`
// IsGoSideSort reports sort keys that are applied in Go after the query
// instead of in SQL.
func IsGoSideSort(sortBy string) bool {
return sortBy == "id"
}
func flipDir(dir string) string {
if dir == "ASC" {
return "DESC"
}
return "ASC"
}
// OrderByForColumns renders the ORDER BY clause for a sort key, mapping sort
// keys to column expressions via col. Used directly by UNION consumers whose
// columns are aliased; per-table callers should use OrderBy.
func OrderByForColumns(sortBy string, sortDesc bool, col func(sortKey string) string) string {
if IsGoSideSort(sortBy) {
return ""
}
def, ok := SortDefs[sortBy]
if !ok {
def = SortDefs[""]
sortBy = ""
}
dir := def.DefaultDir
if sortDesc {
dir = flipDir(dir)
}
if sortBy == "" || sortBy == "priority" {
return fmt.Sprintf("ORDER BY %s %s, %s DESC, %s ASC", col("priority"), dir, col("created"), col("id"))
}
// A nullable sort column (closed_at, assignee) must order NULLs the MySQL way — NULL
// treated as lowest, so first on ASC and last on DESC — on every backend. MySQL/Dolt/
// SQLite do this by default; Postgres defaults the opposite way (NULLS FIRST on DESC),
// so lead with an explicit (col IS NULL) key for those two. NOT NULL columns keep the
// plain clause (byte-identical to before).
if sortBy == "closed" || sortBy == "assignee" {
return fmt.Sprintf("ORDER BY (%s IS NULL) %s, %s %s, %s ASC", col(sortBy), flipDir(dir), col(sortBy), dir, col("id"))
}
return fmt.Sprintf("ORDER BY %s %s, %s ASC", col(sortBy), dir, col("id"))
}
// OrderBy renders the ORDER BY clause against real table columns, optionally
// qualified ("i" -> "i.priority"). Ties always break by id ASC; the default
// priority sort additionally breaks by created_at DESC.
func OrderBy(sortBy string, sortDesc bool, table string) string {
qual := ""
if table != "" {
qual = table + "."
}
return OrderByForColumns(sortBy, sortDesc, func(k string) string {
switch k {
case "id":
return qual + "id"
case "title":
return "LOWER(" + qual + "title)"
}
return qual + SortDefs[k].Column
})
}
// Less is the Go-side mirror of OrderBy for merge sorts over rows fetched
// from separate queries (issues + wisps). It must order exactly the way the
// SQL does, including MySQL NULL-first semantics for nullable columns;
// otherwise a post-merge limit cut keeps a different row set than SQL
// selected.
func Less(a, b *types.Issue, sortBy string, sortDesc bool) bool {
if sortBy == "id" {
return a.ID < b.ID
}
def, ok := SortDefs[sortBy]
if !ok {
def = SortDefs[""]
sortBy = ""
}
descending := def.DefaultDir == "DESC"
if sortDesc {
descending = !descending
}
if c := sortKeyCompare(a, b, sortBy); c != 0 {
if descending {
return c > 0
}
return c < 0
}
if (sortBy == "" || sortBy == "priority") && !a.CreatedAt.Equal(b.CreatedAt) {
return a.CreatedAt.After(b.CreatedAt)
}
return a.ID < b.ID
}
// sortKeyCompare three-way compares the primary sort column in ascending
// order, with MySQL NULL-first semantics for nullable columns.
func sortKeyCompare(a, b *types.Issue, sortBy string) int {
switch sortBy {
case "created":
return compareTimesAsc(a.CreatedAt, b.CreatedAt)
case "updated":
return compareTimesAsc(a.UpdatedAt, b.UpdatedAt)
case "closed":
switch {
case a.ClosedAt == nil && b.ClosedAt == nil:
return 0
case a.ClosedAt == nil:
return -1
case b.ClosedAt == nil:
return 1
}
return compareTimesAsc(*a.ClosedAt, *b.ClosedAt)
case "status":
return strings.Compare(string(a.Status), string(b.Status))
case "type":
return strings.Compare(string(a.IssueType), string(b.IssueType))
case "assignee":
return strings.Compare(a.Assignee, b.Assignee)
case "title":
return strings.Compare(strings.ToLower(a.Title), strings.ToLower(b.Title))
}
return a.Priority - b.Priority
}
func compareTimesAsc(a, b time.Time) int {
switch {
case a.Before(b):
return -1
case a.After(b):
return 1
}
return 0
}