peppa/bench/sqlite/schema-port/tx-check.ts
Асель 50b1d1afec PEP-36: benchmark SQLite vs Postgres and propose Bun + SQLite stack
bench/sqlite (outside the workspace): Peppa-like load with 10/20 runners,
a hook process per event, MCP, bot, web and watchdog; bun:sqlite,
node:sqlite, better-sqlite3, a single-writer service and Postgres; notify
replacements (cursor polling, fs.watch on WAL, unix-socket push); a
ceiling test; a schema port to sqlite with a data copy from Postgres and
a check of drizzle transactions.

openspec/changes/pep-36-lightweight-stack: proposal, design with the
numbers, specs (platform-storage, service-monitoring), phased tasks.
No product code changes; the move waits for the PM's decision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 16:28:59 +05:00

36 lines
1.8 KiB
TypeScript

// PEP-36 prototype: does drizzle's bun:sqlite driver keep `await` inside db.transaction()?
// core/tasks.ts (createTask) is written that way for postgres-js.
import { Database } from "bun:sqlite";
import { eq, sql } from "drizzle-orm";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import * as schema from "./schema.ts";
const db = drizzle(new Database(":memory:"), { schema });
migrate(db, { migrationsFolder: new URL("migrations", import.meta.url).pathname });
await db.insert(schema.projects).values({ slug: "p", key: "P", name: "P", giteaOwner: "o", giteaRepo: "r", workspacePath: "/tmp" });
// 1. Async callback, as in core/tasks.ts.
try {
await db.transaction(async (tx) => {
const [p] = await tx.update(schema.projects).set({ nextTaskNumber: sql`${schema.projects.nextTaskNumber} + 1` }).where(eq(schema.projects.id, 1)).returning();
await tx.insert(schema.tasks).values({ projectId: 1, number: p.nextTaskNumber - 1, title: "t" });
throw new Error("rollback please");
});
} catch (e) {
console.log("async tx threw:", (e as Error).message);
}
const [p1] = await db.select().from(schema.projects);
const t1 = await db.select().from(schema.tasks);
console.log("after async tx + throw: next_task_number =", p1.nextTaskNumber, "tasks =", t1.length, t1.length || p1.nextTaskNumber !== 1 ? "→ NOT rolled back" : "→ rolled back");
// 2. Sync callback (what the sqlite drivers expect).
try {
db.transaction((tx) => {
tx.update(schema.projects).set({ nextTaskNumber: 100 }).where(eq(schema.projects.id, 1)).run();
throw new Error("rollback please");
});
} catch {}
const [p2] = await db.select().from(schema.projects);
console.log("after sync tx + throw: next_task_number =", p2.nextTaskNumber, p2.nextTaskNumber === 100 ? "→ NOT rolled back" : "→ rolled back");