peppa/packages/cli/legacy-pg-migrations/0000_safe_shiver_man.sql
Асель 3a282b8054 PEP-36 stage 2: SQLite (node:sqlite, WAL) instead of Postgres
- packages/db: schema on sqlite-core (timestamps as epoch ms → Date, JSON as text,
  booleans 0/1), one baseline migration; node:sqlite client under drizzle's
  better-sqlite3 session (busy_timeout first, WAL, BEGIN IMMEDIATE, retries on
  SQLITE_BUSY, lazy open, async transaction callbacks refused); PEPPA_DB
  (default ~/.peppa/peppa.db)
- live updates: notify() pokes the bot over a unix socket; the bot also watches
  PRAGMA data_version every second (writes by other processes)
- Postgres specifics ported: createTask transaction is synchronous, no
  `for update skip locked`, `count(*)`, json_extract instead of ->>, json_each
  instead of @>
- `peppa db migrate` and `peppa db migrate-to-sqlite <file>` (read-only
  snapshot of Postgres, FK order from the schema, row count + foreign key
  checks, file appears only when they pass); old Postgres schema kept in
  packages/cli/legacy-pg-migrations for the migration and its test
- daily VACUUM INTO backup from the watchdog (7 copies, admin alert on failure)
- tests: a temp SQLite file per test file; storage.test.ts (20 processes,
  per-project numbering across processes, socket/data_version, migration from
  Postgres incl. a dangling foreign key, backups); two test races fixed
- CI: no per-run Postgres database; PEPPA_TEST_PG only for the migration test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 17:23:24 +05:00

94 lines
5.0 KiB
SQL

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");