fix(scanner): make sure we roll back invalid parents

fixes #402
This commit is contained in:
sentriz
2023-11-04 19:03:52 +00:00
parent 27b2d36376
commit ddb686bddc
2 changed files with 26 additions and 10 deletions

View File

@@ -231,17 +231,13 @@ func (s *Scanner) scanCallback(c *Context, absPath string, d fs.DirEntry, err er
log.Printf("processing folder %q", absPath)
tx := s.db.Begin()
return s.db.Transaction(func(tx *db.DB) error {
if err := s.scanDir(tx, c, absPath); err != nil {
c.errs = append(c.errs, fmt.Errorf("%q: %w", absPath, err))
tx.Rollback()
return nil
}
if err := tx.Commit().Error; err != nil {
return fmt.Errorf("commit tx: %w", err)
}
return nil
})
}
func (s *Scanner) scanDir(tx *db.DB, c *Context, absPath string) error {

View File

@@ -767,3 +767,23 @@ func TestMultiArtistPreload(t *testing.T) {
}
}
}
// https://github.com/sentriz/gonic/issues/402
func TestRootNoClobberOnError(t *testing.T) {
t.Parallel()
m := mockfs.New(t)
m.AddItems()
m.SetTags("artist-0/album-0/track-0.flac", func(tags *mockfs.TagInfo) { tags.Error = fmt.Errorf("no") }) // give a track an error
m.AddCover("artist-0/album-0/Artwork/cover.png") // and add an extra cover dir
_, err := m.ScanAndCleanErr()
require.Error(t, err)
var roots []*db.Album
require.NoError(t, m.DB().Find(&roots, "parent_id IS NULL").Error)
require.Len(t, roots, 1)
require.Equal(t, ".", roots[0].RightPath)
require.Equal(t, 0, roots[0].ParentID)
}