* fix(configfile): detect present Dolt data and give the exact metadata heal (D-8) A workspace whose metadata.json names sqlite/postgres/mysql stays fail-closed, but the rejection text was not truthful for the common case. bd v1.2.x opened those workspaces as Dolt regardless of the "backend" value, so for them the stale field is the only problem — and the old remedy sent the user to export and reinitialize, destroying a database that is one JSON edit from healthy. ExistingDoltDataDir stats the embedded (.beads/embeddeddolt) and server-mode (.beads/dolt, dolt_data_dir, BEADS_DOLT_DATA_DIR) layouts for a .dolt directory, either at the root or one level down. Detection is stat/readdir only — every caller is on a path that has promised not to touch storage. The detection approach is ported from the obsolete PR #4740. The removed- and unknown-backend messages now branch on it: with Dolt data present they name metadata.json and the exact edit ("backend": "dolt", or drop the field); without it they keep today's export/reinitialize path, so the substrings existing tests pin are unchanged. GetDoltDataDir becomes nil-safe like GetBackend, since rejection paths resolve it for configs that may be nil. Co-authored-by: Steve Yegge <steve.yegge@gmail.com> * fix(storage): thread beadsDir into every legacy-backend rejection (D-8) All three rejection surfaces now branch on whether the workspace actually holds a Dolt database: cmd/bd's validateConfiguredBackend (12 call sites, every one already had beadsDir in scope), internal/storage/dolt's requireDoltBackend, and the public beads.OpenBestAvailable path. Widening the signatures rather than adding parallel *At helpers is deliberate — it makes each site state which workspace it is rejecting, so none can silently keep the generic message. Behavior is unchanged for workspaces with no Dolt data; test call sites take the new argument but no assertion changes. * test(storage): cover the tombstone heal branch and its round-trip (D-8) The headline test is the round-trip: init a real Dolt workspace, hand-edit metadata.json to backend:"sqlite", watch bd list exit 1 with the heal message, apply exactly the edit that message prescribes, and confirm bd list opens the workspace with its issue intact. The message's stated alternative — deleting the "backend" field — is exercised too, so both remedies it offers are proven rather than asserted. The same test pins the fail-closed guarantee at a level a stat loop could not: metadata.json stays byte-identical and the whole embedded Dolt tree is unchanged in paths and sizes after the refusal. Around it: the ExistingDoltDataDir layout matrix (embedded and server layouts at both depths, relative and absolute dolt_data_dir, BEADS_DOLT_DATA_DIR, decoys, no workspace dir), string-level branch coverage in internal/configfile, a public OpenBestAvailable case proving beadsDir reaches the library refusal, and an explicit assertion that the no-Dolt-data branch still refuses to suggest a heal that would open an empty store. --------- Co-authored-by: Steve Yegge <steve.yegge@gmail.com>
248 lines
9.6 KiB
Go
248 lines
9.6 KiB
Go
package beads_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/backends"
|
|
)
|
|
|
|
var errRegistryBackendOpen = errors.New("registry backend open sentinel")
|
|
|
|
// registerWorkspaceBackend registers a fake WorkspaceIsBeadsDir backend whose
|
|
// Open/OpenReadOnly report a sentinel instead of touching a store, so tests can
|
|
// assert that discovery and open dispatch to the registry without provisioning
|
|
// Dolt. Register requires both hooks to be non-nil.
|
|
func registerWorkspaceBackend(t *testing.T, name string) {
|
|
t.Helper()
|
|
backends.Register(name, backends.Backend{
|
|
Open: func(context.Context, string) (storage.DoltStorage, error) {
|
|
return nil, errRegistryBackendOpen
|
|
},
|
|
OpenReadOnly: func(context.Context, string) (storage.DoltStorage, error) {
|
|
return nil, errRegistryBackendOpen
|
|
},
|
|
WorkspaceIsBeadsDir: true,
|
|
})
|
|
t.Cleanup(func() { backends.Deregister(name) })
|
|
}
|
|
|
|
func writeBackendMetadata(t *testing.T, backend string) string {
|
|
t.Helper()
|
|
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
|
t.Fatalf("create .beads: %v", err)
|
|
}
|
|
metadata := `{"backend":"` + backend + `"}`
|
|
if backend == "sqlite" {
|
|
// Workspaces created by the removed SQLite backend carry an explicit
|
|
// path marker; a bare backend:"sqlite" can also be stale metadata from
|
|
// the earlier SQLite era (see PR #4740). Both must hit the tombstone.
|
|
metadata = `{"backend":"sqlite","sqlite_path":"beads.db"}`
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(metadata), 0o600); err != nil {
|
|
t.Fatalf("write metadata: %v", err)
|
|
}
|
|
return beadsDir
|
|
}
|
|
|
|
func TestOpenBestAvailableRejectsSQLite(t *testing.T) {
|
|
beadsDir := writeBackendMetadata(t, "sqlite")
|
|
store, err := beads.OpenBestAvailable(context.Background(), beadsDir)
|
|
if store != nil {
|
|
_ = store.Close()
|
|
t.Fatal("removed SQLite backend returned a store")
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), "no longer supported") {
|
|
t.Fatalf("SQLite backend error = %v, want rollback explanation", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "single engine") || !strings.Contains(err.Error(), "export") {
|
|
t.Fatalf("SQLite backend error lacks rationale or migration guidance: %v", err)
|
|
}
|
|
// The fail-closed guarantee includes never provisioning the SQLite file the
|
|
// removed backend would have created.
|
|
for _, name := range []string{"embeddeddolt", "dolt", "beads.db"} {
|
|
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
|
t.Fatalf("removed SQLite backend created %s (stat error: %v)", name, statErr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpenBestAvailableRejectsRemovedBackends(t *testing.T) {
|
|
for _, backend := range []string{"postgres", "mysql", "sqlite"} {
|
|
t.Run(backend, func(t *testing.T) {
|
|
beadsDir := writeBackendMetadata(t, backend)
|
|
store, err := beads.OpenBestAvailable(context.Background(), beadsDir)
|
|
if store != nil {
|
|
_ = store.Close()
|
|
t.Fatalf("removed backend %q returned a store", backend)
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), "no longer supported") {
|
|
t.Fatalf("removed backend error = %v, want rollback explanation", err)
|
|
}
|
|
rationale := "resource-light"
|
|
if backend == "sqlite" {
|
|
rationale = "single engine"
|
|
}
|
|
if !strings.Contains(err.Error(), rationale) || !strings.Contains(err.Error(), "export") {
|
|
t.Fatalf("removed backend error lacks rationale or migration guidance: %v", err)
|
|
}
|
|
for _, name := range []string{"embeddeddolt", "dolt", "beads.db"} {
|
|
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
|
t.Fatalf("removed backend created %s (stat error: %v)", name, statErr)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestOpenBestAvailableOffersHealWhenDoltDataExists covers the public open path
|
|
// for the case D-8 exists for: metadata names a removed backend, but the
|
|
// workspace holds a Dolt database that bd v1.2.x opened happily. The library
|
|
// error must carry the same metadata heal the CLI gives — proving beadsDir and
|
|
// the config reach configuredBackendUnavailable in both the cgo and non-cgo
|
|
// builds — instead of the export/reinitialize path that destroys the database.
|
|
func TestOpenBestAvailableOffersHealWhenDoltDataExists(t *testing.T) {
|
|
for _, backend := range []string{"postgres", "mysql", "sqlite"} {
|
|
t.Run(backend, func(t *testing.T) {
|
|
beadsDir := writeBackendMetadata(t, backend)
|
|
doltDir := filepath.Join(beadsDir, "embeddeddolt", "beads")
|
|
if err := os.MkdirAll(filepath.Join(doltDir, ".dolt"), 0o750); err != nil {
|
|
t.Fatalf("plant dolt database: %v", err)
|
|
}
|
|
|
|
store, err := beads.OpenBestAvailable(context.Background(), beadsDir)
|
|
if store != nil {
|
|
_ = store.Close()
|
|
t.Fatalf("removed backend %q returned a store", backend)
|
|
}
|
|
if err == nil {
|
|
t.Fatal("removed backend with live Dolt data unexpectedly opened")
|
|
}
|
|
for _, want := range []string{
|
|
"no longer supported",
|
|
filepath.Join(beadsDir, "metadata.json"),
|
|
`"backend": "dolt"`,
|
|
doltDir,
|
|
"no storage database was opened or modified",
|
|
} {
|
|
if !strings.Contains(err.Error(), want) {
|
|
t.Errorf("public open error missing %q: %v", want, err)
|
|
}
|
|
}
|
|
if strings.Contains(strings.ToLower(err.Error()), "export") {
|
|
t.Errorf("public open error offered the destructive export path despite live Dolt data: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOpenBestAvailableRejectsUnknownBackend(t *testing.T) {
|
|
beadsDir := writeBackendMetadata(t, "mystery")
|
|
store, err := beads.OpenBestAvailable(context.Background(), beadsDir)
|
|
if store != nil {
|
|
_ = store.Close()
|
|
t.Fatal("unknown backend returned a store")
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), "not recognized") {
|
|
t.Fatalf("unknown backend error = %v, want fail-closed metadata guidance", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "no storage database was opened or modified") {
|
|
t.Fatalf("unknown backend error lacks data-safety guarantee: %v", err)
|
|
}
|
|
for _, name := range []string{"embeddeddolt", "dolt", "beads.db"} {
|
|
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
|
t.Fatalf("unknown backend created %s (stat error: %v)", name, statErr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpenBestAvailableRejectsCorruptMetadata(t *testing.T) {
|
|
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
|
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
|
t.Fatalf("create .beads: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{"), 0o600); err != nil {
|
|
t.Fatalf("write metadata: %v", err)
|
|
}
|
|
|
|
store, err := beads.OpenBestAvailable(context.Background(), beadsDir)
|
|
if store != nil {
|
|
_ = store.Close()
|
|
t.Fatal("corrupt metadata unexpectedly returned a store")
|
|
}
|
|
if err == nil || !strings.Contains(err.Error(), "metadata") {
|
|
t.Fatalf("corrupt metadata error = %v, want metadata load failure", err)
|
|
}
|
|
if _, statErr := os.Stat(filepath.Join(beadsDir, "embeddeddolt")); !os.IsNotExist(statErr) {
|
|
t.Fatalf("corrupt metadata created embedded Dolt storage (stat error: %v)", statErr)
|
|
}
|
|
}
|
|
|
|
// TestOpenBestAvailableDispatchesRegisteredBackend covers the public library
|
|
// open path for a registered extension backend: OpenBestAvailable must call the
|
|
// backend rather than opening Dolt (CGO) or returning the embedded-Dolt error
|
|
// (non-CGO), mirroring the CLI store factories.
|
|
func TestOpenBestAvailableDispatchesRegisteredBackend(t *testing.T) {
|
|
const name = "registry-open"
|
|
registerWorkspaceBackend(t, name)
|
|
|
|
beadsDir := writeBackendMetadata(t, name)
|
|
store, err := beads.OpenBestAvailable(context.Background(), beadsDir)
|
|
if store != nil {
|
|
_ = store.Close()
|
|
t.Fatal("registered backend dispatch returned a store instead of the backend's own result")
|
|
}
|
|
if !errors.Is(err, errRegistryBackendOpen) {
|
|
t.Fatalf("OpenBestAvailable error = %v, want registered backend Open result", err)
|
|
}
|
|
// Dispatch must not fall through and provision an embedded Dolt store.
|
|
for _, artifact := range []string{"embeddeddolt", "dolt"} {
|
|
if _, statErr := os.Stat(filepath.Join(beadsDir, artifact)); !os.IsNotExist(statErr) {
|
|
t.Fatalf("registered backend dispatch created %s (stat error: %v)", artifact, statErr)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestFindDatabasePathDiscoversRegisteredWorkspace covers public discovery
|
|
// parity: a registered WorkspaceIsBeadsDir backend has no local Dolt database,
|
|
// so FindDatabasePath must return the .beads directory itself instead of the
|
|
// empty "no database" result the Dolt-only search would give.
|
|
func TestFindDatabasePathDiscoversRegisteredWorkspace(t *testing.T) {
|
|
const name = "registry-discovery"
|
|
registerWorkspaceBackend(t, name)
|
|
|
|
beadsDir := writeBackendMetadata(t, name)
|
|
t.Setenv("BEADS_DIR", beadsDir)
|
|
|
|
got := beads.FindDatabasePath()
|
|
if got == "" {
|
|
t.Fatal("FindDatabasePath returned empty for a WorkspaceIsBeadsDir backend")
|
|
}
|
|
// Path canonicalization (symlinked temp dirs) can rewrite the string, so
|
|
// compare the workspace by identity rather than raw path equality.
|
|
gotInfo, err := os.Stat(got)
|
|
if err != nil {
|
|
t.Fatalf("stat discovered path %q: %v", got, err)
|
|
}
|
|
wantInfo, err := os.Stat(beadsDir)
|
|
if err != nil {
|
|
t.Fatalf("stat beads dir %q: %v", beadsDir, err)
|
|
}
|
|
if !os.SameFile(gotInfo, wantInfo) {
|
|
t.Fatalf("FindDatabasePath = %q, want the .beads workspace dir %q", got, beadsDir)
|
|
}
|
|
// The registry-only workspace carries no local Dolt database and discovery
|
|
// must not create one.
|
|
for _, artifact := range []string{"embeddeddolt", "dolt"} {
|
|
if _, statErr := os.Stat(filepath.Join(beadsDir, artifact)); !os.IsNotExist(statErr) {
|
|
t.Fatalf("registry workspace discovery created %s (stat error: %v)", artifact, statErr)
|
|
}
|
|
}
|
|
}
|