beads/internal/storage/dolt/embedded_uow.go
beads/crew/emma 2137693dad revert: restore embedded Dolt mode (was only supposed to be removed from Gas Town)
Revert commits e5330641 and 0df41523 which removed embedded Dolt mode
from the beads codebase. The embedded Dolt removal was only intended for
Gas Town, not for Beads.

Restores:
- dolthub/driver dependency and embedded connection path
- Advisory flock (AccessLock) for concurrent process protection
- Embedded connector lifecycle management in DoltStore
- All CGO build tags across ~45 files
- Vendored go-icu-regex library
- nocgo stub files for non-CGO builds
- Bootstrap and embedded_uow helpers
- factory_dolt_test.go and dolt_singleprocess_test.go

Merge conflict resolutions:
- store.go: TCP fail-fast check wrapped inside if cfg.ServerMode
- store.go: backtick-quoting for embedded CREATE DATABASE (prefix-based names)
- perf_dolt.go: port references updated from 3306 to 3307
- ci.yml: restored continue-on-error for Nix (dolthub/driver requires Go 1.25.6+)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Executed-By: beads/crew/emma
Rig: beads
Role: crew
2026-02-07 21:21:24 -08:00

90 lines
2.1 KiB
Go

//go:build cgo
package dolt
import (
"context"
"database/sql"
"errors"
embedded "github.com/dolthub/driver"
"github.com/steveyegge/beads/internal/storage/doltutil"
)
func ignoreContextCanceled(err error) error {
if err == nil {
return nil
}
// Dolt engine close paths may surface context.Canceled from background goroutines / shutdown plumbing.
// For unit-of-work helpers, we treat that as non-fatal cleanup noise.
if errors.Is(err, context.Canceled) {
return nil
}
return err
}
// withEmbeddedDolt executes exactly one unit of work using a single embedded Dolt connector.
//
// Lifecycle:
// 1) ParseDSN
// 2) configure (e.g. enable retries by setting cfg.BackOff)
// 3) NewConnector
// 4) sql.OpenDB(connector)
// 5) PingContext(ctx) to force open (and retries)
// 6) fn(ctx, db)
// 7) db.Close()
// 8) connector.Close() to release filesystem locks
//
// IMPORTANT: This helper does not wrap or modify ctx (no timeouts). The embedded driver derives
// a session context from the Connect(ctx) context and reuses it across statements.
func withEmbeddedDolt(
ctx context.Context,
dsn string,
configure func(cfg *embedded.Config),
fn func(ctx context.Context, db *sql.DB) error,
) (err error) {
if ctx == nil {
ctx = context.Background()
}
if fn == nil {
return errors.New("withEmbeddedDolt: fn is required")
}
cfg, err := embedded.ParseDSN(dsn)
if err != nil {
return err
}
if configure != nil {
configure(&cfg)
}
connector, err := embedded.NewConnector(cfg)
if err != nil {
return err
}
db := sql.OpenDB(connector)
defer func() {
// Close DB first (stops pool activity), then close the connector to release engine locks.
// Use CloseWithTimeout to prevent indefinite hangs (same issue as DoltStore.Close).
cerr := errors.Join(
ignoreContextCanceled(doltutil.CloseWithTimeout("db", db.Close)),
ignoreContextCanceled(doltutil.CloseWithTimeout("connector", connector.Close)),
)
if err == nil {
err = cerr
} else {
err = errors.Join(err, cerr)
}
}()
// Force open (and retries if configured) before entering the unit of work.
if err := db.PingContext(ctx); err != nil {
return err
}
return fn(ctx, db)
}