sonda/migrations/sqlite/0001_init.sql
tgiachi 3a739dc3ae feat: add sondad server with config, storage, auth and listening API
- feat(config): TOML config with env overrides, strict parsing and validation
- feat(storage): SQLite (modernc) and PostgreSQL (pgx) backends through bun,
  embedded per-driver SQL migrations with a transactional runner
- feat(schema): catalog tables (artists, albums, tracks, audio_files with
  per-root paths), users and listening events with idempotency keys
- feat(auth): Argon2id password hashing, HS256 JWT tokens, first-user-only
  registration and login
- feat(api): gin REST API under /api/v1 (health, register, login, listens)
  with a consistent error envelope, body limit and request logging
- feat(sondad): zap logger, graceful shutdown, composition root in main
- chore: example config, .gitignore for secrets, DB files, IDE and agent files
2026-11-11 12:06:15 +01:00

55 lines
2.1 KiB
SQL

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