peppa/packages/cli/legacy-pg-migrations/0011_pep58_incidents.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

62 lines
3.0 KiB
SQL

CREATE TABLE "error_sources" (
"id" serial PRIMARY KEY NOT NULL,
"project_id" integer NOT NULL,
"kind" text NOT NULL,
"bugsink_project_id" integer NOT NULL,
"dsn" text NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "error_sources_bugsink_project_id_unique" UNIQUE("bugsink_project_id")
);
--> statement-breakpoint
CREATE TABLE "incident_settings" (
"project_id" integer PRIMARY KEY NOT NULL,
"hook_secret" text NOT NULL,
"policy" text DEFAULT 'backlog' NOT NULL,
"triage_prompt" text,
"environments" jsonb DEFAULT '["prod","production"]'::jsonb NOT NULL,
"daily_limit" integer DEFAULT 5 NOT NULL,
"ignore_pattern" text,
"goal_id" integer,
"webhook_auto" boolean DEFAULT false NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "incident_settings_hook_secret_unique" UNIQUE("hook_secret")
);
--> statement-breakpoint
CREATE TABLE "incidents" (
"id" serial PRIMARY KEY NOT NULL,
"project_id" integer NOT NULL,
"source" text NOT NULL,
"bugsink_issue_id" text NOT NULL,
"friendly_id" text,
"title" text NOT NULL,
"culprit" text,
"level" text,
"environment" text,
"first_release" text,
"last_release" text,
"events" integer DEFAULT 1 NOT NULL,
"first_seen" timestamp with time zone NOT NULL,
"last_seen" timestamp with time zone NOT NULL,
"url" text,
"stack" text,
"status" text DEFAULT 'new' NOT NULL,
"decision" text,
"decided_by" text,
"decision_reason" text,
"task_id" integer,
"task_created_at" timestamp with time zone,
"resolved_release" text,
"resolved_at" timestamp with time zone,
"regressions" integer DEFAULT 0 NOT NULL,
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT "incidents_bugsink_issue_id_unique" UNIQUE("bugsink_issue_id")
);
--> statement-breakpoint
ALTER TABLE "error_sources" ADD CONSTRAINT "error_sources_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "incident_settings" ADD CONSTRAINT "incident_settings_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "incident_settings" ADD CONSTRAINT "incident_settings_goal_id_goals_id_fk" FOREIGN KEY ("goal_id") REFERENCES "public"."goals"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "incidents" ADD CONSTRAINT "incidents_project_id_projects_id_fk" FOREIGN KEY ("project_id") REFERENCES "public"."projects"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "incidents" ADD CONSTRAINT "incidents_task_id_tasks_id_fk" FOREIGN KEY ("task_id") REFERENCES "public"."tasks"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "error_sources_project_kind" ON "error_sources" USING btree ("project_id","kind");