peppa/packages/db/migrations/0000_baseline.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

313 lines
13 KiB
SQL

CREATE TABLE `check_runs` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`task_id` integer,
`sha` text NOT NULL,
`branch` text,
`pr_number` integer,
`check` text NOT NULL,
`state` text NOT NULL,
`run_url` text,
`log_tail` text,
`duration_sec` integer,
`source` text DEFAULT 'report' NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`task_id`) REFERENCES `tasks`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `check_runs_project_sha_check` ON `check_runs` (`project_id`,`sha`,`check`);--> statement-breakpoint
CREATE TABLE `error_sources` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`kind` text NOT NULL,
`bugsink_project_id` integer NOT NULL,
`dsn` text NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `error_sources_bugsink_project_id_unique` ON `error_sources` (`bugsink_project_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `error_sources_project_kind` ON `error_sources` (`project_id`,`kind`);--> statement-breakpoint
CREATE TABLE `events` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`task_id` integer,
`session_id` integer,
`type` text NOT NULL,
`payload` text,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`task_id`) REFERENCES `tasks`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE TABLE `goals` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`title` text NOT NULL,
`description` text,
`status` text DEFAULT 'open' NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade
);
--> 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` text DEFAULT '["prod","production"]' NOT NULL,
`daily_limit` integer DEFAULT 5 NOT NULL,
`ignore_pattern` text,
`goal_id` integer,
`webhook_auto` integer DEFAULT false NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`goal_id`) REFERENCES `goals`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `incident_settings_hook_secret_unique` ON `incident_settings` (`hook_secret`);--> statement-breakpoint
CREATE TABLE `incidents` (
`id` integer PRIMARY KEY AUTOINCREMENT 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` integer NOT NULL,
`last_seen` integer 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` integer,
`resolved_release` text,
`resolved_at` integer,
`regressions` integer DEFAULT 0 NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`task_id`) REFERENCES `tasks`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `incidents_bugsink_issue_id_unique` ON `incidents` (`bugsink_issue_id`);--> statement-breakpoint
CREATE TABLE `oauth_clients` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`redirect_uris` text NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `oauth_codes` (
`code_hash` text PRIMARY KEY NOT NULL,
`client_id` text NOT NULL,
`user_id` integer NOT NULL,
`redirect_uri` text NOT NULL,
`code_challenge` text NOT NULL,
`scope` text,
`expires_at` integer NOT NULL,
`used_at` integer,
FOREIGN KEY (`client_id`) REFERENCES `oauth_clients`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `oauth_tokens` (
`token_hash` text PRIMARY KEY NOT NULL,
`kind` text NOT NULL,
`client_id` text NOT NULL,
`user_id` integer NOT NULL,
`scope` text,
`expires_at` integer NOT NULL,
`revoked_at` integer,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`last_used_at` integer,
FOREIGN KEY (`client_id`) REFERENCES `oauth_clients`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `project_invites` (
`project_id` integer NOT NULL,
`login` text NOT NULL,
`invited_by` integer,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
PRIMARY KEY(`project_id`, `login`),
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`invited_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE TABLE `project_members` (
`project_id` integer NOT NULL,
`user_id` integer NOT NULL,
`added_by` integer,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
PRIMARY KEY(`project_id`, `user_id`),
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`added_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE TABLE `projects` (
`id` integer PRIMARY KEY AUTOINCREMENT 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,
`owner_id` integer,
`next_task_number` integer DEFAULT 1 NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `projects_slug_unique` ON `projects` (`slug`);--> statement-breakpoint
CREATE UNIQUE INDEX `projects_key_unique` ON `projects` (`key`);--> statement-breakpoint
CREATE TABLE `questions` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`task_id` integer,
`session_id` integer,
`kind` text NOT NULL,
`text` text NOT NULL,
`options` text,
`answer` text,
`answered_via` text,
`telegram_message_id` integer,
`awaiting_hook` integer DEFAULT false NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`answered_at` integer,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`task_id`) REFERENCES `tasks`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`session_id`) REFERENCES `sessions`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE TABLE `releases` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`env` text NOT NULL,
`sha` text NOT NULL,
`previous_sha` text,
`image` text,
`host` text,
`pr_number` integer,
`tasks` text DEFAULT '[]' NOT NULL,
`status` text DEFAULT 'deploying' NOT NULL,
`deployed_by` text DEFAULT 'ci' NOT NULL,
`reason` text,
`run_url` text,
`log_tail` text,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `sessions` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`task_id` integer,
`tmux_name` text NOT NULL,
`provider` text DEFAULT 'claude' NOT NULL,
`claude_session_id` text,
`generation` integer DEFAULT 0 NOT NULL,
`auto_nudges` integer DEFAULT 0 NOT NULL,
`resume_at` integer,
`limit_probe_min` integer,
`limit_resumes` integer DEFAULT 0 NOT NULL,
`limit_resumes_day` text,
`limit_resumed_at` integer,
`status` text DEFAULT 'starting' NOT NULL,
`last_activity_at` integer,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`ended_at` integer,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`task_id`) REFERENCES `tasks`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `sessions_tmux_name_unique` ON `sessions` (`tmux_name`);--> statement-breakpoint
CREATE TABLE `settings` (
`key` text PRIMARY KEY NOT NULL,
`value` text NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `tasks` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`goal_id` integer,
`number` integer NOT NULL,
`title` text NOT NULL,
`description` text,
`status` text DEFAULT 'backlog' NOT NULL,
`openspec_change` text,
`pr_url` text,
`ci_attempts` integer DEFAULT 0 NOT NULL,
`ci_failed_sha` text,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`updated_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`goal_id`) REFERENCES `goals`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `tasks_project_number` ON `tasks` (`project_id`,`number`);--> statement-breakpoint
CREATE TABLE `telegram_outbox` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`dedupe_key` text NOT NULL,
`method` text NOT NULL,
`params` text NOT NULL,
`question_id` integer,
`status` text DEFAULT 'pending' NOT NULL,
`attempts` integer DEFAULT 0 NOT NULL,
`next_attempt_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`telegram_message_id` integer,
`error` text,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`sent_at` integer,
FOREIGN KEY (`question_id`) REFERENCES `questions`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `telegram_outbox_dedupe_key_unique` ON `telegram_outbox` (`dedupe_key`);--> statement-breakpoint
CREATE TABLE `telegram_updates` (
`update_id` integer PRIMARY KEY NOT NULL,
`status` text NOT NULL,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `users` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`gitea_id` integer NOT NULL,
`login` text NOT NULL,
`name` text,
`email` text,
`avatar_url` text,
`access_token_enc` text,
`refresh_token_enc` text,
`token_expires_at` integer,
`claude_token_enc` text,
`claude_token_set_at` integer,
`telegram_chat_id` integer,
`telegram_user_id` integer,
`telegram_username` text,
`telegram_paired_at` integer,
`created_at` integer DEFAULT (cast(unixepoch('subsec') * 1000 as integer)) NOT NULL,
`last_login_at` integer
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_gitea_id_unique` ON `users` (`gitea_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_login_unique` ON `users` (`login`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_telegram_chat_id_unique` ON `users` (`telegram_chat_id`);