Remove dangling tracks after changing MusicFolder. Fix #445

This commit is contained in:
Deluan
2020-10-02 16:18:45 -04:00
parent 1be79fa945
commit f859772723
5 changed files with 27 additions and 13 deletions
+12 -1
View File
@@ -108,7 +108,7 @@ func (r mediaFileRepository) FindAllByPath(path string) (model.MediaFiles, error
return res, err
}
func pathStartsWith(path string) Sqlizer {
func pathStartsWith(path string) Eq {
cleanPath := filepath.Clean(path)
substr := fmt.Sprintf("substr(path, 1, %d)", utf8.RuneCountInString(cleanPath))
return Eq{substr: cleanPath}
@@ -124,6 +124,17 @@ func (r mediaFileRepository) FindPathsRecursively(basePath string) ([]string, er
return res, err
}
func (r mediaFileRepository) deleteNotInPath(basePath string) error {
sel := Delete(r.tableName).Where(NotEq(pathStartsWith(basePath)))
c, err := r.executeSQL(sel)
if err == nil {
if c > 0 {
log.Debug(r.ctx, "Deleted dangling tracks", "totalDeleted", c)
}
}
return err
}
func (r mediaFileRepository) GetStarred(options ...model.QueryOptions) (model.MediaFiles, error) {
sq := r.selectMediaFile(options...).Where("starred = true")
starred := model.MediaFiles{}
+1 -1
View File
@@ -89,7 +89,7 @@ func (db *MockDataStore) Resource(ctx context.Context, m interface{}) model.Reso
return struct{ model.ResourceRepository }{}
}
func (db *MockDataStore) GC(ctx context.Context) error {
func (db *MockDataStore) GC(ctx context.Context, rootFolder string) error {
return nil
}
+7 -2
View File
@@ -111,8 +111,13 @@ func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error {
return nil
}
func (s *SQLStore) GC(ctx context.Context) error {
err := s.Album(ctx).(*albumRepository).purgeEmpty()
func (s *SQLStore) GC(ctx context.Context, rootFolder string) error {
err := s.MediaFile(ctx).(*mediaFileRepository).deleteNotInPath(rootFolder)
if err != nil {
log.Error(ctx, "Error removing dangling tracks", err)
return err
}
err = s.Album(ctx).(*albumRepository).purgeEmpty()
if err != nil {
log.Error(ctx, "Error removing empty albums", err)
return err