beads/cmd/bd/backend_support.go
Julian Knutsen b13fab0151
fix(storage): truthful legacy-backend tombstone rejection — detect present Dolt data, give the exact heal (D-8) (#6044)
* 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>
2026-08-28 23:27:39 -07:00

76 lines
2.5 KiB
Go

package main
import (
"fmt"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage/backends"
)
// validateConfiguredBackend fails closed on metadata that selects a removed or
// unrecognized backend. beadsDir is the workspace whose metadata cfg came from;
// the rejection inspects it (read-only) so a workspace that already holds a Dolt
// database is told to fix the stale "backend" value rather than to export and
// reinitialize. Pass "" only where there is genuinely no workspace directory.
func validateConfiguredBackend(cfg *configfile.Config, beadsDir string) error {
if cfg == nil {
return nil
}
if backends.Registered(cfg.Backend) {
return nil
}
switch cfg.Backend {
case configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite:
return configfile.RemovedBackendErrorAt(cfg.Backend, beadsDir, cfg)
case "", configfile.BackendDolt:
return nil
default:
return configfile.UnknownBackendErrorAt(cfg.Backend, beadsDir, cfg)
}
}
// registeredBackendWorkspaceIsBeadsDir reports whether metadata selects a
// backend that has no separately discoverable local database.
func registeredBackendWorkspaceIsBeadsDir(cfg *configfile.Config) bool {
if cfg == nil {
return false
}
return backends.WorkspaceIsBeadsDir(cfg.GetBackend())
}
func requireDoltBackend(cfg *configfile.Config, beadsDir string) error {
if err := validateConfiguredBackend(cfg, beadsDir); err != nil {
return err
}
if cfg != nil && cfg.GetBackend() != configfile.BackendDolt {
return fmt.Errorf("not using Dolt backend (configured backend %q)", cfg.GetBackend())
}
return nil
}
// normalizeLoadedConfig substitutes the default config for an absent
// metadata.json (cfg == nil) so mode inference still runs: a remote host
// supplied via BEADS_DOLT_SERVER_HOST or config.yaml dolt.host (GH#3545)
// must select server mode even when no metadata.json exists — otherwise
// the CLI silently opens the embedded store against a remote-host
// configuration.
func normalizeLoadedConfig(cfg *configfile.Config) *configfile.Config {
if cfg == nil {
return configfile.DefaultConfig()
}
return cfg
}
func loadDoltBackendConfig(beadsDir string) (*configfile.Config, error) {
cfg, err := configfile.Load(beadsDir)
if err != nil {
return nil, fmt.Errorf("loading config: %w", err)
}
if cfg == nil {
cfg = configfile.DefaultConfig()
}
if err := requireDoltBackend(cfg, beadsDir); err != nil {
return nil, err
}
return cfg, nil
}