import { sql } from 'kysely'; // ADR 0002 §3. PostgreSQL ≥ 18 (uuidv7() ist eingebaut). export const up = async (db)=>{ await sql` CREATE TABLE brews ( id uuid PRIMARY KEY DEFAULT uuidv7(), share_id text NOT NULL UNIQUE, edit_id text NOT NULL UNIQUE, google_id text, title text NOT NULL DEFAULT '', text text NOT NULL DEFAULT '', page_count integer NOT NULL DEFAULT 1, description text NOT NULL DEFAULT '', tags text[] NOT NULL DEFAULT '{}', systems text[], lang text NOT NULL DEFAULT 'en', renderer text NOT NULL DEFAULT '', authors text[] NOT NULL DEFAULT '{}', invited_authors text[] NOT NULL DEFAULT '{}', published boolean NOT NULL DEFAULT false, thumbnail text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now(), last_viewed timestamptz NOT NULL DEFAULT now(), views integer NOT NULL DEFAULT 0, version integer NOT NULL DEFAULT 1, lock jsonb, title_tsv tsvector GENERATED ALWAYS AS (to_tsvector('simple', title)) STORED )`.execute(db); await sql`CREATE INDEX brews_authors_gin ON brews USING gin (authors)`.execute(db); await sql`CREATE INDEX brews_tags_gin ON brews USING gin (tags)`.execute(db); await sql`CREATE INDEX brews_title_tsv_gin ON brews USING gin (title_tsv)`.execute(db); await sql`CREATE INDEX brews_published_updated ON brews (published, updated_at DESC)`.execute(db); await sql`CREATE INDEX brews_google_id ON brews (google_id) WHERE google_id IS NOT NULL`.execute(db); await sql`CREATE INDEX brews_lock ON brews ((lock IS NOT NULL)) WHERE lock IS NOT NULL`.execute(db); await sql` CREATE TABLE notifications ( id uuid PRIMARY KEY DEFAULT uuidv7(), dismiss_key text NOT NULL UNIQUE, title text NOT NULL DEFAULT '', text text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), start_at timestamptz NOT NULL DEFAULT now(), stop_at timestamptz NOT NULL DEFAULT now() )`.execute(db); await sql` CREATE TABLE users ( id uuid PRIMARY KEY DEFAULT uuidv7(), username text NOT NULL UNIQUE, oidc_issuer text, oidc_sub text, email text, email_verified boolean NOT NULL DEFAULT false, display_name text, created_at timestamptz NOT NULL DEFAULT now(), last_login_at timestamptz, UNIQUE (oidc_issuer, oidc_sub) )`.execute(db); await sql`CREATE INDEX users_email ON users (email)`.execute(db); }; export const down = async (db)=>{ await sql`DROP TABLE IF EXISTS users`.execute(db); await sql`DROP TABLE IF EXISTS notifications`.execute(db); await sql`DROP TABLE IF EXISTS brews`.execute(db); };