diff --git a/cmd/bd/doctor/database.go b/cmd/bd/doctor/database.go index 7cc44048a..451b6f35d 100644 --- a/cmd/bd/doctor/database.go +++ b/cmd/bd/doctor/database.go @@ -91,14 +91,10 @@ func CheckDatabaseVersion(path string, cliVersion string) DoctorCheck { // CheckDatabaseVersionWithStore checks the database version using a shared store (GH#2636). func CheckDatabaseVersionWithStore(ss *SharedStore, cliVersion string) DoctorCheck { - beadsDir := "" - if ss != nil { - beadsDir = ss.BeadsDir() - } + beadsDir := sharedStoreBeadsDir(ss) store := ss.Store() if store == nil { - cfg, err := configfile.Load(beadsDir) - if err == nil && cfg != nil && cfg.IsDoltServerMode() { + if !sharedStoreNeedsLocalDoltDir(beadsDir) { return DoctorCheck{ Name: "Database", Status: StatusError, diff --git a/cmd/bd/doctor/installation.go b/cmd/bd/doctor/installation.go index 781228e0b..ad327ce28 100644 --- a/cmd/bd/doctor/installation.go +++ b/cmd/bd/doctor/installation.go @@ -93,10 +93,8 @@ func CheckPermissions(path string) DoctorCheck { // CheckPermissionsWithStore verifies permissions using a shared store (GH#2636). // If the shared store was opened successfully, the database is accessible. func CheckPermissionsWithStore(path string, ss *SharedStore) DoctorCheck { - beadsDir := ResolveBeadsDirForRepo(path) - if ss != nil && ss.BeadsDir() != "" { - beadsDir = ss.BeadsDir() - } + beadsDir := beadsDirFromSharedStore(path, ss) + store := ss.Store() // Check if .beads/ is writable testFile := filepath.Join(beadsDir, ".doctor-test-write") @@ -113,9 +111,8 @@ func CheckPermissionsWithStore(path string, ss *SharedStore) DoctorCheck { // Check Dolt database directory permissions cfg, err := configfile.Load(beadsDir) if err == nil && cfg != nil && cfg.GetBackend() == configfile.BackendDolt { - doltPath := getDatabasePath(beadsDir) if cfg.IsDoltServerMode() { - if ss == nil || ss.Store() == nil { + if store == nil { return DoctorCheck{ Name: "Permissions", Status: StatusError, @@ -129,6 +126,8 @@ func CheckPermissionsWithStore(path string, ss *SharedStore) DoctorCheck { Message: "All permissions OK", } } + + doltPath := getDatabasePath(beadsDir) if info, err := os.Stat(doltPath); err == nil { if !info.IsDir() { return DoctorCheck{ @@ -139,7 +138,7 @@ func CheckPermissionsWithStore(path string, ss *SharedStore) DoctorCheck { } } // If shared store is nil, the database could not be opened - if ss.Store() == nil { + if store == nil { return DoctorCheck{ Name: "Permissions", Status: StatusError, diff --git a/cmd/bd/doctor/shared_store.go b/cmd/bd/doctor/shared_store.go index 13c97cd2f..69c8c8f4e 100644 --- a/cmd/bd/doctor/shared_store.go +++ b/cmd/bd/doctor/shared_store.go @@ -26,6 +26,25 @@ type SharedStore struct { beadsDir string } +func beadsDirFromSharedStore(path string, ss *SharedStore) string { + if beadsDir := sharedStoreBeadsDir(ss); beadsDir != "" { + return beadsDir + } + return ResolveBeadsDirForRepo(path) +} + +func sharedStoreBeadsDir(ss *SharedStore) string { + if ss == nil { + return "" + } + return ss.BeadsDir() +} + +func sharedStoreNeedsLocalDoltDir(beadsDir string) bool { + cfg, err := configfile.Load(beadsDir) + return err != nil || cfg == nil || !cfg.IsDoltServerMode() +} + // NewSharedStore opens a single read-only DoltStore for the given repo path. // If the database doesn't exist or can't be opened, Store() will return nil. // The caller MUST call Close() when done (typically via defer). @@ -33,13 +52,7 @@ func NewSharedStore(path string) *SharedStore { beadsDir := ResolveBeadsDirForRepo(path) ss := &SharedStore{beadsDir: beadsDir} - cfg, err := configfile.Load(beadsDir) - if err != nil || cfg == nil { - doltPath := getDatabasePath(beadsDir) - if _, err := os.Stat(doltPath); os.IsNotExist(err) { - return ss // No database, store stays nil - } - } else if !cfg.IsDoltServerMode() { + if sharedStoreNeedsLocalDoltDir(beadsDir) { doltPath := getDatabasePath(beadsDir) if _, err := os.Stat(doltPath); os.IsNotExist(err) { return ss // No database, store stays nil