fix(library): make the libraries upgrade and validation robust

- fix(migration): roots that differ only in case (/Music and /music are two
  directories on Linux) used to make migration 0005 fail on the unique name;
  the later ones now get a numeric suffix
- feat(library): at startup, log a warning for every library whose directory
  cannot be read or that overlaps another, because migrated rows skip
  validation and may still hold roots that were removed from the old config
- fix(library): creating or updating a library is now serialised, so two
  concurrent requests cannot both pass the overlap check
- fix(library): changing a library's directory or deleting it is refused with
  409 scan_running while a scan runs; the scan works from the list it started
  with and would write under a stale path
- fix(library): library names clash ignoring case and accents the same way on
  SQLite and PostgreSQL, checked in Go
- fix(library): changing only the path no longer revalidates an untouched
  name, which can be longer than the limit after a migration
- test: roots differing in case on both databases; concurrent creates with a
  slow store, verified to fail without the lock; the busy guard; Problems
This commit is contained in:
tgiachi 2026-11-11 13:34:44 +01:00
commit eb23ee426a
2 changed files with 342 additions and 23 deletions

View file

@ -11,10 +11,14 @@ CREATE TABLE libraries (
-- Case-insensitive uniqueness (SQLite uses COLLATE NOCASE for the same effect).
CREATE UNIQUE INDEX libraries_name_lower ON libraries (lower(name));
-- Existing data: every distinct root becomes a library. The path doubles as
-- the name, which is always unique; admins can rename it afterwards.
-- Existing data: every distinct root becomes a library, named after its path.
-- Names are unique ignoring case, and on Linux /Music and /music are two
-- directories, so the second one gets a suffix. Admins can rename them later.
INSERT INTO libraries (name, path, created_at)
SELECT DISTINCT root, root, extract(epoch FROM now())::bigint FROM audio_files;
SELECT CASE WHEN n = 1 THEN root ELSE root || ' (' || n || ')' END, root,
extract(epoch FROM now())::bigint
FROM (SELECT root, ROW_NUMBER() OVER (PARTITION BY lower(root) ORDER BY root) AS n
FROM (SELECT DISTINCT root FROM audio_files) AS roots) AS numbered;
ALTER TABLE audio_files ADD COLUMN library_id BIGINT REFERENCES libraries(id) ON DELETE CASCADE;
UPDATE audio_files f SET library_id = l.id FROM libraries l WHERE l.path = f.root;

View file

@ -8,10 +8,13 @@ CREATE TABLE libraries (
created_at INTEGER NOT NULL -- unix seconds
);
-- Existing data: every distinct root becomes a library. The path doubles as
-- the name, which is always unique; admins can rename it afterwards.
-- Existing data: every distinct root becomes a library, named after its path.
-- Names are unique ignoring case, and on Linux /Music and /music are two
-- directories, so the second one gets a suffix. Admins can rename them later.
INSERT INTO libraries (name, path, created_at)
SELECT DISTINCT root, root, unixepoch() FROM audio_files;
SELECT CASE WHEN n = 1 THEN root ELSE root || ' (' || n || ')' END, root, unixepoch()
FROM (SELECT root, ROW_NUMBER() OVER (PARTITION BY lower(root) ORDER BY root) AS n
FROM (SELECT DISTINCT root FROM audio_files));
-- SQLite cannot drop a column that is part of a UNIQUE constraint, so the
-- table is rebuilt.