sonda/migrations/postgres/0019_playlists.sql
tgiachi f900a22904 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
2026-11-12 16:55:25 +01:00

24 lines
1,011 B
SQL

-- 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);