persistence/genre: change Put() to upsert

Absolutely disgusting hack to work around [1]. Try to insert the genre,
but if it conflicts, ignore it and update the genre with the existing
ID.

[1]: https://github.com/navidrome/navidrome/issues/1918.
This commit is contained in:
Zane van Iperen committed 2022-10-07 20:02:46 +10:00
1 parent c530ccf138
commit 0a225107e4
1 file changed
+13 -3
+13 -3
View File
@@ -3,6 +3,8 @@ package persistence
import (
"context"
"github.com/google/uuid"
. "github.com/Masterminds/squirrel"
"github.com/beego/beego/v2/client/orm"
"github.com/deluan/rest"
@@ -36,9 +38,17 @@ func (r *genreRepository) GetAll(opt ...model.QueryOptions) (model.Genres, error
}
func (r *genreRepository) Put(m *model.Genre) error {
id, err := r.put(m.ID, m)
m.ID = id
return err
if m.ID == "" {
m.ID = uuid.NewString()
}
return r.ormer.Raw(`
INSERT INTO genre(id, name)
VALUES(?, ?)
ON CONFLICT (name) DO UPDATE
SET name=excluded.name
RETURNING id
`, m.ID, m.Name).QueryRow(&m.ID)
}
func (r *genreRepository) Count(options ...rest.QueryOptions) (int64, error) {