refactor:clean annotations in GC

This commit is contained in:
Deluan 2020-02-01 12:15:12 -05:00 committed by Deluan Quintão
parent 88e01d05f6
commit 0d0787e656
4 changed files with 26 additions and 1 deletions

View File

@ -126,7 +126,19 @@ func (db *NewSQLStore) GC(ctx context.Context) error {
if err != nil {
return err
}
return db.Artist(ctx).(*artistRepository).cleanSearchIndex()
err = db.Artist(ctx).(*artistRepository).cleanSearchIndex()
if err != nil {
return err
}
err = db.MediaFile(ctx).(*mediaFileRepository).cleanAnnotations()
if err != nil {
return err
}
err = db.Album(ctx).(*albumRepository).cleanAnnotations()
if err != nil {
return err
}
return db.Artist(ctx).(*artistRepository).cleanAnnotations()
}
func (db *NewSQLStore) getOrmer() orm.Ormer {

View File

@ -5,6 +5,7 @@ import (
. "github.com/Masterminds/squirrel"
"github.com/astaxie/beego/orm"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/google/uuid"
)
@ -95,3 +96,15 @@ func (r sqlRepository) SetStar(starred bool, ids ...string) error {
func (r sqlRepository) SetRating(rating int, itemID string) error {
return r.annUpsert(map[string]interface{}{"rating": rating}, itemID)
}
func (r sqlRepository) cleanAnnotations() error {
del := Delete(annotationTable).Where(Eq{"item_type": r.tableName}).Where("item_id not in (select id from " + r.tableName + ")")
c, err := r.executeSQL(del)
if err != nil {
return err
}
if c > 0 {
log.Debug(r.ctx, "Clean-up annotations", "table", r.tableName, "totalDeleted", c)
}
return nil
}