diff --git a/internal/reconcile/reconcile.go b/internal/reconcile/reconcile.go index eb2b0911..80a91755 100644 --- a/internal/reconcile/reconcile.go +++ b/internal/reconcile/reconcile.go @@ -227,6 +227,7 @@ func (r *Reconciler) handleMissingObject( ) error { report.MetadataScanned++ if metadata.Status == model.AttachmentDeleting { + report.DeletingAttachments++ return r.deleteClaimedMetadata(ctx, metadata, report) } if metadata.CreatedAt.After(cutoff) { @@ -252,6 +253,9 @@ func (r *Reconciler) handleMissingObject( } func (r *Reconciler) deleteClaimedMetadata(ctx context.Context, metadata model.Attachment, report *Report) error { + if !r.config.Apply { + return nil + } deleted, err := r.metadata.DeleteAttachmentIfDeleting(ctx, metadata.ID) if err != nil { return fmt.Errorf("delete missing attachment metadata %q: %w", metadata.ID, err) diff --git a/internal/reconcile/reconcile_test.go b/internal/reconcile/reconcile_test.go index 62471a65..5b43b904 100644 --- a/internal/reconcile/reconcile_test.go +++ b/internal/reconcile/reconcile_test.go @@ -75,6 +75,54 @@ func TestReconcilerRechecksOrphanBeforeDelete(t *testing.T) { require.Contains(t, objects.items, "attachments/a/original") } +func TestReconcilerDeletingAttachmentRespectsApplyMode(t *testing.T) { + now := time.Now().UTC() + key := "attachments/a/original" + tests := []struct { + name string + apply bool + objectPresent bool + }{ + {name: "dry run with object", objectPresent: true}, + {name: "dry run without object"}, + {name: "apply with object", apply: true, objectPresent: true}, + {name: "apply without object", apply: true}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + metadata := &metadataStub{items: map[string]model.Attachment{ + key: attachment("a", key, model.AttachmentDeleting, now.Add(-time.Hour), false), + }} + objects := &objectStub{items: make(map[string]model.StoredObject)} + if test.objectPresent { + objects.items[key] = model.StoredObject{Key: key, LastModified: now.Add(-time.Hour)} + } + reconciler, err := New(metadata, objects, Config{ + Prefix: "attachments/", PageSize: 1, GraceTime: time.Minute, + UnreferencedRetention: time.Hour, Apply: test.apply, Now: func() time.Time { return now }, + }) + require.NoError(t, err) + + report, err := reconciler.Run(context.Background()) + require.NoError(t, err) + require.Equal(t, 1, report.DeletingAttachments) + var expectedDeletedRows, expectedDeletedObjects int + if test.apply { + expectedDeletedRows = 1 + } + if test.apply && test.objectPresent { + expectedDeletedObjects = 1 + } + require.Equal(t, expectedDeletedRows, report.DeletedUnreferencedRows) + require.Equal(t, expectedDeletedObjects, report.DeletedAttachmentObjects) + _, metadataExists := metadata.items[key] + require.Equal(t, !test.apply, metadataExists) + _, objectExists := objects.items[key] + require.Equal(t, test.objectPresent && !test.apply, objectExists) + }) + } +} + func TestNewRejectsInvalidRetention(t *testing.T) { _, err := New(&metadataStub{}, &objectStub{}, Config{PageSize: 1}) require.Error(t, err)