-- +goose Up CREATE TABLE devices ( id bigserial PRIMARY KEY, name text NOT NULL UNIQUE, kind text NOT NULL, api_key_hash text NOT NULL UNIQUE, created_at timestamptz NOT NULL DEFAULT now(), last_seen_at timestamptz, revoked_at timestamptz ); CREATE TABLE activities ( id bigserial PRIMARY KEY, device_id bigint NOT NULL REFERENCES devices (id), source text NOT NULL, kind text NOT NULL, label text NOT NULL DEFAULT '', started_at timestamptz NOT NULL, ended_at timestamptz NOT NULL, duration_s integer GENERATED ALWAYS AS (extract(epoch FROM ended_at - started_at)::integer) STORED, attrs jsonb NOT NULL DEFAULT '{}', external_id text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), CHECK (ended_at >= started_at), UNIQUE (source, external_id) ); CREATE INDEX activities_started_at_idx ON activities (started_at); CREATE INDEX activities_kind_started_at_idx ON activities (kind, started_at); CREATE INDEX activities_label_idx ON activities (label); -- Point-in-time metrics such as step counts or heart rate. CREATE TABLE samples ( id bigserial PRIMARY KEY, device_id bigint NOT NULL REFERENCES devices (id), source text NOT NULL, metric text NOT NULL, ts timestamptz NOT NULL, value double precision NOT NULL, unit text NOT NULL DEFAULT '', external_id text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), UNIQUE (source, external_id) ); CREATE INDEX samples_metric_ts_idx ON samples (metric, ts); -- Daily targets, e.g. kind=tab_focus label=youtube.com op=<= target_s=3600. -- An empty label matches every label of that kind. CREATE TABLE goals ( id bigserial PRIMARY KEY, kind text NOT NULL, label text NOT NULL DEFAULT '', op text NOT NULL CHECK (op IN ('>=', '<=')), target_s integer NOT NULL CHECK (target_s >= 0), period text NOT NULL DEFAULT 'day' CHECK (period IN ('day')), created_at timestamptz NOT NULL DEFAULT now() ); -- Output of future AI analysis jobs. CREATE TABLE reports ( id bigserial PRIMARY KEY, period_start timestamptz NOT NULL, period_end timestamptz NOT NULL, model text NOT NULL, prompt_hash text NOT NULL, body text NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); -- +goose Down DROP TABLE reports; DROP TABLE goals; DROP TABLE samples; DROP TABLE activities; DROP TABLE devices;