-- Core catalog for local music (M1). -- Internal ids are authoritative; MusicBrainz ids are optional and never invented. CREATE TABLE artists ( id INTEGER 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 INTEGER PRIMARY KEY, mbid TEXT UNIQUE, -- MusicBrainz release-group id, NULL when unknown title TEXT NOT NULL, artist_id INTEGER REFERENCES artists(id) ); CREATE TABLE tracks ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, duration_ms INTEGER, track_no INTEGER, disc_no INTEGER, album_id INTEGER REFERENCES albums(id), recording_mbid TEXT UNIQUE -- MusicBrainz recording id, NULL when unknown ); CREATE TABLE track_artists ( track_id INTEGER NOT NULL REFERENCES tracks(id) ON DELETE CASCADE, artist_id INTEGER 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 INTEGER PRIMARY KEY, track_id INTEGER 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 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 (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);