feat(playlists): a user keeps playlists of tracks

Each user can make playlists: a name (1 to 200 characters), an optional
description and an ordered list of tracks, where a track may appear more than once
(each place has its own item id). The API makes, lists, renames and deletes
playlists, and adds tracks (at the end or at a position), lists them, moves one and
removes one; positions count from 0 without gaps.

A playlist is its owner's alone: somebody else's answers playlist_not_found (404),
as one that does not exist. A track whose files were deleted or moved stays in the
playlist and is listed with available false. Deleting a user deletes their
playlists. Limits: 1,000 playlists per user, 10,000 tracks per playlist, 500 tracks
per request.

Changes of one playlist are serialised by locking its row, so two requests at once
keep the positions unique on PostgreSQL too. Migration 0019, OpenAPI spec, contract
test, README and the web API types follow. The web app comes separately.

Closes #89
This commit is contained in:
tgiachi 2026-11-12 16:55:25 +01:00
commit f900a22904
2 changed files with 2732 additions and 2 deletions

View file

@ -0,0 +1,24 @@
-- Playlists: named, ordered lists of tracks that belong to one user. PostgreSQL flavour of
-- sqlite/0019_playlists.sql.
CREATE TABLE playlists (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
created_at BIGINT NOT NULL, -- unix seconds
updated_at BIGINT NOT NULL
);
CREATE INDEX idx_playlists_user ON playlists(user_id, updated_at);
CREATE TABLE playlist_items (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
playlist_id BIGINT NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
track_id BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
position INTEGER NOT NULL CHECK (position >= 0),
added_at BIGINT NOT NULL
);
CREATE INDEX idx_playlist_items_order ON playlist_items(playlist_id, position);
CREATE INDEX idx_playlist_items_track ON playlist_items(track_id);

View file

@ -0,0 +1,27 @@
-- Playlists: named, ordered lists of tracks that belong to one user.
--
-- position is the place of an item in its playlist, counted from 0 without gaps; the code keeps it
-- that way inside a transaction (it is not unique, so a shift never trips over itself). A track
-- can be in a playlist more than once, which is why an item has its own id.
CREATE TABLE playlists (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
created_at INTEGER NOT NULL, -- unix seconds
updated_at INTEGER NOT NULL
);
CREATE INDEX idx_playlists_user ON playlists(user_id, updated_at);
CREATE TABLE playlist_items (
id INTEGER PRIMARY KEY,
playlist_id INTEGER NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
position INTEGER NOT NULL CHECK (position >= 0),
added_at INTEGER NOT NULL
);
CREATE INDEX idx_playlist_items_order ON playlist_items(playlist_id, position);
CREATE INDEX idx_playlist_items_track ON playlist_items(track_id);