mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Purge unused genres at the end of the scan
This commit is contained in:
parent
b56e034ce3
commit
c56c7c865e
2 changed files with 26 additions and 0 deletions
|
@ -3,6 +3,8 @@ package persistence
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/navidrome/navidrome/log"
|
||||
|
||||
"github.com/deluan/rest"
|
||||
|
||||
. "github.com/Masterminds/squirrel"
|
||||
|
@ -68,5 +70,24 @@ func (r *genreRepository) NewInstance() interface{} {
|
|||
return &model.Genre{}
|
||||
}
|
||||
|
||||
func (r *genreRepository) purgeEmpty() error {
|
||||
del := Delete(r.tableName).Where(`id in (
|
||||
select genre.id from genre
|
||||
left join album_genres ag on genre.id = ag.genre_id
|
||||
left join artist_genres a on genre.id = a.genre_id
|
||||
left join media_file_genres mfg on genre.id = mfg.genre_id
|
||||
where ag.genre_id is null
|
||||
and a.genre_id is null
|
||||
and mfg.genre_id is null
|
||||
)`)
|
||||
c, err := r.executeSQL(del)
|
||||
if err == nil {
|
||||
if c > 0 {
|
||||
log.Debug(r.ctx, "Purged unused genres", "totalDeleted", c)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var _ model.GenreRepository = (*genreRepository)(nil)
|
||||
var _ model.ResourceRepository = (*genreRepository)(nil)
|
||||
|
|
|
@ -165,6 +165,11 @@ func (s *SQLStore) GC(ctx context.Context, rootFolder string) error {
|
|||
if err != nil {
|
||||
log.Error(ctx, "Error tidying up playlists", err)
|
||||
}
|
||||
err = s.Genre(ctx).(*genreRepository).purgeEmpty()
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error removing unused genres", err)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue