fix(dolt): proper server mode support for routing and storage

- FindDatabasePath now handles Dolt server mode (no local dir required)
- main.go uses NewFromConfigWithOptions for Dolt to read server settings
- Routing uses factory via callback to respect backend configuration
- Handle Dolt "database exists" error (error 1007) gracefully

Previously, Dolt server mode failed because:
1. FindDatabasePath required a local directory to exist
2. main.go bypassed server mode config when creating Dolt storage
3. Routing always opened SQLite regardless of backend config

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
beads/crew/emma
2026-01-24 00:26:23 -08:00
committed by Steve Yegge
parent 13c362e67e
commit e82f5136c1
5 changed files with 71 additions and 41 deletions

View File

@@ -7,7 +7,7 @@ import (
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/storage/sqlite"
"github.com/steveyegge/beads/internal/storage/factory"
)
// ensureDirectMode makes sure the CLI is operating in direct-storage mode.
@@ -52,7 +52,8 @@ func disableDaemonForFallback(reason string) {
}
}
// ensureStoreActive guarantees that a local SQLite store is initialized and tracked.
// ensureStoreActive guarantees that a storage backend is initialized and tracked.
// Uses the factory to respect metadata.json backend configuration (SQLite, Dolt embedded, or Dolt server).
func ensureStoreActive() error {
lockStore()
active := isStoreActive() && getStore() != nil
@@ -61,15 +62,15 @@ func ensureStoreActive() error {
return nil
}
path := getDBPath()
if path == "" {
if found := beads.FindDatabasePath(); found != "" {
setDBPath(found)
path = found
} else {
// Check if this is a JSONL-only project
// Find the .beads directory
beadsDir := beads.FindBeadsDir()
if beadsDir != "" {
if beadsDir == "" {
return fmt.Errorf("no beads database found.\n" +
"Hint: run 'bd init' to create a database in the current directory,\n" +
" or use 'bd --no-db' for JSONL-only mode")
}
// Check if this is a JSONL-only project
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); err == nil {
// JSONL exists - check if no-db mode is configured
@@ -77,31 +78,28 @@ func ensureStoreActive() error {
return fmt.Errorf("this project uses JSONL-only mode (no SQLite database).\n" +
"Hint: use 'bd --no-db <command>' or set 'no-db: true' in config.yaml")
}
// JSONL exists but no-db not configured - fresh clone scenario
}
// Use factory to create the appropriate backend (SQLite, Dolt embedded, or Dolt server)
// based on metadata.json configuration
store, err := factory.NewFromConfig(getRootContext(), beadsDir)
if err != nil {
// Check for fresh clone scenario (JSONL exists but no database)
if _, statErr := os.Stat(jsonlPath); statErr == nil {
return fmt.Errorf("found JSONL file but no database: %s\n"+
"Hint: run 'bd init' to create the database and import issues,\n"+
" or use 'bd --no-db' for JSONL-only mode", jsonlPath)
}
}
return fmt.Errorf("no beads database found.\n" +
"Hint: run 'bd init' to create a database in the current directory,\n" +
" or use 'bd --no-db' for JSONL-only mode")
}
}
sqlStore, err := sqlite.New(getRootContext(), path)
if err != nil {
// Check for fresh clone scenario
if isFreshCloneError(err) {
beadsDir := filepath.Dir(path)
handleFreshCloneError(err, beadsDir)
return fmt.Errorf("database not initialized")
}
return fmt.Errorf("failed to open database: %w", err)
}
// Update the database path for compatibility with code that expects it
if dbPath := beads.FindDatabasePath(); dbPath != "" {
setDBPath(dbPath)
}
lockStore()
setStore(sqlStore)
setStore(store)
setStoreActive(true)
unlockStore()

View File

@@ -6,6 +6,7 @@ import (
"github.com/steveyegge/beads/internal/routing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/factory"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
@@ -41,7 +42,8 @@ func resolveAndGetIssueWithRouting(ctx context.Context, localStore storage.Stora
}
beadsDir := filepath.Dir(dbPath)
routedStorage, err := routing.GetRoutedStorageForID(ctx, id, beadsDir)
// Use factory.NewFromConfig as the storage opener to respect backend configuration
routedStorage, err := routing.GetRoutedStorageWithOpener(ctx, id, beadsDir, factory.NewFromConfig)
if err != nil {
return nil, err
}

View File

@@ -232,7 +232,11 @@ func findDatabaseInBeadsDir(beadsDir string, warnOnIssues bool) string {
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
backend := cfg.GetBackend()
if backend == configfile.BackendDolt {
// For Dolt, check if the configured database directory exists
// For Dolt server mode, database is on the server - no local directory required
if cfg.IsDoltServerMode() {
return cfg.DatabasePath(beadsDir)
}
// For embedded Dolt, check if the configured database directory exists
doltPath := cfg.DatabasePath(beadsDir)
if info, err := os.Stat(doltPath); err == nil && info.IsDir() {
return doltPath

View File

@@ -425,12 +425,27 @@ func (rs *RoutedStorage) Close() error {
return nil
}
// StorageOpener is a function that opens storage for a given beads directory.
// This allows callers to provide custom storage opening logic (e.g., using factory).
type StorageOpener func(ctx context.Context, beadsDir string) (storage.Storage, error)
// GetRoutedStorageForID returns a storage connection for the given issue ID.
// If the ID matches a route, it opens a connection to the routed database using SQLite.
// Otherwise, it returns nil (caller should use their existing storage).
//
// DEPRECATED: Use GetRoutedStorageWithOpener for proper backend support.
// The caller is responsible for closing the returned RoutedStorage.
func GetRoutedStorageForID(ctx context.Context, id, currentBeadsDir string) (*RoutedStorage, error) {
return GetRoutedStorageWithOpener(ctx, id, currentBeadsDir, nil)
}
// GetRoutedStorageWithOpener returns a storage connection for the given issue ID.
// If the ID matches a route, it opens a connection to the routed database.
// The opener function is used to create storage; if nil, defaults to SQLite.
// Otherwise, it returns nil (caller should use their existing storage).
//
// The caller is responsible for closing the returned RoutedStorage.
func GetRoutedStorageForID(ctx context.Context, id, currentBeadsDir string) (*RoutedStorage, error) {
func GetRoutedStorageWithOpener(ctx context.Context, id, currentBeadsDir string, opener StorageOpener) (*RoutedStorage, error) {
beadsDir, routed, err := ResolveBeadsDirForID(ctx, id, currentBeadsDir)
if err != nil {
return nil, err
@@ -441,8 +456,14 @@ func GetRoutedStorageForID(ctx context.Context, id, currentBeadsDir string) (*Ro
}
// Open storage for the routed directory
var store storage.Storage
if opener != nil {
store, err = opener(ctx, beadsDir)
} else {
// Default to SQLite for backward compatibility
dbPath := filepath.Join(beadsDir, "beads.db")
store, err := sqlite.New(ctx, dbPath)
store, err = sqlite.New(ctx, dbPath)
}
if err != nil {
return nil, err
}

View File

@@ -312,9 +312,14 @@ func openServerConnection(ctx context.Context, cfg *Config) (*sql.DB, string, er
_, err = initDB.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", cfg.Database))
if err != nil {
// Dolt may return error 1007 even with IF NOT EXISTS - ignore if database already exists
errLower := strings.ToLower(err.Error())
if !strings.Contains(errLower, "database exists") && !strings.Contains(errLower, "1007") {
_ = db.Close()
return nil, "", fmt.Errorf("failed to create database: %w", err)
}
// Database already exists - that's fine, continue
}
return db, connStr, nil
}