Skip to content

Commit

Permalink
fix: do not store empty tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rdelaage committed Mar 23, 2024
1 parent 7ee4a73 commit 7184662
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions internal/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,4 +876,9 @@ var migrations = []func(tx *sql.Tx) error{
_, err = tx.Exec(sql)
return err
},
func(tx *sql.Tx) (err error) {
sql := `UPDATE entries SET tags = array_remove(tags, '') WHERE '' = ANY(tags);`
_, err = tx.Exec(sql)
return err
},
}
14 changes: 12 additions & 2 deletions internal/storage/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (s *Storage) createEntry(tx *sql.Tx, entry *model.Entry) error {
entry.UserID,
entry.FeedID,
entry.ReadingTime,
pq.Array(removeDuplicates(entry.Tags)),
pq.Array(removeEmpty(removeDuplicates(entry.Tags))),
).Scan(
&entry.ID,
&entry.Status,
Expand Down Expand Up @@ -195,7 +195,7 @@ func (s *Storage) updateEntry(tx *sql.Tx, entry *model.Entry) error {
entry.UserID,
entry.FeedID,
entry.Hash,
pq.Array(removeDuplicates(entry.Tags)),
pq.Array(removeEmpty(removeDuplicates(entry.Tags))),
).Scan(&entry.ID)

if err != nil {
Expand Down Expand Up @@ -620,3 +620,13 @@ func removeDuplicates(l []string) []string {
slices.Sort(l)
return slices.Compact(l)
}

func removeEmpty(l []string) []string {
finalSlice := []string{}
for _, item := range l {
if item != "" {
finalSlice = append(finalSlice, item)
}
}
return finalSlice
}

0 comments on commit 7184662

Please sign in to comment.