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>
54 lines
3.0 KiB
TypeScript
54 lines
3.0 KiB
TypeScript
// PEP-36 prototype: copy a Peppa Postgres database into a fresh SQLite file using the
|
|
// ported schema, then read it back through drizzle and compare row counts.
|
|
// bun copy-from-pg.ts <postgres-url> <out.db>
|
|
// The Postgres session is read-only (default_transaction_read_only), so pointing it at
|
|
// the live database cannot change anything there.
|
|
import { Database } from "bun:sqlite";
|
|
import { drizzle } from "drizzle-orm/bun-sqlite";
|
|
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
|
|
import postgres from "postgres";
|
|
import * as schema from "./schema.ts";
|
|
|
|
const [url, out] = process.argv.slice(2);
|
|
if (!url || !out) throw new Error("usage: bun copy-from-pg.ts <postgres-url> <out.db>");
|
|
|
|
const pg = postgres(url, { max: 1, connection: { default_transaction_read_only: "on" } as any });
|
|
const file = new Database(out, { create: true });
|
|
file.exec("PRAGMA busy_timeout = 5000");
|
|
file.exec("PRAGMA journal_mode = WAL");
|
|
const db = drizzle(file, { schema });
|
|
migrate(db, { migrationsFolder: new URL("migrations", import.meta.url).pathname });
|
|
|
|
// Parents before children (foreign keys are enforced).
|
|
const order = [
|
|
"users", "projects", "project_members", "project_invites", "goals", "tasks", "sessions", "questions", "events",
|
|
"settings", "telegram_outbox", "telegram_updates", "oauth_clients", "oauth_codes", "oauth_tokens",
|
|
"incident_settings", "error_sources", "incidents",
|
|
];
|
|
const toSqlite = (v: unknown) =>
|
|
v instanceof Date ? v.getTime() : typeof v === "boolean" ? (v ? 1 : 0) : v !== null && typeof v === "object" ? JSON.stringify(v) : typeof v === "bigint" ? Number(v) : v;
|
|
|
|
file.exec("PRAGMA foreign_keys = ON");
|
|
const t0 = performance.now();
|
|
const counts: Record<string, [number, number]> = {};
|
|
for (const t of order) {
|
|
const rows = await pg.unsafe(`select * from ${t}`);
|
|
if (rows.length) {
|
|
const cols = Object.keys(rows[0]);
|
|
const ins = file.prepare(`insert into ${t} (${cols.map((c) => `"${c}"`).join(", ")}) values (${cols.map(() => "?").join(", ")})`);
|
|
file.transaction(() => {
|
|
for (const r of rows) ins.run(...(cols.map((c) => toSqlite(r[c])) as any[]));
|
|
})();
|
|
}
|
|
const [{ n }] = file.query(`select count(*) n from ${t}`).all() as { n: number }[];
|
|
counts[t] = [rows.length, n];
|
|
}
|
|
const ms = performance.now() - t0;
|
|
await pg.end();
|
|
|
|
// Read back through drizzle: relations, timestamps as Date, json as objects, enums as text.
|
|
const tasks = await db.query.tasks.findMany({ with: { project: true, sessions: true }, limit: 3 });
|
|
const q = await db.query.questions.findFirst({ where: (q, { isNotNull }) => isNotNull(q.options) });
|
|
const fk = file.query("PRAGMA foreign_key_check").all();
|
|
console.log(JSON.stringify({ ms: Math.round(ms), counts, mismatched: Object.entries(counts).filter(([, [a, b]]) => a !== b), fkViolations: fk.length, sample: { task: tasks[0] && { key: `${tasks[0].project.key}-${tasks[0].number}`, createdAt: tasks[0].createdAt, isDate: tasks[0].createdAt instanceof Date, sessions: tasks[0].sessions.length }, questionOptions: q?.options, optionsIsArray: Array.isArray(q?.options) } }, null, 2));
|