-- Core catalog for local music (M1). PostgreSQL flavour of sqlite/0001_init.sql. -- Keep both files in step: same tables, same columns, same meaning. CREATE TABLE artists ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, mbid TEXT UNIQUE, -- MusicBrainz artist id, NULL when unknown name TEXT NOT NULL, tags TEXT NOT NULL DEFAULT '[]' -- JSON array ); CREATE TABLE albums ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, mbid TEXT UNIQUE, -- MusicBrainz release-group id, NULL when unknown title TEXT NOT NULL, artist_id BIGINT REFERENCES artists(id) ); CREATE TABLE tracks ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, title TEXT NOT NULL, duration_ms BIGINT, track_no INTEGER, disc_no INTEGER, album_id BIGINT REFERENCES albums(id), recording_mbid TEXT UNIQUE -- MusicBrainz recording id, NULL when unknown ); CREATE TABLE track_artists ( track_id BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, artist_id BIGINT NOT NULL REFERENCES artists(id) ON DELETE CASCADE, role TEXT NOT NULL DEFAULT 'main', PRIMARY KEY (track_id, artist_id, role) ); -- Physical files, kept separate from tracks so several encodings can exist. -- Missing files are marked, not deleted, so history survives a rescan. CREATE TABLE audio_files ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, track_id BIGINT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, root TEXT NOT NULL, -- configured library root the file belongs to rel_path TEXT NOT NULL, -- relative to that root format TEXT NOT NULL, size_bytes BIGINT NOT NULL, mtime_unix BIGINT NOT NULL, sample_rate INTEGER, bitrate INTEGER, content_hash TEXT, missing_at BIGINT, -- unix time the file was last seen missing UNIQUE (root, rel_path) ); CREATE INDEX idx_albums_artist ON albums(artist_id); CREATE INDEX idx_tracks_album ON tracks(album_id); CREATE INDEX idx_track_artists_artist ON track_artists(artist_id); CREATE INDEX idx_audio_files_track ON audio_files(track_id);