peppa/packages/shared/src/env.ts
Асель 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

41 lines
1.7 KiB
TypeScript

import { homedir } from "node:os";
import { z } from "zod";
const schema = z.object({
// Peppa's SQLite database file (PEP-36); default ~/.peppa/peppa.db.
PEPPA_DB: z.string().optional(),
// The old Postgres database: only the source of `peppa db migrate-to-sqlite`.
DATABASE_URL: z.string().optional(),
GITEA_URL: z.string().url().default("https://git.telecom.quest"),
GITEA_TOKEN: z.string().optional(),
GITEA_TEMPLATE_REPO: z.string().default("peppa-bot/template"),
PEPPA_WORKSPACES: z.string().default("~/peppa/workspaces"),
TELEGRAM_BOT_TOKEN: z.string().optional(),
TELEGRAM_PM_CHAT_ID: z.string().optional(),
PEPPA_WEB_URL: z.string().default("http://localhost:3100"),
PEPPA_SECRET: z.string().optional(),
GITEA_OAUTH_CLIENT_ID: z.string().optional(),
GITEA_OAUTH_CLIENT_SECRET: z.string().optional(),
});
export type Env = z.infer<typeof schema> & { PEPPA_DB: string };
export function loadEnv(source: NodeJS.ProcessEnv = process.env): Env {
const env = schema.parse(source);
env.PEPPA_WORKSPACES = env.PEPPA_WORKSPACES.replace(/^~(?=$|\/)/, homedir());
const db = (env.PEPPA_DB ?? `${source.PEPPA_HOME ?? `${homedir()}/.peppa`}/peppa.db`).replace(/^~(?=$|\/)/, homedir());
return { ...env, PEPPA_DB: db };
}
/** The Peppa checkout; bins, hooks and the template are resolved from it. */
export function peppaRoot(fallback?: string): string {
if (process.env.PEPPA_ROOT) return process.env.PEPPA_ROOT;
if (fallback) return fallback;
throw new Error("PEPPA_ROOT is not set");
}
/** Peppa's local state dir (logs, run files, caches). Tests point it elsewhere. */
export function peppaHome(): string {
return process.env.PEPPA_HOME ?? `${homedir()}/.peppa`;
}