feat(library): named media libraries managed through the API
- feat(storage): libraries table (name, path); audio_files points at a library
by id instead of repeating the root path; migration 0005 for both drivers
turns every existing root into a library and keeps file ids, tracks and the
missing flag
- feat(library): Libraries service validates names and paths: absolute, an
existing directory, never the filesystem root, never inside or around
another library (checked on real paths, so symlinks cannot hide an overlap)
- feat(api): admin-only GET/POST /libraries, PATCH/DELETE /libraries/{id} and
POST /libraries/{id}/scan; deleting a library drops its index entries and
keeps tracks and listening history
- feat(library): the scanner and the scan manager work on libraries; scanning
with no libraries answers 409 no_libraries
- feat(streaming): the library directory comes from the database instead of
a configured list of roots
- breaking(config): library.roots is removed; an old config fails with a hint
to use POST /api/v1/libraries
- test: CRUD and duplicates on both databases, path change keeps files,
upgrade of a database written before libraries existed
This commit is contained in:
parent
99de521130
commit
46463846b3
2 changed files with 1542 additions and 236 deletions
27
migrations/postgres/0005_libraries.sql
Normal file
27
migrations/postgres/0005_libraries.sql
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
-- Named media libraries. audio_files points at a library by id instead of
|
||||
-- repeating the root path in every row.
|
||||
|
||||
CREATE TABLE libraries (
|
||||
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
path TEXT NOT NULL UNIQUE, -- absolute directory
|
||||
created_at BIGINT NOT NULL -- unix seconds
|
||||
);
|
||||
|
||||
-- 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.
|
||||
INSERT INTO libraries (name, path, created_at)
|
||||
SELECT DISTINCT root, root, extract(epoch FROM now())::bigint FROM audio_files;
|
||||
|
||||
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;
|
||||
ALTER TABLE audio_files ALTER COLUMN library_id SET NOT NULL;
|
||||
|
||||
-- Dropping the column also drops UNIQUE (root, rel_path).
|
||||
ALTER TABLE audio_files DROP COLUMN root;
|
||||
ALTER TABLE audio_files ADD CONSTRAINT audio_files_library_rel_path_key UNIQUE (library_id, rel_path);
|
||||
|
||||
CREATE INDEX idx_audio_files_library ON audio_files(library_id);
|
||||
44
migrations/sqlite/0005_libraries.sql
Normal file
44
migrations/sqlite/0005_libraries.sql
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
-- Named media libraries. audio_files points at a library by id instead of
|
||||
-- repeating the root path in every row.
|
||||
|
||||
CREATE TABLE libraries (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE COLLATE NOCASE,
|
||||
path TEXT NOT NULL UNIQUE, -- absolute directory
|
||||
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.
|
||||
INSERT INTO libraries (name, path, created_at)
|
||||
SELECT DISTINCT root, root, unixepoch() FROM audio_files;
|
||||
|
||||
-- SQLite cannot drop a column that is part of a UNIQUE constraint, so the
|
||||
-- table is rebuilt.
|
||||
CREATE TABLE audio_files_new (
|
||||
id INTEGER PRIMARY KEY,
|
||||
track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
|
||||
library_id INTEGER NOT NULL REFERENCES libraries(id) ON DELETE CASCADE,
|
||||
rel_path TEXT NOT NULL, -- relative to the library path
|
||||
format TEXT NOT NULL,
|
||||
size_bytes INTEGER NOT NULL,
|
||||
mtime_unix INTEGER NOT NULL,
|
||||
sample_rate INTEGER,
|
||||
bitrate INTEGER,
|
||||
content_hash TEXT,
|
||||
missing_at INTEGER, -- unix time the file was last seen missing
|
||||
UNIQUE (library_id, rel_path)
|
||||
);
|
||||
|
||||
INSERT INTO audio_files_new
|
||||
(id, track_id, library_id, rel_path, format, size_bytes, mtime_unix,
|
||||
sample_rate, bitrate, content_hash, missing_at)
|
||||
SELECT f.id, f.track_id, l.id, f.rel_path, f.format, f.size_bytes, f.mtime_unix,
|
||||
f.sample_rate, f.bitrate, f.content_hash, f.missing_at
|
||||
FROM audio_files f JOIN libraries l ON l.path = f.root;
|
||||
|
||||
DROP TABLE audio_files;
|
||||
ALTER TABLE audio_files_new RENAME TO audio_files;
|
||||
|
||||
CREATE INDEX idx_audio_files_track ON audio_files(track_id);
|
||||
CREATE INDEX idx_audio_files_library ON audio_files(library_id);
|
||||
Loading…
Reference in a new issue