Skip to content

Commit 8311e55

Browse files
committed
fix prune when based on counting
Signed-off-by: Avi Deitcher <avi@deitcher.net>
1 parent cb647be commit 8311e55

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pkg/core/prune.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func pruneTarget(ctx context.Context, logger *logrus.Entry, target storage.Stora
122122
return 0
123123
})
124124
slices.Reverse(filesWithTimes)
125-
if retainCount >= len(filesWithTimes) {
125+
if retainCount < len(filesWithTimes) {
126126
for i := 0 + retainCount; i < len(filesWithTimes); i++ {
127127
logger.Debugf("Adding candidate file %s:", filesWithTimes[i].filename)
128128
candidates = append(candidates, filesWithTimes[i].filename)

pkg/core/prune_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func TestPrune(t *testing.T) {
6969
safefilename := fmt.Sprintf("db_backup_%sZ.gz", relativeTime.Format("2006-01-02T15-04-05"))
7070
safefilenames = append(safefilenames, safefilename)
7171
}
72+
t.Log(filenames)
73+
t.Log(filenames[0:2])
7274
tests := []struct {
7375
name string
7476
opts PruneOptions
@@ -87,6 +89,9 @@ func TestPrune(t *testing.T) {
8789
{"2 days", PruneOptions{Retention: "2d", Now: now}, filenames, filenames[0:6], nil},
8890
// 3 weeks - file[13] is 504h+30m = 504.5h, so it should be pruned
8991
{"3 weeks", PruneOptions{Retention: "3w", Now: now}, filenames, filenames[0:13], nil},
92+
// 2 most recent files
93+
{"2 most recent", PruneOptions{Retention: "2c", Now: now}, filenames, filenames[0:2], nil},
94+
9095
// repeat for safe file names
9196
{"1 hour safe names", PruneOptions{Retention: "1h", Now: now}, safefilenames, safefilenames[0:1], nil},
9297
// 2 hours - file[2] is 2h+30m = 2.5h, so it should be pruned
@@ -148,6 +153,8 @@ func TestPrune(t *testing.T) {
148153
}
149154
slices.Sort(afterFiles)
150155
slices.Sort(tt.afterFiles)
156+
t.Logf("expected files: %v", tt.afterFiles)
157+
t.Logf("after files: %v", afterFiles)
151158
assert.ElementsMatch(t, tt.afterFiles, afterFiles)
152159
})
153160
}

0 commit comments

Comments
 (0)