Skip to content

Commit

Permalink
Return counter from DeleteByPath
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 12, 2020
1 parent e55397f commit dc8368c
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion model/mediafile.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type MediaFileRepository interface {
GetRandom(options ...QueryOptions) (MediaFiles, error)
Search(q string, offset int, size int) (MediaFiles, error)
Delete(id string) error
DeleteByPath(path string) error
DeleteByPath(path string) (int64, error)

AnnotatedRepository
}
Expand Down
5 changes: 2 additions & 3 deletions persistence/mediafile_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,13 @@ func (r mediaFileRepository) Delete(id string) error {
}

// DeleteByPath delete from the DB all mediafiles that are direct children of path
func (r mediaFileRepository) DeleteByPath(path string) error {
func (r mediaFileRepository) DeleteByPath(path string) (int64, error) {
path = filepath.Clean(path)
del := Delete(r.tableName).
Where(And{Like{"path": filepath.Join(path, "%")},
Eq{fmt.Sprintf("substr(path, %d) glob '*%s*'", len(path)+2, string(os.PathSeparator)): 0}})
log.Debug(r.ctx, "Deleting mediafiles by path", "path", path)
_, err := r.executeSQL(del)
return err
return r.executeSQL(del)
}

func (r mediaFileRepository) Search(q string, offset int, size int) (model.MediaFiles, error) {
Expand Down
2 changes: 1 addition & 1 deletion persistence/mediafile_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var _ = Describe("MediaRepository", func() {
id3 := "3333"
Expect(mr.Put(&model.MediaFile{ID: id3, Path: P("/abc/" + id3 + ".mp3")})).To(BeNil())

Expect(mr.DeleteByPath(P("/abc"))).To(BeNil())
Expect(mr.DeleteByPath(P("/abc"))).To(Equal(int64(1)))

Expect(mr.Get(id1)).ToNot(BeNil())
Expect(mr.Get(id2)).ToNot(BeNil())
Expand Down
5 changes: 3 additions & 2 deletions scanner/tag_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ func (s *TagScanner) processDeletedDir(ctx context.Context, dir string, updatedA
}

log.Info("Finished processing deleted folder", "dir", dir, "purged", len(ct), "elapsed", time.Since(start))
return s.ds.MediaFile(ctx).DeleteByPath(dir)
_, err = s.ds.MediaFile(ctx).DeleteByPath(dir)
return err
}

func (s *TagScanner) removeDeletedFolders(ctx context.Context, changed []string) {
Expand All @@ -303,7 +304,7 @@ func (s *TagScanner) removeDeletedFolders(ctx context.Context, changed []string)
for _, path := range paths {
if readable, err := utils.IsDirReadable(path); !readable {
log.Info(ctx, "Path unavailable. Removing tracks from DB", "path", path, err)
err = s.ds.MediaFile(ctx).DeleteByPath(path)
_, err = s.ds.MediaFile(ctx).DeleteByPath(path)
if err != nil {
log.Error(ctx, "Error removing MediaFiles from DB", "path", path, err)
}
Expand Down

0 comments on commit dc8368c

Please sign in to comment.