fix: do not store empty tags

This commit is contained in:
Romain de Laage 2024-03-24 07:31:54 +01:00 committed by Frédéric Guillot
parent 93c9d43497
commit b54fe66809
2 changed files with 19 additions and 2 deletions

View file

@ -876,4 +876,10 @@ var migrations = []func(tx *sql.Tx) error{
_, err = tx.Exec(sql) _, err = tx.Exec(sql)
return err return err
}, },
func(tx *sql.Tx) (err error) {
// the WHERE part speed-up the request a lot
sql := `UPDATE entries SET tags = array_remove(tags, '') WHERE '' = ANY(tags);`
_, err = tx.Exec(sql)
return err
},
} }

View file

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"log/slog" "log/slog"
"slices" "slices"
"strings"
"time" "time"
"miniflux.app/v2/internal/crypto" "miniflux.app/v2/internal/crypto"
@ -139,7 +140,7 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
entry.UserID, entry.UserID,
entry.FeedID, entry.FeedID,
entry.ReadingTime, entry.ReadingTime,
pq.Array(removeDuplicates(entry.Tags)), pq.Array(removeEmpty(removeDuplicates(entry.Tags))),
).Scan( ).Scan(
&entry.ID, &entry.ID,
&entry.Status, &entry.Status,
@ -195,7 +196,7 @@ func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
entry.UserID, entry.UserID,
entry.FeedID, entry.FeedID,
entry.Hash, entry.Hash,
pq.Array(removeDuplicates(entry.Tags)), pq.Array(removeEmpty(removeDuplicates(entry.Tags))),
).Scan(&entry.ID) ).Scan(&entry.ID)
if err != nil { if err != nil {
@ -620,3 +621,13 @@ func removeDuplicates(l []string) []string {
slices.Sort(l) slices.Sort(l)
return slices.Compact(l) return slices.Compact(l)
} }
func removeEmpty(l []string) []string {
var finalSlice []string
for _, item := range l {
if strings.TrimSpace(item) != "" {
finalSlice = append(finalSlice, item)
}
}
return finalSlice
}