From a16ac51326fef95d471c42d22366e3c4551df495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sat, 16 Aug 2025 21:12:36 -0700 Subject: [PATCH] refactor(storage): split `DeleteContentRemovedEntries` into 2 functions and fix condition - Add `content IS NOT NULL` to avoid clearing the same entries over and over - Create `DeleteRemovedEntriesEnclosures` and `ClearRemovedEntriesContent` that report individual counts --- internal/cli/cleanup_tasks.go | 15 ++++++++++---- internal/storage/entry.go | 37 ++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/internal/cli/cleanup_tasks.go b/internal/cli/cleanup_tasks.go index ef071e3e..224e708e 100644 --- a/internal/cli/cleanup_tasks.go +++ b/internal/cli/cleanup_tasks.go @@ -47,10 +47,17 @@ func runCleanupTasks(store *storage.Storage) { } } - if rowsAffected, err := store.DeleteContentRemovedEntries(); err != nil { - slog.Error("Unable to delete the content of removed entries", slog.Any("error", err)) + if enclosuresAffected, err := store.DeleteRemovedEntriesEnclosures(); err != nil { + slog.Error("Unable to delete enclosures from removed entries", slog.Any("error", err)) } else { - slog.Info("Deleting content of removed entries completed", - slog.Int64("removed_entries_content_removed", rowsAffected)) + slog.Info("Deleting enclosures from removed entries completed", + slog.Int64("removed_entries_enclosures_deleted", enclosuresAffected)) + } + + if contentAffected, err := store.ClearRemovedEntriesContent(); err != nil { + slog.Error("Unable to clear content from removed entries", slog.Any("error", err)) + } else { + slog.Info("Clearing content from removed entries completed", + slog.Int64("removed_entries_content_cleared", contentAffected)) } } diff --git a/internal/storage/entry.go b/internal/storage/entry.go index 2b3c5dd1..09a32083 100644 --- a/internal/storage/entry.go +++ b/internal/storage/entry.go @@ -258,8 +258,8 @@ func (s *Storage) GetReadTime(feedID int64, entryHash string) int { return result } -// deleteRemovedNonexistentEntries deletes from the database entries marked as "removed" and not visible anymore in the feed. -func (s *Storage) deleteRemovedNonexistentEntries(feedID int64, entryHashes []string) error { +// cleanupRemovedEntriesNotInFeed deletes from the database entries marked as "removed" and not visible anymore in the feed. +func (s *Storage) cleanupRemovedEntriesNotInFeed(feedID int64, entryHashes []string) error { query := ` DELETE FROM entries @@ -275,21 +275,30 @@ func (s *Storage) deleteRemovedNonexistentEntries(feedID int64, entryHashes []st return nil } -// deleteContentRemovedEntries deletes the content and corresponding enclosures -// of entries marked as "removed", and only keeps their metadata. -func (s *Storage) DeleteContentRemovedEntries() (int64, error) { +// DeleteRemovedEntriesEnclosures deletes enclosures associated with entries marked as "removed". +func (s *Storage) DeleteRemovedEntriesEnclosures() (int64, error) { query := ` DELETE FROM enclosures WHERE - enclosures.entry_id IN - (SELECT id FROM entries WHERE status=$1) + enclosures.entry_id IN (SELECT id FROM entries WHERE status=$1) ` - if _, err := s.db.Exec(query, model.EntryStatusRemoved); err != nil { + result, err := s.db.Exec(query, model.EntryStatusRemoved) + if err != nil { return 0, fmt.Errorf(`store: unable to delete enclosures from removed entries: %v`, err) } - query = ` + count, err := result.RowsAffected() + if err != nil { + return 0, fmt.Errorf(`store: unable to get the number of rows affected while deleting enclosures from removed entries: %v`, err) + } + + return count, nil +} + +// ClearRemovedEntriesContent clears the content fields of entries marked as "removed", keeping only their metadata. +func (s *Storage) ClearRemovedEntriesContent() (int64, error) { + query := ` UPDATE entries SET @@ -298,17 +307,17 @@ func (s *Storage) DeleteContentRemovedEntries() (int64, error) { url='', author=NULL WHERE - status=$1 + status=$1 AND content IS NOT NULL ` result, err := s.db.Exec(query, model.EntryStatusRemoved) if err != nil { - return 0, fmt.Errorf(`store: unable to delete removed entries: %v`, err) + return 0, fmt.Errorf(`store: unable to clear content from removed entries: %v`, err) } count, err := result.RowsAffected() if err != nil { - return 0, fmt.Errorf(`store: unable to get the number of rows affected while deleting content from removed entries: %v`, err) + return 0, fmt.Errorf(`store: unable to get the number of rows affected while clearing content from removed entries: %v`, err) } return count, nil @@ -361,8 +370,8 @@ func (s *Storage) RefreshFeedEntries(userID, feedID int64, entries model.Entries } go func() { - if err := s.deleteRemovedNonexistentEntries(feedID, entryHashes); err != nil { - slog.Error("Unable to cleanup entries", + if err := s.cleanupRemovedEntriesNotInFeed(feedID, entryHashes); err != nil { + slog.Error("Unable to cleanup removed entries", slog.Int64("user_id", userID), slog.Int64("feed_id", feedID), slog.Any("error", err),