beads/internal/storage/versioncontrolops/backup.go
Robin Carswell a097723f28
fix: suppress auto-backup address conflict warning (#2927)
* fix: suppress auto-backup address conflict warning when default backup is configured

When a user runs `bd backup init <path>` it registers a Dolt backup named
"default". The auto-backup system also calls BackupDatabase() which registers
its own remote named "backup_export" pointing to the same .beads/backup/
directory. Dolt returns "Error 1105: address conflict with a remote: 'default'"
because two remotes cannot share a URL, causing a noisy warning on every write:

  Warning: auto-backup failed: register backup remote: add backup backup_export: \
  Error 1105: address conflict with a remote: 'default' -> file://.../backup

Fix: when BackupAdd fails with an address conflict, extract the name of the
already-registered remote from the error message and sync using that name
instead. The backup still runs — it just reuses the existing remote.

Adds ExtractAddressConflictName() to versioncontrolops for parsing the
conflicting name out of the Dolt error. Applies the fix to both the
EmbeddedDoltStore and DoltStore implementations of BackupDatabase().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* test: add table-driven tests for ExtractAddressConflictName

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-02 20:28:47 -07:00

86 lines
2.7 KiB
Go

package versioncontrolops
import (
"context"
"fmt"
"path/filepath"
"strings"
)
// BackupAdd registers a Dolt backup destination.
func BackupAdd(ctx context.Context, db DBConn, name, url string) error {
if _, err := db.ExecContext(ctx, "CALL DOLT_BACKUP('add', ?, ?)", name, url); err != nil {
return fmt.Errorf("add backup %s: %w", name, err)
}
return nil
}
// BackupSync pushes the database to the named backup destination.
func BackupSync(ctx context.Context, db DBConn, name string) error {
if _, err := db.ExecContext(ctx, "CALL DOLT_BACKUP('sync', ?)", name); err != nil {
return fmt.Errorf("sync backup %s: %w", name, err)
}
return nil
}
// BackupRemove removes a configured Dolt backup destination.
func BackupRemove(ctx context.Context, db DBConn, name string) error {
if _, err := db.ExecContext(ctx, "CALL DOLT_BACKUP('rm', ?)", name); err != nil {
return fmt.Errorf("remove backup %s: %w", name, err)
}
return nil
}
// BackupRestore restores a database from a backup at the given URL into
// the named database. When force is true, an existing database with the
// same name is overwritten. Mirrors the CLI: dolt backup restore [--force] <url> <db_name>
func BackupRestore(ctx context.Context, db DBConn, url, dbName string, force bool) error {
if force {
if _, err := db.ExecContext(ctx, "CALL DOLT_BACKUP('restore', '--force', ?, ?)", url, dbName); err != nil {
return fmt.Errorf("restore from backup %s: %w", url, err)
}
} else {
if _, err := db.ExecContext(ctx, "CALL DOLT_BACKUP('restore', ?, ?)", url, dbName); err != nil {
return fmt.Errorf("restore from backup %s: %w", url, err)
}
}
return nil
}
// DirToFileURL resolves dir to an absolute path and returns a file:// URL.
func DirToFileURL(dir string) (string, error) {
abs, err := filepath.Abs(dir)
if err != nil {
return "", fmt.Errorf("resolve absolute path: %w", err)
}
return "file://" + abs, nil
}
// ExtractAddressConflictName parses the conflicting remote name from a Dolt
// "address conflict with a remote" error.
//
// Dolt returns errors of the form:
//
// Error 1105: address conflict with a remote: 'name' -> url
//
// When BackupAdd fails because another remote (e.g. "default", registered by
// `bd backup init`) already points to the same URL, the caller can use the
// conflicting name to sync directly rather than treating it as a hard error.
// Returns "" if the error is not an address conflict.
func ExtractAddressConflictName(err error) string {
if err == nil {
return ""
}
s := err.Error()
const marker = "address conflict with a remote: '"
idx := strings.Index(s, marker)
if idx == -1 {
return ""
}
s = s[idx+len(marker):]
end := strings.Index(s, "'")
if end == -1 {
return ""
}
return s[:end]
}