fix: refresh match keys at startup and re-check file type after stat
- fix(storage): RefreshMatchKeys recomputes name_key and title_key for rows created before migration 0003, whose SQL lower() key is ASCII-only on SQLite; idempotent, runs at every start - fix(library): re-check that the stat result is a regular file, closing the window where a path is swapped for a FIFO or symlink after the directory read - docs(library): note that OnFinish callbacks of consecutive scans can overlap - test: old rows with approximate keys are matched after the refresh
This commit is contained in:
parent
d6e9e09ce7
commit
11aa47e79f
5 changed files with 85 additions and 1 deletions
|
|
@ -83,7 +83,11 @@ func run() error {
|
|||
if cfg.Log.Env == "production" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
scanner := library.NewScanner(storage.NewLibraryRepo(db), library.TaglibReader{}, library.NewHub(), cfg.Library.ScanWorkers)
|
||||
libraryRepo := storage.NewLibraryRepo(db)
|
||||
if err := libraryRepo.RefreshMatchKeys(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
scanner := library.NewScanner(libraryRepo, library.TaglibReader{}, library.NewHub(), cfg.Library.ScanWorkers)
|
||||
scans := library.NewManager(scanner, cfg.Library.Roots)
|
||||
logScanResults(logger, scans)
|
||||
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ type Manager struct {
|
|||
|
||||
// OnFinish registers a function called once after every scan, with the result
|
||||
// of that scan. Unlike hub events it is never dropped. Set it before Start.
|
||||
// A new scan may start while a callback is still running, so callbacks of
|
||||
// consecutive scans can overlap: keep the function safe for concurrent use.
|
||||
func (m *Manager) OnFinish(fn func(Summary, error)) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
|
|
|||
|
|
@ -273,6 +273,11 @@ func (s *Scanner) walk(ctx context.Context, root string, sum *Summary) (walkResu
|
|||
}
|
||||
return nil
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
// Replaced by a FIFO or symlink between the directory read and now.
|
||||
sum.Skipped++
|
||||
return nil
|
||||
}
|
||||
out.files = append(out.files, FileInfo{RelPath: rel, Size: info.Size(), MTimeUnix: info.ModTime().Unix()})
|
||||
return nil
|
||||
})
|
||||
|
|
|
|||
|
|
@ -205,6 +205,45 @@ func (r *LibraryRepo) Restore(ctx context.Context, ids []int64) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// RefreshMatchKeys recomputes name_key and title_key for every artist and album
|
||||
// whose stored key differs from the current rule. It is idempotent and cheap
|
||||
// when nothing changed, so it can run at every start: rows created before
|
||||
// migration 0003 only have an approximate key from SQL lower().
|
||||
func (r *LibraryRepo) RefreshMatchKeys(ctx context.Context) error {
|
||||
return r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
var artists []artistModel
|
||||
if err := tx.NewSelect().Model(&artists).Column("id", "name", "name_key").Scan(ctx); err != nil {
|
||||
return fmt.Errorf("list artists: %w", err)
|
||||
}
|
||||
for _, a := range artists {
|
||||
key := matchKey(a.Name)
|
||||
if key == a.NameKey {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.NewUpdate().Model((*artistModel)(nil)).
|
||||
Set("name_key = ?", key).Where("id = ?", a.ID).Exec(ctx); err != nil {
|
||||
return fmt.Errorf("update artist key: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
var albums []albumModel
|
||||
if err := tx.NewSelect().Model(&albums).Column("id", "title", "title_key").Scan(ctx); err != nil {
|
||||
return fmt.Errorf("list albums: %w", err)
|
||||
}
|
||||
for _, al := range albums {
|
||||
key := matchKey(al.Title)
|
||||
if key == al.TitleKey {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.NewUpdate().Model((*albumModel)(nil)).
|
||||
Set("title_key = ?", key).Where("id = ?", al.ID).Exec(ctx); err != nil {
|
||||
return fmt.Errorf("update album key: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// ---- helpers (run inside a transaction) ----
|
||||
|
||||
func findOrCreateArtist(ctx context.Context, tx bun.Tx, name string) (int64, error) {
|
||||
|
|
|
|||
|
|
@ -107,6 +107,40 @@ func TestLibraryRepoMatchesUnicodeNamesCaseInsensitively(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// Rows written before migration 0003 got an ASCII-only key from SQL lower().
|
||||
// RefreshMatchKeys recomputes them so matching works for those rows too.
|
||||
func TestLibraryRepoRefreshMatchKeysFixesOldRows(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
ctx := context.Background()
|
||||
repo := NewLibraryRepo(db)
|
||||
|
||||
// Simulate what the migration backfill leaves behind on SQLite.
|
||||
if _, err := db.Exec("INSERT INTO artists (name, name_key) VALUES ('BJÖRK', 'bjÖrk')"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Exec("INSERT INTO albums (title, title_key, artist_id) SELECT 'POST', 'post', id FROM artists"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := repo.RefreshMatchKeys(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := repo.RefreshMatchKeys(ctx); err != nil { // idempotent
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := repo.SaveFile(ctx, "/m", parsed("1.flac", "Hyperballad", "post", "Björk"), 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n := count(t, db, "SELECT COUNT(*) FROM artists"); n != 1 {
|
||||
t.Errorf("artists = %d, want 1: the old row was not matched", n)
|
||||
}
|
||||
if n := count(t, db, "SELECT COUNT(*) FROM albums"); n != 1 {
|
||||
t.Errorf("albums = %d, want 1", n)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestLibraryRepoTrackWithoutAlbum(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
repo := NewLibraryRepo(db)
|
||||
|
|
|
|||
Loading…
Reference in a new issue