diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index 1ac52cf..50e855f 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -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) diff --git a/internal/library/manager.go b/internal/library/manager.go index 455386c..868a334 100644 --- a/internal/library/manager.go +++ b/internal/library/manager.go @@ -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() diff --git a/internal/library/scanner.go b/internal/library/scanner.go index 79c5888..e387da0 100644 --- a/internal/library/scanner.go +++ b/internal/library/scanner.go @@ -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 }) diff --git a/internal/storage/library.go b/internal/storage/library.go index 37860fa..f7f8df7 100644 --- a/internal/storage/library.go +++ b/internal/storage/library.go @@ -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) { diff --git a/internal/storage/library_test.go b/internal/storage/library_test.go index 53ae88c..fdeb64d 100644 --- a/internal/storage/library_test.go +++ b/internal/storage/library_test.go @@ -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)