Fix Dolt backend init/daemon/doctor; prevent accidental SQLite artifacts; add integration tests; clean up lint (#1218)
* /{cmd,internal}: get dolt backend init working and allow issue creation
* /{website,internal,docs,cmd}: integration tests and more split backend fixes
* /{cmd,internal}: fix lint issues
* /cmd/bd/doctor/integrity.go: fix unable to query issues bug with dolt backend
* /cmd/bd/daemon.go: remove debug logging
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const ConfigFileName = "metadata.json"
|
||||
@@ -37,7 +38,7 @@ func ConfigPath(beadsDir string) string {
|
||||
|
||||
func Load(beadsDir string) (*Config, error) {
|
||||
configPath := ConfigPath(beadsDir)
|
||||
|
||||
|
||||
data, err := os.ReadFile(configPath) // #nosec G304 - controlled path from config
|
||||
if os.IsNotExist(err) {
|
||||
// Try legacy config.json location (migration path)
|
||||
@@ -49,52 +50,79 @@ func Load(beadsDir string) (*Config, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading legacy config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// Migrate: parse legacy config, save as metadata.json, remove old file
|
||||
var cfg Config
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("parsing legacy config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// Save to new location
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
return nil, fmt.Errorf("migrating config to metadata.json: %w", err)
|
||||
}
|
||||
|
||||
|
||||
// Remove legacy file (best effort)
|
||||
_ = os.Remove(legacyPath)
|
||||
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
var cfg Config
|
||||
if err := json.Unmarshal(data, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("parsing config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) Save(beadsDir string) error {
|
||||
configPath := ConfigPath(beadsDir)
|
||||
|
||||
|
||||
data, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshaling config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
if err := os.WriteFile(configPath, data, 0600); err != nil {
|
||||
return fmt.Errorf("writing config: %w", err)
|
||||
}
|
||||
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) DatabasePath(beadsDir string) string {
|
||||
return filepath.Join(beadsDir, c.Database)
|
||||
backend := c.GetBackend()
|
||||
|
||||
// Treat Database as the on-disk storage location:
|
||||
// - SQLite: filename (default: beads.db)
|
||||
// - Dolt: directory name (default: dolt)
|
||||
//
|
||||
// Backward-compat: early dolt configs wrote "beads.db" even when Backend=dolt.
|
||||
// In that case, treat it as "dolt".
|
||||
if backend == BackendDolt {
|
||||
db := strings.TrimSpace(c.Database)
|
||||
if db == "" || db == "beads.db" {
|
||||
db = "dolt"
|
||||
}
|
||||
if filepath.IsAbs(db) {
|
||||
return db
|
||||
}
|
||||
return filepath.Join(beadsDir, db)
|
||||
}
|
||||
|
||||
// SQLite (default)
|
||||
db := strings.TrimSpace(c.Database)
|
||||
if db == "" {
|
||||
db = "beads.db"
|
||||
}
|
||||
if filepath.IsAbs(db) {
|
||||
return db
|
||||
}
|
||||
return filepath.Join(beadsDir, db)
|
||||
}
|
||||
|
||||
func (c *Config) JSONLPath(beadsDir string) string {
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
|
||||
func TestDefaultConfig(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
|
||||
|
||||
if cfg.Database != "beads.db" {
|
||||
t.Errorf("Database = %q, want beads.db", cfg.Database)
|
||||
}
|
||||
@@ -25,26 +25,26 @@ func TestLoadSaveRoundtrip(t *testing.T) {
|
||||
if err := os.MkdirAll(beadsDir, 0750); err != nil {
|
||||
t.Fatalf("failed to create .beads directory: %v", err)
|
||||
}
|
||||
|
||||
|
||||
cfg := DefaultConfig()
|
||||
|
||||
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Save() failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
loaded, err := Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() failed: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if loaded == nil {
|
||||
t.Fatal("Load() returned nil config")
|
||||
}
|
||||
|
||||
|
||||
if loaded.Database != cfg.Database {
|
||||
t.Errorf("Database = %q, want %q", loaded.Database, cfg.Database)
|
||||
}
|
||||
|
||||
|
||||
if loaded.JSONLExport != cfg.JSONLExport {
|
||||
t.Errorf("JSONLExport = %q, want %q", loaded.JSONLExport, cfg.JSONLExport)
|
||||
}
|
||||
@@ -52,12 +52,12 @@ func TestLoadSaveRoundtrip(t *testing.T) {
|
||||
|
||||
func TestLoadNonexistent(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
|
||||
cfg, err := Load(tmpDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() returned error for nonexistent config: %v", err)
|
||||
}
|
||||
|
||||
|
||||
if cfg != nil {
|
||||
t.Errorf("Load() = %v, want nil for nonexistent config", cfg)
|
||||
}
|
||||
@@ -66,22 +66,44 @@ func TestLoadNonexistent(t *testing.T) {
|
||||
func TestDatabasePath(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
cfg := &Config{Database: "beads.db"}
|
||||
|
||||
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "beads.db")
|
||||
|
||||
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDatabasePath_Dolt(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
||||
t.Run("explicit dolt dir", func(t *testing.T) {
|
||||
cfg := &Config{Database: "dolt", Backend: BackendDolt}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "dolt")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("backward compat: dolt backend with beads.db field", func(t *testing.T) {
|
||||
cfg := &Config{Database: "beads.db", Backend: BackendDolt}
|
||||
got := cfg.DatabasePath(beadsDir)
|
||||
want := filepath.Join(beadsDir, "dolt")
|
||||
if got != want {
|
||||
t.Errorf("DatabasePath() = %q, want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestJSONLPath(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *Config
|
||||
want string
|
||||
name string
|
||||
cfg *Config
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "default",
|
||||
@@ -99,7 +121,7 @@ func TestJSONLPath(t *testing.T) {
|
||||
want: filepath.Join(beadsDir, "issues.jsonl"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.cfg.JSONLPath(beadsDir)
|
||||
@@ -122,9 +144,9 @@ func TestConfigPath(t *testing.T) {
|
||||
|
||||
func TestGetDeletionsRetentionDays(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg *Config
|
||||
want int
|
||||
name string
|
||||
cfg *Config
|
||||
want int
|
||||
}{
|
||||
{
|
||||
name: "zero uses default",
|
||||
|
||||
Reference in New Issue
Block a user