Skip to content

Commit

Permalink
Remove dangling tracks after changing MusicFolder. Fix #445
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Oct 2, 2020
1 parent 1be79fa commit f859772
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion model/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ type DataStore interface {
Resource(ctx context.Context, model interface{}) ResourceRepository

WithTx(func(tx DataStore) error) error
GC(ctx context.Context) error
GC(ctx context.Context, rootFolder string) error
}
13 changes: 12 additions & 1 deletion persistence/mediafile_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion persistence/mock_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 7 additions & 2 deletions persistence/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions scanner/tag_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ func NewTagScanner(rootFolder string, ds model.DataStore) *TagScanner {
}
}

type (
counters struct {
added int64
updated int64
deleted int64
}
)
type counters struct {
added int64
updated int64
deleted int64
}

const (
// filesBatchSize used for batching file metadata extraction
Expand Down Expand Up @@ -125,7 +123,7 @@ func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time) erro
log.Debug("Playlist auto-import is disabled")
}

err = s.ds.GC(log.NewContext(ctx))
err = s.ds.GC(log.NewContext(ctx), s.rootFolder)
log.Info("Finished processing Music Folder", "folder", s.rootFolder, "elapsed", time.Since(start),
"added", s.cnt.added, "updated", s.cnt.updated, "deleted", s.cnt.deleted, "playlistsImported", plsCount)

Expand Down

0 comments on commit f859772

Please sign in to comment.