Skip to content

Commit

Permalink
fix prune when based on counting
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <avi@deitcher.net>
  • Loading branch information
deitch committed Nov 21, 2024
1 parent cb647be commit 05e1054
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/core/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func pruneTarget(ctx context.Context, logger *logrus.Entry, target storage.Stora
return 0
})
slices.Reverse(filesWithTimes)
if retainCount >= len(filesWithTimes) {
if retainCount < len(filesWithTimes) {
for i := 0 + retainCount; i < len(filesWithTimes); i++ {
logger.Debugf("Adding candidate file %s:", filesWithTimes[i].filename)
candidates = append(candidates, filesWithTimes[i].filename)
Expand Down
8 changes: 6 additions & 2 deletions pkg/core/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
import (
"context"
"fmt"
"io"
"os"
"slices"
"testing"
Expand Down Expand Up @@ -69,6 +68,7 @@ func TestPrune(t *testing.T) {
safefilename := fmt.Sprintf("db_backup_%sZ.gz", relativeTime.Format("2006-01-02T15-04-05"))
safefilenames = append(safefilenames, safefilename)
}
t.Log(filenames)
tests := []struct {
name string
opts PruneOptions
Expand All @@ -87,6 +87,9 @@ func TestPrune(t *testing.T) {
{"2 days", PruneOptions{Retention: "2d", Now: now}, filenames, filenames[0:6], nil},
// 3 weeks - file[13] is 504h+30m = 504.5h, so it should be pruned
{"3 weeks", PruneOptions{Retention: "3w", Now: now}, filenames, filenames[0:13], nil},
// 2 most recent files
{"2 most recent", PruneOptions{Retention: "2c", Now: now}, filenames, filenames[0:2], nil},

// repeat for safe file names
{"1 hour safe names", PruneOptions{Retention: "1h", Now: now}, safefilenames, safefilenames[0:1], nil},
// 2 hours - file[2] is 2h+30m = 2.5h, so it should be pruned
Expand Down Expand Up @@ -123,7 +126,8 @@ func TestPrune(t *testing.T) {

// run Prune
logger := log.New()
logger.Out = io.Discard
//logger.Out = io.Discard
logger.SetLevel(log.DebugLevel)
executor := Executor{
Logger: logger,
}
Expand Down

0 comments on commit 05e1054

Please sign in to comment.