AI Engineering Workspace
PartialChat, files, terminal and preview in one place. Chat sessions run on a durable Postgres-backed queue (chat only: no tools or shell). Live models need an API key; the terminal is unavailable.
Project files
read-onlyapp/scripts/migrate-sqlite-to-pg.mjs
1// SQLite -> PostgreSQL migration with verification. Idempotent (ON CONFLICT DO NOTHING). Never modifies or deletes the SQLite file.2// Usage (from app/): node scripts/migrate-sqlite-to-pg.mjs3import fs from "node:fs"; import path from "node:path"; import os from "node:os"; import crypto from "node:crypto";4import pg from "pg";5pg.types.setTypeParser(20, v => Number(v));6const sqlite = process.getBuiltinModule("node:sqlite");7const SRC = path.resolve(process.env.STUDIO_DATA_DIR || "data", "studio.db");8if (!fs.existsSync(SRC)) { console.error("no SQLite DB at", SRC); process.exit(2); }910// 1) Backup (consistent snapshot via VACUUM INTO)11const bdir = path.resolve(path.dirname(SRC), "backups"); fs.mkdirSync(bdir, { recursive: true });12const backup = path.join(bdir, `studio-${new Date().toISOString().replace(/[:.]/g, "-")}.db`);13const src = new sqlite.DatabaseSync(SRC, { readOnly: false }); src.exec(`VACUUM INTO '${backup.replace(/'/g, "''")}'`);14fs.chmodSync(backup, 0o600);1516const pw = fs.readFileSync(path.join(os.homedir(), ".config/zoo-cloud-studio/pg-password"), "utf8").trim();17const client = new pg.Client({ connectionString: `postgres://studio:${encodeURIComponent(pw)}@127.0.0.1:5432/studio` }); await client.connect();18const dbts = fs.readFileSync(path.resolve("src/lib/db.ts"), "utf8"); // single source of truth for the schema19await client.query(dbts.match(/export const SCHEMA = `([\s\S]*?)`;/)[1]);2021const TABLES = [22 ["projects", ["id", "slug", "name", "status", "data", "created_at", "updated_at"], "id"],23 ["tasks", ["id", "project_id", "title", "done", "created_at"], "id"],24 ["deployments", ["id", "version", "commit_sha", "status", "env", "url", "health", "log", "started_at", "finished_at"], "id"],25 ["audit_log", ["seq", "ts", "actor", "action", "resource", "detail", "prev_hash", "hash"], "seq"],26 ["settings", ["key", "value"], "key"],27];28const report = { at: new Date().toISOString(), backup: path.basename(backup), tables: {}, ok: true, notes: ["sessions are intentionally not migrated (ephemeral); users sign in again"] };29await client.query("BEGIN");30try {31 for (const [t, cols, pk] of TABLES) {32 const rows = src.prepare(`SELECT ${cols.join(",")} FROM ${t} ORDER BY ${pk}`).all();33 for (const r of rows) {34 await client.query(`INSERT INTO ${t}(${cols.join(",")}) VALUES(${cols.map((_, i) => `$${i + 1}`).join(",")}) ON CONFLICT(${pk}) DO NOTHING`, cols.map(c => r[c] ?? null));35 }36 if (t === "audit_log") await client.query("SELECT setval(pg_get_serial_sequence('audit_log','seq'), COALESCE((SELECT MAX(seq) FROM audit_log),1))");37 // Verification: row count + order-stable content checksum on both sides38 const sum = rowsx => crypto.createHash("sha256").update(JSON.stringify(rowsx)).digest("hex");39 const a = src.prepare(`SELECT ${cols.join(",")} FROM ${t} ORDER BY ${pk}`).all().map(r => cols.map(c => r[c] ?? null));40 const b = (await client.query(`SELECT ${cols.join(",")} FROM ${t} WHERE ${pk} = ANY($1::${pk === "seq" ? "bigint" : "text"}[]) ORDER BY ${pk}`, [a.map(r => r[cols.indexOf(pk)])])).rows.map(r => cols.map(c => r[c] ?? null));41 const total = (await client.query(`SELECT COUNT(*)::int c FROM ${t}`)).rows[0].c;42 const rep = { sqlite: a.length, postgres_matching: b.length, postgres_total: total, checksum_equal: sum(a) === sum(b) };43 if (rep.sqlite !== rep.postgres_matching || !rep.checksum_equal) report.ok = false;44 report.tables[t] = rep;45 }46 // Audit hash chain must verify in Postgres47 const ar = (await client.query("SELECT * FROM audit_log ORDER BY seq")).rows; let prev = "GENESIS", chain = true;48 for (const r of ar) { const h = crypto.createHash("sha256").update([prev, r.ts, r.actor, r.action, r.resource, r.detail].join("|")).digest("hex"); if (r.prev_hash !== prev || r.hash !== h) { chain = false; break; } prev = r.hash; }49 report.audit_chain_verified = chain; if (!chain) report.ok = false;50 if (!report.ok) throw new Error("verification failed");51 await client.query("COMMIT");52} catch (e) { await client.query("ROLLBACK"); report.ok = false; report.error = String(e.message); }53await client.end();54fs.mkdirSync("../design/verification", { recursive: true });55fs.writeFileSync("../design/verification/migration.json", JSON.stringify(report, null, 2));56console.log(JSON.stringify(report, null, 2)); process.exit(report.ok ? 0 : 1);57
Conversation
PartialSign in to start AI sessions.
Task checklist
- Application shell deployed
- Persistent projects
- Agent runtime
- Sandboxed terminal
Execution activity
No agent sessions. Git HEAD 692573d on master.
Approval requests
None pending.