// The hot part of Peppa's schema (packages/db/src/schema.ts), in both dialects. // Timestamps are epoch milliseconds passed as params so the same SQL runs on both. const tables = (pg: boolean) => { const id = pg ? "serial primary key" : "integer primary key autoincrement"; const json = pg ? "jsonb" : "text"; const ts = pg ? "bigint" : "integer"; return [ `create table projects (id ${id}, key text not null unique, name text not null, next_task_number integer not null default 1, updated_at ${ts})`, `create table tasks (id ${id}, project_id integer not null references projects(id) on delete cascade, number integer not null, title text not null, status text not null default 'backlog', updated_at ${ts}, unique(project_id, number))`, `create table sessions (id ${id}, project_id integer not null references projects(id) on delete cascade, task_id integer references tasks(id) on delete set null, tmux_name text not null unique, generation integer not null default 0, auto_nudges integer not null default 0, status text not null default 'starting', last_activity_at ${ts}, started_at ${ts})`, `create table questions (id ${id}, project_id integer not null references projects(id) on delete cascade, task_id integer references tasks(id) on delete cascade, session_id integer references sessions(id) on delete set null, kind text not null, text text not null, options ${json}, answer text, answered_via text, telegram_message_id integer, awaiting_hook boolean not null default false, created_at ${ts}, answered_at ${ts})`, `create table events (id ${id}, project_id integer not null references projects(id) on delete cascade, task_id integer references tasks(id) on delete cascade, session_id integer references sessions(id) on delete set null, type text not null, payload ${json}, created_at ${ts})`, `create index events_task on events(task_id, id)`, `create table settings (key text primary key, value ${json} not null, updated_at ${ts})`, `create table telegram_outbox (id ${id}, dedupe_key text not null unique, method text not null, params ${json} not null, question_id integer references questions(id) on delete set null, status text not null default 'pending', attempts integer not null default 0, next_attempt_at ${ts} not null, telegram_message_id integer, created_at ${ts}, sent_at ${ts})`, `create index outbox_pending on telegram_outbox(status, next_attempt_at)`, `create table telegram_updates (update_id integer primary key, status text not null, created_at ${ts})`, ]; }; export const sqliteSchema = tables(false); export const pgSchema = tables(true); /** One project, `runners` tasks in progress, one live session per task. */ export function seedStmts(runners: number): [string, unknown[]][] { const now = Date.now(); const out: [string, unknown[]][] = [["insert into projects (key, name, next_task_number, updated_at) values ('PEP', 'Peppa', ?, ?)", [runners + 1, now]]]; for (let i = 1; i <= runners; i++) { out.push(["insert into tasks (project_id, number, title, status, updated_at) values (1, ?, ?, 'in_progress', ?)", [i, `Task ${i}`, now]]); out.push(["insert into sessions (project_id, task_id, tmux_name, generation, status, last_activity_at, started_at) values (1, ?, ?, 1, 'running', ?, ?)", [i, `peppa-PEP-${i}-s${i}`, now, now]]); } return out; }