Avoid constraint error when having duplicate entries

During feed creation, duplicated entries will generate an SQL contraint error.

This change ignore the duplicated entry to avoid showing an error.
This commit is contained in:
Frédéric Guillot 2019-09-18 22:41:33 -07:00 committed by Frédéric Guillot
parent 36d7732234
commit d610d091fe
2 changed files with 9 additions and 6 deletions

View File

@ -84,7 +84,7 @@ func (s *Storage) createEntry(entry *model.Entry) error {
).Scan(&entry.ID, &entry.Status)
if err != nil {
return fmt.Errorf("unable to create entry %q (feed #%d): %v", entry.URL, entry.FeedID, err)
return fmt.Errorf("Unable to create entry %q (feed #%d): %v", entry.URL, entry.FeedID, err)
}
for i := 0; i < len(entry.Enclosures); i++ {
@ -137,9 +137,9 @@ func (s *Storage) updateEntry(entry *model.Entry) error {
// entryExists checks if an entry already exists based on its hash when refreshing a feed.
func (s *Storage) entryExists(entry *model.Entry) bool {
var result int
query := `SELECT count(*) as c FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`
query := `SELECT 1 FROM entries WHERE user_id=$1 AND feed_id=$2 AND hash=$3`
s.db.QueryRow(query, entry.UserID, entry.FeedID, entry.Hash).Scan(&result)
return result >= 1
return result == 1
}
// cleanupEntries deletes from the database entries marked as "removed" and not visible anymore in the feed.

View File

@ -216,9 +216,12 @@ func (s *Storage) CreateFeed(feed *model.Feed) error {
for i := 0; i < len(feed.Entries); i++ {
feed.Entries[i].FeedID = feed.ID
feed.Entries[i].UserID = feed.UserID
err := s.createEntry(feed.Entries[i])
if err != nil {
return err
if !s.entryExists(feed.Entries[i]) {
err := s.createEntry(feed.Entries[i])
if err != nil {
return err
}
}
}