1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-26 18:21:01 +00:00

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
This commit is contained in:
Frédéric Guillot 2025-08-16 21:12:36 -07:00
parent 5c26e06780
commit a16ac51326
2 changed files with 34 additions and 18 deletions

View file

@ -47,10 +47,17 @@ func runCleanupTasks(store *storage.Storage) {
} }
} }
if rowsAffected, err := store.DeleteContentRemovedEntries(); err != nil { if enclosuresAffected, err := store.DeleteRemovedEntriesEnclosures(); err != nil {
slog.Error("Unable to delete the content of removed entries", slog.Any("error", err)) slog.Error("Unable to delete enclosures from removed entries", slog.Any("error", err))
} else { } else {
slog.Info("Deleting content of removed entries completed", slog.Info("Deleting enclosures from removed entries completed",
slog.Int64("removed_entries_content_removed", rowsAffected)) 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))
} }
} }

View file

@ -258,8 +258,8 @@ func (s *Storage) GetReadTime(feedID int64, entryHash string) int {
return result return result
} }
// deleteRemovedNonexistentEntries deletes from the database entries marked as "removed" and not visible anymore in the feed. // cleanupRemovedEntriesNotInFeed deletes from the database entries marked as "removed" and not visible anymore in the feed.
func (s *Storage) deleteRemovedNonexistentEntries(feedID int64, entryHashes []string) error { func (s *Storage) cleanupRemovedEntriesNotInFeed(feedID int64, entryHashes []string) error {
query := ` query := `
DELETE FROM DELETE FROM
entries entries
@ -275,21 +275,30 @@ func (s *Storage) deleteRemovedNonexistentEntries(feedID int64, entryHashes []st
return nil return nil
} }
// deleteContentRemovedEntries deletes the content and corresponding enclosures // DeleteRemovedEntriesEnclosures deletes enclosures associated with entries marked as "removed".
// of entries marked as "removed", and only keeps their metadata. func (s *Storage) DeleteRemovedEntriesEnclosures() (int64, error) {
func (s *Storage) DeleteContentRemovedEntries() (int64, error) {
query := ` query := `
DELETE FROM DELETE FROM
enclosures enclosures
WHERE WHERE
enclosures.entry_id IN enclosures.entry_id IN (SELECT id FROM entries WHERE status=$1)
(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) 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 UPDATE
entries entries
SET SET
@ -298,17 +307,17 @@ func (s *Storage) DeleteContentRemovedEntries() (int64, error) {
url='', url='',
author=NULL author=NULL
WHERE WHERE
status=$1 status=$1 AND content IS NOT NULL
` `
result, err := s.db.Exec(query, model.EntryStatusRemoved) result, err := s.db.Exec(query, model.EntryStatusRemoved)
if err != nil { 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() count, err := result.RowsAffected()
if err != nil { 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 return count, nil
@ -361,8 +370,8 @@ func (s *Storage) RefreshFeedEntries(userID, feedID int64, entries model.Entries
} }
go func() { go func() {
if err := s.deleteRemovedNonexistentEntries(feedID, entryHashes); err != nil { if err := s.cleanupRemovedEntriesNotInFeed(feedID, entryHashes); err != nil {
slog.Error("Unable to cleanup entries", slog.Error("Unable to cleanup removed entries",
slog.Int64("user_id", userID), slog.Int64("user_id", userID),
slog.Int64("feed_id", feedID), slog.Int64("feed_id", feedID),
slog.Any("error", err), slog.Any("error", err),