beads/internal/storage/factory/factory_dolt.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

133 lines
4.3 KiB
Go

//go:build cgo
package factory
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/steveyegge/beads/internal/config"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dolt"
)
func init() {
RegisterBackend(configfile.BackendDolt, func(ctx context.Context, path string, opts Options) (storage.Storage, error) {
// Only bootstrap in embedded mode - server mode has database on server
if !opts.ServerMode {
if err := bootstrapEmbeddedDolt(ctx, path, opts); err != nil {
return nil, err
}
}
store, err := dolt.New(ctx, &dolt.Config{
Path: path,
Database: opts.Database,
ReadOnly: opts.ReadOnly,
OpenTimeout: opts.OpenTimeout,
ServerMode: opts.ServerMode,
ServerHost: opts.ServerHost,
ServerPort: opts.ServerPort,
ServerUser: opts.ServerUser,
})
if err != nil {
// If server mode failed with a connection error, fall back to embedded mode.
// This preserves the 2-tier architecture: server when available, embedded when not.
if opts.ServerMode && isServerConnectionError(err) {
fmt.Fprintf(os.Stderr, "Warning: Dolt server at %s:%d is not reachable, falling back to embedded mode\n",
opts.ServerHost, opts.ServerPort)
// Run bootstrap for embedded mode (JSONL import if needed)
if berr := bootstrapEmbeddedDolt(ctx, path, opts); berr != nil {
return nil, berr
}
return dolt.New(ctx, &dolt.Config{
Path: path,
Database: opts.Database,
ReadOnly: opts.ReadOnly,
OpenTimeout: opts.OpenTimeout,
ServerMode: false, // Fall back to embedded
})
}
return nil, err
}
return store, nil
})
}
// bootstrapEmbeddedDolt checks if a JSONL-to-Dolt bootstrap is needed and runs it if so.
func bootstrapEmbeddedDolt(ctx context.Context, path string, opts Options) error {
// Path is the dolt subdirectory, parent is .beads directory
beadsDir := filepath.Dir(path)
// In dolt-native mode, JSONL is export-only backup — never auto-import.
// If the dolt DB doesn't exist in this mode, that's an error, not a bootstrap opportunity.
// This prevents split-brain: without this guard, a wrong path (from B1) would silently
// create a new DB from stale JSONL, diverging from the real dolt-native data.
if config.GetSyncMode() == config.SyncModeDoltNative {
if !hasDoltSubdir(path) {
return fmt.Errorf("dolt database not found at %s (JSONL auto-import is disabled in dolt-native sync mode; run 'bd init --backend=dolt' to create a new database)", path)
}
return nil // Dolt exists, no bootstrap needed
}
bootstrapped, result, err := dolt.Bootstrap(ctx, dolt.BootstrapConfig{
BeadsDir: beadsDir,
DoltPath: path,
LockTimeout: opts.LockTimeout,
})
if err != nil {
return fmt.Errorf("bootstrap failed: %w", err)
}
if bootstrapped && result != nil {
fmt.Fprintf(os.Stderr, "Bootstrapping Dolt from JSONL...\n")
if len(result.ParseErrors) > 0 {
fmt.Fprintf(os.Stderr, " Skipped %d malformed lines (see above for details)\n", len(result.ParseErrors))
}
fmt.Fprintf(os.Stderr, " Imported %d issues", result.IssuesImported)
if result.IssuesSkipped > 0 {
fmt.Fprintf(os.Stderr, ", skipped %d duplicates", result.IssuesSkipped)
}
fmt.Fprintf(os.Stderr, "\n Dolt database ready\n")
}
return nil
}
// hasDoltSubdir checks if the given path contains any subdirectory with a .dolt directory inside.
func hasDoltSubdir(basePath string) bool {
entries, err := os.ReadDir(basePath)
if err != nil {
return false
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
doltDir := filepath.Join(basePath, entry.Name(), ".dolt")
if info, err := os.Stat(doltDir); err == nil && info.IsDir() {
return true
}
}
return false
}
// isServerConnectionError returns true if the error indicates the Dolt server
// is unreachable (connection refused, timeout, DNS failure, etc.).
func isServerConnectionError(err error) bool {
if err == nil {
return false
}
errLower := strings.ToLower(err.Error())
return strings.Contains(errLower, "connection refused") ||
strings.Contains(errLower, "no such host") ||
strings.Contains(errLower, "i/o timeout") ||
strings.Contains(errLower, "connection reset") ||
strings.Contains(errLower, "network is unreachable")
}