CREATE TYPE "public"."goal_status" AS ENUM('open', 'done', 'archived');--> statement-breakpoint CREATE TYPE "public"."session_status" AS ENUM('starting', 'running', 'waiting_input', 'idle', 'stalled', 'done', 'failed');--> statement-breakpoint CREATE TYPE "public"."task_status" AS ENUM('backlog', 'proposing', 'awaiting_approval', 'in_progress', 'review', 'done', 'blocked');--> statement-breakpoint CREATE TABLE "events" ( "id" serial PRIMARY KEY NOT NULL, "project_id" integer NOT NULL, "task_id" integer, "session_id" integer, "type" text NOT NULL, "payload" jsonb, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "goals" ( "id" serial PRIMARY KEY NOT NULL, "project_id" integer NOT NULL, "title" text NOT NULL, "description" text, "status" "goal_status" DEFAULT 'open' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "projects" ( "id" serial PRIMARY KEY NOT NULL, "slug" text NOT NULL, "key" text NOT NULL, "name" text NOT NULL, "description" text, "gitea_owner" text NOT NULL, "gitea_repo" text NOT NULL, "default_branch" text DEFAULT 'main' NOT NULL, "workspace_path" text NOT NULL, "next_task_number" integer DEFAULT 1 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "projects_slug_unique" UNIQUE("slug"), CONSTRAINT "projects_key_unique" UNIQUE("key") ); --> statement-breakpoint CREATE TABLE "questions" ( "id" serial PRIMARY KEY NOT NULL, "project_id" integer NOT NULL, "task_id" integer, "session_id" integer, "kind" text NOT NULL, "text" text NOT NULL, "options" jsonb, "answer" text, "answered_via" text, "telegram_message_id" integer, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "answered_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "sessions" ( "id" serial PRIMARY KEY NOT NULL, "project_id" integer NOT NULL, "task_id" integer, "tmux_name" text NOT NULL, "claude_session_id" text, "status" "session_status" DEFAULT 'starting' NOT NULL, "last_activity_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "ended_at" timestamp with time zone, CONSTRAINT "sessions_tmux_name_unique" UNIQUE("tmux_name") ); --> statement-breakpoint CREATE TABLE "tasks" ( "id" serial PRIMARY KEY NOT NULL, "project_id" integer NOT NULL, "goal_id" integer, "number" integer NOT NULL, "title" text NOT NULL, "description" text, "status" "task_status" DEFAULT 'backlog' NOT NULL, "openspec_change" text, "pr_url" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "events" ADD CONSTRAINT "events_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "events" ADD CONSTRAINT "events_task_id_tasks_id_fk" FOREIGN KEY ("task_id") REFERENCES "public"."tasks"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "events" ADD CONSTRAINT "events_session_id_sessions_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."sessions"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "goals" ADD CONSTRAINT "goals_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "questions" ADD CONSTRAINT "questions_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "questions" ADD CONSTRAINT "questions_task_id_tasks_id_fk" FOREIGN KEY ("task_id") REFERENCES "public"."tasks"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "questions" ADD CONSTRAINT "questions_session_id_sessions_id_fk" FOREIGN KEY ("session_id") REFERENCES "public"."sessions"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sessions" ADD CONSTRAINT "sessions_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sessions" ADD CONSTRAINT "sessions_task_id_tasks_id_fk" FOREIGN KEY ("task_id") REFERENCES "public"."tasks"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tasks" ADD CONSTRAINT "tasks_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tasks" ADD CONSTRAINT "tasks_goal_id_goals_id_fk" FOREIGN KEY ("goal_id") REFERENCES "public"."goals"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "tasks_project_number" ON "tasks" USING btree ("project_id","number");