// P2 data copy: SQLite → Postgres THROUGH the Drizzle client, so the camelCase // entity ↔ snake_case column mapping is exercised on every real row. Parents // (tasks) first for the FK. Idempotent-ish: assumes tables freshly created by // the drizzle-kit DDL (run pg schema apply first). // // DATABASE_URL=… STREAMLINE_DB_PATH=~/.streamline/data.sqlite bun scripts/pg_copy_drizzle.ts import { drizzle } from "drizzle-orm/bun-sql"; import { SQL } from "bun"; import { sql } from "drizzle-orm"; import { Database } from "bun:sqlite"; import { homedir } from "node:os"; import path from "node:path"; import * as schema from "../control-server/src/schema"; const SRC = process.env.STREAMLINE_DB_PATH || path.join(homedir(), ".streamline", "data.sqlite"); const client = new SQL(process.env.DATABASE_URL!); const db = drizzle({ client, schema, casing: "snake_case" }); const sq = new Database(SRC, { readonly: true }); // tasks first (parent of logs + fix_report_versions); rest in any order. const order = [ "tasks", "features", "security", "nodes", "settings", "node_clusters", "lark_users", "watcher_names", "readable_prompts", "readable_report_runs", "readable_batch_jobs", "readable_sources", "fix_report_versions", "evidence_media", "evidence_capture_events", "task_transitions", "logs", ] as const; let failures = 0; for (const name of order) { const table = (schema as Record)[name] as never; const rows = sq.query(`SELECT * FROM "${name}"`).all() as Record[]; if (!rows.length) { console.log(` ${name}: 0 rows`); continue; } const batch = 400; let copied = 0; for (let i = 0; i < rows.length; i += batch) { await db.insert(table).values(rows.slice(i, i + batch) as never); copied += Math.min(batch, rows.length - i); } console.log(` ${name}: copied ${copied}`); } // advance logs identity sequence past the copied max id await client.unsafe( "select setval(pg_get_serial_sequence('logs','id'), (select coalesce(max(id),1) from logs), true)"); // verify counts console.log("[copy] verifying counts…"); for (const name of order) { const s = (sq.query(`SELECT COUNT(*) c FROM "${name}"`).get() as { c: number }).c; const p = Number((await client.unsafe(`select count(*)::int c from "${name}"`))[0].c); const ok = s === p; if (!ok) failures++; console.log(` ${ok ? "✓" : "✗"} ${name}: sqlite=${s} pg=${p}`); } await client.end(); console.log(failures === 0 ? "\n[copy] DONE — counts match ✅" : `\n[copy] ${failures} MISMATCH ❌`); process.exit(failures === 0 ? 0 : 1);