/cmd/bd: support --purge-dropped in proxied-server mode

This commit is contained in:
coffeegoddd☕️✨ 2026-07-24 13:38:54 -07:00
commit de05a3d564
5 changed files with 306 additions and 184 deletions

View file

@ -81,6 +81,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- **`bd dolt clean-databases --purge-dropped` now works in proxied-server
mode** (p1-9lf). The command read the flag and then dropped it on the floor
before dispatching to the proxied-server implementation, so under
`--proxied-server` stale databases were dropped but never purged: their
directories stayed under `.dolt_dropped_databases/`, disk was never
reclaimed, and nothing reported that the flag had done nothing. The proxied
dual had drifted further than that one flag — it also lacked the
batching/circuit-breaker drop loop, the dry-run "purge ignored" notice, and
the `DOLT_UNDROP` recoverability trailer, and its early return on "nothing
stale found" was incompatible with purge semantics (a prior run's residue is
invisible to `SHOW DATABASES`, so `--purge-dropped` must still fire when this
run drops nothing). Both topologies now share one implementation keyed on
`versioncontrolops.DBConn`, which `*sql.DB` (direct server) and the pinned
non-transactional `*sql.Conn` (proxied server, via
`uow.MaintenanceProvider.RunNonTx`) both satisfy, so the behavior cannot
diverge again. Per-operation timeouts now derive from the command's context
on both paths, so Ctrl-C interrupts a long clean.
- **`bd dolt clean-databases` gains an opt-in `--purge-dropped` flag to
reclaim disk from what it drops** (be-pq5,
[#3663](https://github.com/gastownhall/beads/pull/3663)). `DROP DATABASE`

View file

@ -22,6 +22,7 @@ import (
"github.com/steveyegge/beads/internal/storage/dberrors"
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
"github.com/steveyegge/beads/internal/storage/doltutil"
"github.com/steveyegge/beads/internal/storage/versioncontrolops"
"github.com/steveyegge/beads/internal/ui"
"golang.org/x/term"
)
@ -1034,9 +1035,10 @@ recovery.`,
}
dryRun, _ := cmd.Flags().GetBool("dry-run")
purgeDropped, _ := cmd.Flags().GetBool("purge-dropped")
opts := cleanDatabasesOptions{dryRun: dryRun, purgeDropped: purgeDropped}
if usesProxiedServer() {
return runDoltCleanDatabasesProxied(rootCtx, beadsDir, dryRun)
return runDoltCleanDatabasesProxied(rootCtx, beadsDir, opts)
}
// Connect directly to the Dolt server via config instead of getStore(),
@ -1047,125 +1049,7 @@ recovery.`,
}
defer cleanup()
listCtx, listCancel := context.WithTimeout(context.Background(), 30*time.Second)
defer listCancel()
rows, err := db.QueryContext(listCtx, "SHOW DATABASES")
if err != nil {
return HandleError("listing databases: %v", err)
}
defer rows.Close()
var stale []string
for rows.Next() {
var dbName string
if err := rows.Scan(&dbName); err != nil {
continue
}
for _, prefix := range staleDatabasePrefixes {
if strings.HasPrefix(dbName, prefix) {
stale = append(stale, dbName)
break
}
}
}
if len(stale) == 0 {
fmt.Println("No stale databases found.")
} else {
fmt.Printf("Found %d stale databases:\n", len(stale))
for _, name := range stale {
fmt.Printf(" %s\n", name)
}
}
if dryRun {
if len(stale) > 0 {
fmt.Println("\n(dry run — no databases dropped)")
}
if purgeDropped {
fmt.Println("(dry run — --purge-dropped ignored; no purge performed)")
}
return nil
}
dropped := 0
if len(stale) > 0 {
fmt.Println()
failures := 0
consecutiveTimeouts := 0
const (
batchSize = 5 // Drop this many before pausing
batchPause = 2 * time.Second
backoffPause = 10 * time.Second
timeoutThreshold = 3 // Consecutive timeouts before backoff
perDropTimeout = 30 * time.Second
maxConsecFailures = 10 // Stop after this many consecutive failures
)
for i, name := range stale {
// Circuit breaker: back off when server is overwhelmed
if consecutiveTimeouts >= timeoutThreshold {
fmt.Fprintf(os.Stderr, " ⚠ %d consecutive timeouts — backing off %s\n",
consecutiveTimeouts, backoffPause)
time.Sleep(backoffPause)
consecutiveTimeouts = 0
}
// Stop if too many consecutive failures — server is likely unhealthy
if failures >= maxConsecFailures {
fmt.Fprintf(os.Stderr, "\n✗ Aborting: %d consecutive failures suggest server is unhealthy.\n", failures)
fmt.Fprintf(os.Stderr, " Dropped %d/%d before stopping.\n", dropped, len(stale))
return SilentExit()
}
// Per-operation timeout: DROP DATABASE can be slow on Dolt
dropCtx, dropCancel := context.WithTimeout(context.Background(), perDropTimeout)
// Escape backticks in database name to prevent SQL injection (` → ``)
safeName := strings.ReplaceAll(name, "`", "``")
_, err := db.ExecContext(dropCtx, fmt.Sprintf("DROP DATABASE `%s`", safeName)) //nolint:gosec // G201: identifier-escaped
dropCancel()
if err != nil {
fmt.Fprintf(os.Stderr, " FAIL: %s: %v\n", name, err)
failures++
if isTimeoutError(err) {
consecutiveTimeouts++
}
} else {
fmt.Printf(" Dropped: %s\n", name)
dropped++
failures = 0
consecutiveTimeouts = 0
}
// Rate limiting: pause between batches to let the server breathe
if (i+1)%batchSize == 0 && i+1 < len(stale) {
fmt.Printf(" [%d/%d] pausing %s...\n", i+1, len(stale), batchPause)
time.Sleep(batchPause)
}
}
fmt.Printf("\nDropped %d/%d stale databases.\n", dropped, len(stale))
}
// DROP DATABASE only marks a database as dropped; Dolt moves its
// directory under .dolt_dropped_databases/ so `dolt_undrop()` can
// restore it, but leaves the disk footprint in place until an
// explicit purge (be-pq5).
fmt.Println()
if shouldPurgeDroppedDatabases(purgeDropped, dropped) {
if err := purgeDroppedDatabases(context.Background(), db); err != nil {
fmt.Fprintf(os.Stderr, " WARN: PURGE_DROPPED_DATABASES failed: %v\n", err)
fmt.Fprintln(os.Stderr, " Try `dolt sql -q 'CALL DOLT_PURGE_DROPPED_DATABASES()'`.")
} else {
fmt.Println("Purged all dropped databases on this server (server-global, irreversible —")
fmt.Println("CALL DOLT_UNDROP is no longer available for any of them, not just this run's).")
}
} else {
fmt.Println("Dropped databases remain recoverable via `CALL DOLT_UNDROP(name)` until purged.")
fmt.Println("Pass --purge-dropped to permanently reclaim their disk. This purges ALL dropped")
fmt.Println("databases on the server (server-global), not just the ones from this run.")
}
return nil
return cleanDatabases(rootCtx, db, opts)
},
}
@ -1192,10 +1076,10 @@ func shouldPurgeDroppedDatabases(purgeDropped bool, droppedCount int) bool {
// against a live test server without going through the full
// clean-databases command wiring (config loading, SHOW DATABASES scan,
// batching/backoff).
func purgeDroppedDatabases(ctx context.Context, db *sql.DB) error {
func purgeDroppedDatabases(ctx context.Context, conn versioncontrolops.DBConn) error {
purgeCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
_, err := db.ExecContext(purgeCtx, "CALL DOLT_PURGE_DROPPED_DATABASES()")
_, err := conn.ExecContext(purgeCtx, "CALL DOLT_PURGE_DROPPED_DATABASES()")
return err
}

View file

@ -0,0 +1,152 @@
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/steveyegge/beads/internal/storage/versioncontrolops"
)
type cleanDatabasesOptions struct {
dryRun bool
purgeDropped bool
}
const (
cleanDatabasesListTimeout = 30 * time.Second
cleanDatabasesDropTimeout = 30 * time.Second
cleanDatabasesBatchSize = 5
cleanDatabasesBatchPause = 2 * time.Second
cleanDatabasesBackoffPause = 10 * time.Second
cleanDatabasesTimeoutTrip = 3
cleanDatabasesMaxFailures = 10
)
func listStaleDatabases(ctx context.Context, conn versioncontrolops.DBConn) ([]string, error) {
listCtx, cancel := context.WithTimeout(ctx, cleanDatabasesListTimeout)
defer cancel()
rows, err := conn.QueryContext(listCtx, "SHOW DATABASES")
if err != nil {
return nil, err
}
defer rows.Close()
var stale []string
for rows.Next() {
var dbName string
if err := rows.Scan(&dbName); err != nil {
return nil, err
}
for _, prefix := range staleDatabasePrefixes {
if strings.HasPrefix(dbName, prefix) {
stale = append(stale, dbName)
break
}
}
}
if err := rows.Err(); err != nil {
return nil, err
}
return stale, rows.Close()
}
func cleanDatabases(ctx context.Context, conn versioncontrolops.DBConn, opts cleanDatabasesOptions) error {
stale, err := listStaleDatabases(ctx, conn)
if err != nil {
return HandleError("listing databases: %v", err)
}
if len(stale) == 0 {
fmt.Println("No stale databases found.")
} else {
fmt.Printf("Found %d stale databases:\n", len(stale))
for _, name := range stale {
fmt.Printf(" %s\n", name)
}
}
if opts.dryRun {
if len(stale) > 0 {
fmt.Println("\n(dry run — no databases dropped)")
}
if opts.purgeDropped {
fmt.Println("(dry run — --purge-dropped ignored; no purge performed)")
}
return nil
}
dropped, err := dropStaleDatabases(ctx, conn, stale)
if err != nil {
return err
}
fmt.Println()
if shouldPurgeDroppedDatabases(opts.purgeDropped, dropped) {
if err := purgeDroppedDatabases(ctx, conn); err != nil {
fmt.Fprintf(os.Stderr, " WARN: PURGE_DROPPED_DATABASES failed: %v\n", err)
fmt.Fprintln(os.Stderr, " Try `dolt sql -q 'CALL DOLT_PURGE_DROPPED_DATABASES()'`.")
} else {
fmt.Println("Purged all dropped databases on this server (server-global, irreversible —")
fmt.Println("CALL DOLT_UNDROP is no longer available for any of them, not just this run's).")
}
} else {
fmt.Println("Dropped databases remain recoverable via `CALL DOLT_UNDROP(name)` until purged.")
fmt.Println("Pass --purge-dropped to permanently reclaim their disk. This purges ALL dropped")
fmt.Println("databases on the server (server-global), not just the ones from this run.")
}
return nil
}
func dropStaleDatabases(ctx context.Context, conn versioncontrolops.DBConn, stale []string) (int, error) {
if len(stale) == 0 {
return 0, nil
}
fmt.Println()
dropped := 0
failures := 0
consecutiveTimeouts := 0
for i, name := range stale {
if consecutiveTimeouts >= cleanDatabasesTimeoutTrip {
fmt.Fprintf(os.Stderr, " ⚠ %d consecutive timeouts — backing off %s\n",
consecutiveTimeouts, cleanDatabasesBackoffPause)
time.Sleep(cleanDatabasesBackoffPause)
consecutiveTimeouts = 0
}
if failures >= cleanDatabasesMaxFailures {
fmt.Fprintf(os.Stderr, "\n✗ Aborting: %d consecutive failures suggest server is unhealthy.\n", failures)
fmt.Fprintf(os.Stderr, " Dropped %d/%d before stopping.\n", dropped, len(stale))
return dropped, SilentExit()
}
dropCtx, dropCancel := context.WithTimeout(ctx, cleanDatabasesDropTimeout)
safeName := strings.ReplaceAll(name, "`", "``")
_, err := conn.ExecContext(dropCtx, fmt.Sprintf("DROP DATABASE `%s`", safeName)) //nolint:gosec // G201: identifier-escaped
dropCancel()
if err != nil {
fmt.Fprintf(os.Stderr, " FAIL: %s: %v\n", name, err)
failures++
if isTimeoutError(err) {
consecutiveTimeouts++
}
} else {
fmt.Printf(" Dropped: %s\n", name)
dropped++
failures = 0
consecutiveTimeouts = 0
}
if (i+1)%cleanDatabasesBatchSize == 0 && i+1 < len(stale) {
fmt.Printf(" [%d/%d] pausing %s...\n", i+1, len(stale), cleanDatabasesBatchPause)
time.Sleep(cleanDatabasesBatchPause)
}
}
fmt.Printf("\nDropped %d/%d stale databases.\n", dropped, len(stale))
return dropped, nil
}

View file

@ -3,15 +3,11 @@ package main
import (
"context"
"database/sql"
"fmt"
"os"
"strings"
"time"
"github.com/steveyegge/beads/internal/storage/uow"
)
func runDoltCleanDatabasesProxied(ctx context.Context, beadsDir string, dryRun bool) error {
func runDoltCleanDatabasesProxied(ctx context.Context, beadsDir string, opts cleanDatabasesOptions) error {
provider, err := newProxiedServerUOWProvider(ctx, beadsDir)
if err != nil {
return HandleError("failed to open uow provider: %v", err)
@ -24,62 +20,6 @@ func runDoltCleanDatabasesProxied(ctx context.Context, beadsDir string, dryRun b
}
return mp.RunNonTx(ctx, func(ctx context.Context, conn *sql.Conn) error {
listCtx, listCancel := context.WithTimeout(ctx, 30*time.Second)
defer listCancel()
rows, err := conn.QueryContext(listCtx, "SHOW DATABASES")
if err != nil {
return HandleError("listing databases: %v", err)
}
defer rows.Close()
var stale []string
for rows.Next() {
var dbName string
if err := rows.Scan(&dbName); err != nil {
return HandleError("scanning database name: %v", err)
}
for _, prefix := range staleDatabasePrefixes {
if strings.HasPrefix(dbName, prefix) {
stale = append(stale, dbName)
break
}
}
}
if err := rows.Err(); err != nil {
return HandleError("listing databases: %v", err)
}
if len(stale) == 0 {
fmt.Println("No stale databases found.")
return nil
}
fmt.Printf("Found %d stale databases:\n", len(stale))
for _, name := range stale {
fmt.Printf(" %s\n", name)
}
if dryRun {
fmt.Println("\n(dry run — no databases dropped)")
return nil
}
fmt.Println()
dropped := 0
for _, name := range stale {
dropCtx, dropCancel := context.WithTimeout(ctx, 30*time.Second)
safeName := strings.ReplaceAll(name, "`", "``")
_, err := conn.ExecContext(dropCtx, fmt.Sprintf("DROP DATABASE `%s`", safeName)) //nolint:gosec // G201: identifier-escaped
dropCancel()
if err != nil {
fmt.Fprintf(os.Stderr, " FAIL: %s: %v\n", name, err)
} else {
fmt.Printf(" Dropped: %s\n", name)
dropped++
}
}
fmt.Printf("\nDropped %d/%d stale databases.\n", dropped, len(stale))
return nil
return cleanDatabases(ctx, conn, opts)
})
}

View file

@ -0,0 +1,128 @@
//go:build cgo
package main
import (
"fmt"
"strings"
"testing"
)
func TestProxiedServerCleanDatabasesPurgeDropped(t *testing.T) {
requireProxiedServerEnv(t)
bd := buildEmbeddedBD(t)
p := bdProxiedInit(t, bd, "purge")
createDatabase := func(t *testing.T, name string) {
t.Helper()
bdProxiedSQL(t, bd, p.dir, "CREATE DATABASE `"+name+"`")
}
dropDatabase := func(t *testing.T, name string) {
t.Helper()
bdProxiedSQL(t, bd, p.dir, "DROP DATABASE `"+name+"`")
}
databaseExists := func(t *testing.T, name string) bool {
t.Helper()
rows := bdProxiedSQLJSON(t, bd, p.dir,
"SELECT COUNT(*) as count FROM information_schema.schemata WHERE schema_name = '"+name+"'")
return len(rows) == 1 && sqlValueEquals(rows[0]["count"], 1)
}
undropSucceeds := func(t *testing.T, name string) bool {
t.Helper()
_, _, err := bdProxiedRunBuffers(t, bd, p.dir, "sql", fmt.Sprintf("CALL DOLT_UNDROP('%s')", name))
return err == nil
}
cleanDatabasesRun := func(t *testing.T, args ...string) string {
t.Helper()
full := append([]string{"dolt", "clean-databases"}, args...)
stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, full...)
if err != nil {
t.Fatalf("bd dolt clean-databases %s failed: %v\nstdout:\n%s\nstderr:\n%s",
strings.Join(args, " "), err, stdout, stderr)
}
return stdout
}
const (
dryDB = "testdb_purge_dry"
controlDB = "testdb_purge_control"
purgedDB = "testdb_purge_target"
residueDB = "testdb_purge_residue"
purgedText = "Purged all dropped databases"
)
t.Run("dry_run_ignores_purge", func(t *testing.T) {
createDatabase(t, dryDB)
stdout := cleanDatabasesRun(t, "--dry-run", "--purge-dropped")
if !strings.Contains(stdout, "--purge-dropped ignored") {
t.Errorf("expected dry-run to report --purge-dropped ignored, got:\n%s", stdout)
}
if strings.Contains(stdout, purgedText) {
t.Errorf("dry-run must not purge, got:\n%s", stdout)
}
if !databaseExists(t, dryDB) {
t.Errorf("dry-run must not drop %s", dryDB)
}
dropDatabase(t, dryDB)
})
t.Run("without_flag_keeps_undrop_recovery", func(t *testing.T) {
createDatabase(t, controlDB)
stdout := cleanDatabasesRun(t)
if !strings.Contains(stdout, "Dropped: "+controlDB) {
t.Fatalf("expected %s to be dropped, got:\n%s", controlDB, stdout)
}
if !strings.Contains(stdout, "remain recoverable") {
t.Errorf("expected the DOLT_UNDROP recoverability trailer, got:\n%s", stdout)
}
if strings.Contains(stdout, purgedText) {
t.Errorf("no --purge-dropped was passed, but output claims a purge:\n%s", stdout)
}
if !undropSucceeds(t, controlDB) {
t.Errorf("DOLT_UNDROP(%q) failed without --purge-dropped; the drop should still be recoverable", controlDB)
}
dropDatabase(t, controlDB)
})
t.Run("purge_removes_undrop_recovery", func(t *testing.T) {
createDatabase(t, purgedDB)
stdout := cleanDatabasesRun(t, "--purge-dropped")
if !strings.Contains(stdout, "Dropped: "+purgedDB) {
t.Fatalf("expected %s to be dropped, got:\n%s", purgedDB, stdout)
}
if !strings.Contains(stdout, purgedText) {
t.Fatalf("expected a purge confirmation, got:\n%s", stdout)
}
if undropSucceeds(t, purgedDB) {
t.Errorf("DOLT_UNDROP(%q) succeeded after --purge-dropped; the database was not purged", purgedDB)
}
if undropSucceeds(t, controlDB) {
t.Errorf("DOLT_UNDROP(%q) succeeded; the purge should be server-global and take prior residue with it", controlDB)
}
})
t.Run("zero_stale_still_purges_residue", func(t *testing.T) {
createDatabase(t, residueDB)
dropDatabase(t, residueDB)
stdout := cleanDatabasesRun(t, "--purge-dropped")
if !strings.Contains(stdout, "No stale databases found") {
t.Fatalf("expected no stale databases on this run, got:\n%s", stdout)
}
if !strings.Contains(stdout, purgedText) {
t.Fatalf("expected --purge-dropped to purge residue even with nothing stale, got:\n%s", stdout)
}
if undropSucceeds(t, residueDB) {
t.Errorf("DOLT_UNDROP(%q) succeeded; residue was not purged", residueDB)
}
})
}