-- M7 #381: track library-scan / MBID-backfill / cover-enrich runs so the
-- admin overview can show last-run results + manual trigger state.
CREATE TABLE scan_runs (
    id              uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    started_at      timestamptz NOT NULL DEFAULT now(),
    finished_at     timestamptz,
    -- Per-stage tallies are jsonb so the schema doesn't need to grow when
    -- a stage gains/loses a counter. Each stage's value matches the Go
    -- struct emitted by that worker (e.g. library.Stats).
    library         jsonb,
    mbid_backfill   jsonb,
    cover_enrich    jsonb,
    -- Non-empty when the run as a whole errored (a stage panic, or the
    -- worker hit a fatal error before writing stage tallies).
    error_message   text
);

-- The status endpoint always reads the latest row; the in-flight guard
-- needs to find rows where finished_at IS NULL. Both hit started_at DESC.
CREATE INDEX scan_runs_started_at_idx ON scan_runs (started_at DESC);
