refactor: introduce GC, to delete old data
This commit is contained in:
@@ -73,6 +73,10 @@ func (db *MockDataStore) Resource(ctx context.Context, m interface{}) model.Reso
|
||||
return struct{ model.ResourceRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) GC(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type mockedUserRepo struct {
|
||||
model.UserRepository
|
||||
}
|
||||
|
||||
@@ -113,6 +113,26 @@ func (db *NewSQLStore) WithTx(block func(tx model.DataStore) error) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *NewSQLStore) GC(ctx context.Context) error {
|
||||
err := db.Album(ctx).PurgeEmpty()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.Artist(ctx).PurgeEmpty()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.MediaFile(ctx).(*mediaFileRepository).cleanSearchIndex()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.Album(ctx).(*albumRepository).cleanSearchIndex()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Artist(ctx).(*artistRepository).cleanSearchIndex()
|
||||
}
|
||||
|
||||
func (db *NewSQLStore) getOrmer() orm.Ormer {
|
||||
if db.orm == nil {
|
||||
return orm.NewOrm()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
|
||||
. "github.com/Masterminds/squirrel"
|
||||
"github.com/deluan/navidrome/log"
|
||||
"github.com/kennygrant/sanitize"
|
||||
)
|
||||
|
||||
@@ -14,6 +15,7 @@ func (r sqlRepository) index(id string, text string) error {
|
||||
|
||||
values := map[string]interface{}{
|
||||
"id": id,
|
||||
"item_type": r.tableName,
|
||||
"full_text": sanitizedText,
|
||||
}
|
||||
update := Update(searchTable).Where(Eq{"id": id}).SetMap(values)
|
||||
@@ -54,3 +56,15 @@ func (r sqlRepository) doSearch(q string, offset, size int, results interface{},
|
||||
_, err = r.ormer.Raw(sql, args...).QueryRows(results)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r sqlRepository) cleanSearchIndex() error {
|
||||
del := Delete(searchTable).Where(Eq{"item_type": r.tableName}).Where("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 search index", "table", r.tableName, "totalDeleted", c)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user