From 0a225107e4097f28fdee5386a8d11e8f71574e40 Mon Sep 17 00:00:00 2001 From: Zane van Iperen Date: Fri, 7 Oct 2022 19:52:00 +1000 Subject: [PATCH] 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. --- persistence/genre_repository.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/persistence/genre_repository.go b/persistence/genre_repository.go index 15e2fba1f..3c0fe8a43 100644 --- a/persistence/genre_repository.go +++ b/persistence/genre_repository.go @@ -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) {