// One-off: copy applications + reviews from the site's Cloudflare D1 into Postgres. // 1. In GRVSummit: npx wrangler d1 export grv-summit --remote --no-schema --output d1.sql // 2. Here: pnpm db:import-d1 ../GRVSummit/d1.sql // Re-running is safe: existing ids are skipped. import { readFileSync } from 'node:fs'; import { PGlite } from '@electric-sql/pglite'; import { PrismaService } from '../src/prisma.service.js'; import type { Prisma } from '../src/generated/prisma/client.js'; const file = process.argv[2]; if (!file) throw new Error('Usage: pnpm db:import-d1 '); // Load the SQLite INSERTs into a scratch in-memory Postgres to parse them; the D1 types are all TEXT/INTEGER/REAL. const sql = readFileSync(file, 'utf8'); const scratch = new PGlite(); const ddl = readFileSync(new URL('../../GRVSummit/migrations/0001_applications.sql', import.meta.url), 'utf8') .replace(/INSERT INTO sectors[\s\S]*?;/, '') .replace(/INTEGER PRIMARY KEY/g, 'INTEGER PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY'); await scratch.exec(ddl); for (const stmt of sql.split(/;\s*\n/)) { if (/^\s*INSERT INTO "?(applications|reviews)"?/i.test(stmt)) await scratch.exec(stmt); } const prisma = new PrismaService(); const json = (v: unknown) => (v ? (JSON.parse(String(v)) as Prisma.InputJsonValue) : undefined); const date = (v: unknown) => (v ? new Date(String(v)) : undefined); const apps = (await scratch.query>('SELECT * FROM applications')).rows; let imported = 0; for (const a of apps) { if (await prisma.application.findUnique({ where: { id: String(a.id) } })) continue; await prisma.application.create({ data: { id: String(a.id), editionId: String(a.edition_id), resumeToken: String(a.resume_token), status: String(a.status) as never, email: String(a.email), founderName: a.founder_name as string | null, phone: a.phone as string | null, role: a.role as string | null, startupName: a.startup_name as string | null, website: a.website as string | null, city: a.city as string | null, yearFounded: a.year_founded == null ? null : Number(a.year_founded), coFounders: a.co_founders == null ? null : Number(a.co_founders), teamSize: a.team_size as string | null, stage: a.stage as string | null, primarySector: a.primary_sector as string | null, secondarySector: a.secondary_sector as string | null, otherSector: a.other_sector as string | null, referralSource: a.referral_source as string | null, businessPlanUrl: a.business_plan_url as string | null, pitchDeckUrl: a.pitch_deck_url as string | null, licenseUrl: a.license_url as string | null, licenseStatus: a.license_status as string | null, licenseNote: a.license_note as string | null, tin: a.tin as string | null, demoUrl: a.demo_url as string | null, product: json(a.product), mindset: json(a.mindset), goals: json(a.goals), consents: json(a.consents), createdAt: date(a.created_at), updatedAt: date(a.updated_at), submittedAt: date(a.submitted_at), }, }); imported += 1; } const reviews = (await scratch.query>('SELECT * FROM reviews')).rows; for (const r of reviews) { await prisma.review.upsert({ where: { applicationId_reviewer: { applicationId: String(r.application_id), reviewer: String(r.reviewer) } }, create: { id: String(r.id), applicationId: String(r.application_id), reviewer: String(r.reviewer), scores: json(r.scores) ?? {}, total: Number(r.total), comment: r.comment as string | null, createdAt: date(r.created_at), updatedAt: date(r.updated_at), }, update: {}, }); } console.log(`Imported ${imported}/${apps.length} applications, ${reviews.length} reviews.`); await prisma.$disconnect();