PersistentPreRunE on adminCmd fires before main.go's PersistentPreRun sets serverMode/cmdCtx.ServerMode. Cobra runs child PersistentPreRunE before parent PersistentPreRun, so isEmbeddedMode() sees cmdCtx.ServerMode as false (zero value) even when metadata.json declares dolt_mode=server. Fix: remove PersistentPreRunE from adminCmd. Add requireServerMode() helper and call it at the top of each subcommand's Run func, after store init has populated cmdCtx.ServerMode correctly. Fixes: bd admin compact, bd admin cleanup, bd admin reset all returning 'not yet supported in embedded mode' for server-mode projects.
179 lines
5.4 KiB
Go
179 lines
5.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/internal/ui"
|
|
)
|
|
|
|
// CleanupEmptyResponse is returned when there are no closed issues to delete
|
|
type CleanupEmptyResponse struct {
|
|
DeletedCount int `json:"deleted_count"`
|
|
Message string `json:"message"`
|
|
Filter string `json:"filter,omitempty"`
|
|
Ephemeral bool `json:"ephemeral,omitempty"`
|
|
}
|
|
|
|
var cleanupCmd = &cobra.Command{
|
|
Use: "cleanup",
|
|
Short: "Delete closed issues to reduce database size",
|
|
Long: `Delete closed issues to reduce database size.
|
|
|
|
This command permanently removes closed issues from the database.
|
|
|
|
NOTE: This command only manages issue lifecycle (closed -> deleted). For general
|
|
health checks and automatic repairs, use 'bd doctor --fix' instead.
|
|
|
|
By default, deletes ALL closed issues. Use --older-than to only delete
|
|
issues closed before a certain date.
|
|
|
|
EXAMPLES:
|
|
bd admin cleanup --force # Delete all closed issues
|
|
bd admin cleanup --older-than 30 --force # Only issues closed 30+ days ago
|
|
bd admin cleanup --ephemeral --force # Only closed wisps (transient molecules)
|
|
bd admin cleanup --dry-run # Preview what would be deleted
|
|
|
|
SAFETY:
|
|
- Requires --force flag to actually delete (unless --dry-run)
|
|
- Supports --cascade to delete dependents
|
|
- Shows preview of what will be deleted
|
|
- Use --json for programmatic output
|
|
|
|
SEE ALSO:
|
|
bd doctor --fix Automatic health checks and repairs (recommended for routine maintenance)
|
|
bd admin compact Compact old closed issues to save space`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
if err := requireServerMode("cleanup"); err != nil {
|
|
FatalError("%v", err)
|
|
}
|
|
force, _ := cmd.Flags().GetBool("force")
|
|
dryRun, _ := cmd.Flags().GetBool("dry-run")
|
|
cascade, _ := cmd.Flags().GetBool("cascade")
|
|
olderThanDays, _ := cmd.Flags().GetInt("older-than")
|
|
wispOnly, _ := cmd.Flags().GetBool("ephemeral")
|
|
|
|
// Ensure we have storage
|
|
if store == nil {
|
|
if err := ensureStoreActive(); err != nil {
|
|
FatalError("%v", err)
|
|
}
|
|
}
|
|
|
|
ctx := rootCtx
|
|
|
|
// Build filter for closed issues
|
|
statusClosed := types.StatusClosed
|
|
filter := types.IssueFilter{
|
|
Status: &statusClosed,
|
|
}
|
|
|
|
// Add age filter if specified
|
|
if olderThanDays > 0 {
|
|
cutoffTime := time.Now().AddDate(0, 0, -olderThanDays)
|
|
filter.ClosedBefore = &cutoffTime
|
|
}
|
|
|
|
// Add wisp filter if specified (bd-kwro.9)
|
|
if wispOnly {
|
|
wispTrue := true
|
|
filter.Ephemeral = &wispTrue
|
|
}
|
|
|
|
// Get all closed issues matching filter
|
|
closedIssues, err := store.SearchIssues(ctx, "", filter)
|
|
if err != nil {
|
|
FatalError("listing issues: %v", err)
|
|
}
|
|
|
|
// Filter out pinned issues - they are protected from cleanup (bd-b2k)
|
|
pinnedCount := 0
|
|
filteredIssues := make([]*types.Issue, 0, len(closedIssues))
|
|
for _, issue := range closedIssues {
|
|
if issue.Pinned {
|
|
pinnedCount++
|
|
continue
|
|
}
|
|
filteredIssues = append(filteredIssues, issue)
|
|
}
|
|
closedIssues = filteredIssues
|
|
|
|
if pinnedCount > 0 && !jsonOutput {
|
|
fmt.Printf("Skipping %d pinned issue(s) (protected from cleanup)\n", pinnedCount)
|
|
}
|
|
|
|
if len(closedIssues) == 0 {
|
|
if jsonOutput {
|
|
result := CleanupEmptyResponse{
|
|
DeletedCount: 0,
|
|
Message: "No closed issues to delete",
|
|
}
|
|
if olderThanDays > 0 {
|
|
result.Filter = fmt.Sprintf("older than %d days", olderThanDays)
|
|
}
|
|
if wispOnly {
|
|
result.Ephemeral = true
|
|
}
|
|
outputJSON(result)
|
|
} else {
|
|
msg := "No closed issues to delete"
|
|
if wispOnly && olderThanDays > 0 {
|
|
msg = fmt.Sprintf("No closed wisps older than %d days to delete", olderThanDays)
|
|
} else if wispOnly {
|
|
msg = "No closed wisps to delete"
|
|
} else if olderThanDays > 0 {
|
|
msg = fmt.Sprintf("No closed issues older than %d days to delete", olderThanDays)
|
|
}
|
|
fmt.Println(msg)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Extract IDs
|
|
issueIDs := make([]string, len(closedIssues))
|
|
for i, issue := range closedIssues {
|
|
issueIDs[i] = issue.ID
|
|
}
|
|
|
|
// Show preview
|
|
if !force && !dryRun {
|
|
issueType := "closed"
|
|
if wispOnly {
|
|
issueType = "closed wisp"
|
|
}
|
|
FatalErrorWithHint(
|
|
fmt.Sprintf("would delete %d %s issue(s)", len(issueIDs), issueType),
|
|
"Use --force to confirm or --dry-run to preview.")
|
|
}
|
|
|
|
if !jsonOutput {
|
|
issueType := "closed"
|
|
if wispOnly {
|
|
issueType = "closed wisp"
|
|
}
|
|
if olderThanDays > 0 {
|
|
fmt.Printf("Found %d %s issue(s) older than %d days\n", len(closedIssues), issueType, olderThanDays)
|
|
} else {
|
|
fmt.Printf("Found %d %s issue(s)\n", len(closedIssues), issueType)
|
|
}
|
|
if dryRun {
|
|
fmt.Println(ui.RenderWarn("DRY RUN - no changes will be made"))
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
// Use the existing batch deletion logic
|
|
deleteBatch(cmd, issueIDs, force, dryRun, cascade, jsonOutput, false, "cleanup")
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
cleanupCmd.Flags().BoolP("force", "f", false, "Actually delete (without this flag, shows error)")
|
|
cleanupCmd.Flags().Bool("dry-run", false, "Preview what would be deleted without making changes")
|
|
cleanupCmd.Flags().Bool("cascade", false, "Recursively delete all dependent issues")
|
|
cleanupCmd.Flags().Int("older-than", 0, "Only delete issues closed more than N days ago (0 = all closed issues)")
|
|
cleanupCmd.Flags().Bool("ephemeral", false, "Only delete closed wisps (transient molecules)")
|
|
// Note: cleanupCmd is added to adminCmd in admin.go
|
|
}
|