import { datasourceExamplestoSQL, datasourceItemsToSQL } from "./to-sql"; import fs from "node:fs/promises"; import { existsSync } from "node:fs"; import { join, dirname } from "node:path"; import type { Datasource } from "./types"; import { execa } from "execa"; export type DatasourceItemFromApi = { id: string; data: Record; createdAt?: string | null; updatedAt?: string | null; }; type FetchItems = (datasourceId: string) => Promise; type SeedOptions = { fetchItems?: FetchItems; persistTo?: string }; type Params = { WORKSPACE: string; WORKSPACE_TMP: string; fetchItems?: FetchItems; }; /** * Returns true if wrangler.json in the workspace declares at least one D1 binding. * Called before every preview-db operation to avoid "Couldn't find a D1 DB" errors * on workspaces that haven't had datasources configured yet. */ async function hasD1Configured(workspaceDir: string): Promise { try { const raw = await fs.readFile(join(workspaceDir, "wrangler.json"), "utf-8"); const cfg = JSON.parse(raw); return Array.isArray(cfg.d1_databases) && cfg.d1_databases.length > 0; } catch { return false; } } /** * Resolve the persist dir wrangler must write to so the preview-runtime miniflare * reads the same SQLite file. * * Resolution: explicit option > UPSTART_D1_PERSIST_DIR env var (set by the sandbox * process so subprocess invocations from skeleton scripts inherit it) > undefined * (wrangler defaults to {ws}/.wrangler/state/v3, NOT seen by our miniflare — only * usable for standalone CLI use). */ function resolvePersistTo(options?: { persistTo?: string }): string | undefined { return options?.persistTo ?? process.env.UPSTART_D1_PERSIST_DIR; } function escapeIdentifier(identifier: string): string { return `"${identifier.replace(/"/g, '""')}"`; } /** * Apply pending D1 migrations declared in the workspace's `.migrations/` folder * into the local SQLite file (same `--persist-to` as the seed so wrangler-CLI and * the preview-runtime miniflare share state). Idempotent: wrangler tracks applied * migrations in the `d1_migrations` table. * * Migration files are the SOLE authority for the schema (local and remote): the * local DB is never created out-of-band, which is why re-applying them never hits * the "DROP TABLE on a populated, FK-referenced table" failure. * * No-ops if `.migrations/` doesn't exist yet (fresh skeleton) or D1 isn't * configured. Throws on wrangler exit != 0 so the caller can decide what to do. */ export async function applyWranglerMigrations( WORKSPACE: string, options?: { persistTo?: string }, ): Promise { if (!(await hasD1Configured(WORKSPACE))) { console.log("[applyWranglerMigrations] No D1 database configured in wrangler.json yet, skipping."); return; } const migrationsDir = join(WORKSPACE, ".migrations"); if (!existsSync(migrationsDir)) { console.log("[applyWranglerMigrations] '.migrations' dir missing, skipping."); return; } const persistTo = resolvePersistTo(options); const wranglerConfig = join(WORKSPACE, "wrangler.json"); const args = ["exec", "wrangler", "d1", "migrations", "apply", "DB", "--local", "--config", wranglerConfig]; if (persistTo) { args.push("--persist-to", persistTo); } const result = await execa("pnpm", args, { cwd: WORKSPACE, reject: false, all: true }); if (result.exitCode !== 0) { console.error( "[applyWranglerMigrations] wrangler d1 migrations apply FAILED (exit %d):\n%s", result.exitCode, result.all, ); throw new Error(`applyWranglerMigrations: wrangler d1 migrations apply failed (exit ${result.exitCode})`); } } /** * Seed the local D1 with datasource DATA only — no schema. The schema is owned by * `applyWranglerMigrations`; callers must run that first (the tables must exist). * * For each datasource it clears the table (`DELETE FROM`, so a re-seed is * idempotent) and re-inserts either real items from the API (`fetchItems`) or the * static example fixtures. Everything runs in one `defer_foreign_keys` batch so * cross-datasource FK fixtures (e.g. `projects.categoryId → categories`) stay valid. * * No-ops silently if wrangler.json has no D1 binding or there are no datasources. */ export async function seedWranglerDb( WORKSPACE: string, WORKSPACE_TMP: string, options?: SeedOptions, ): Promise { if (!(await hasD1Configured(WORKSPACE))) { console.log("[seedWranglerDb] No D1 database configured in wrangler.json yet, skipping."); return; } const { tables, fixtures } = await getDatasourceSeed({ WORKSPACE, WORKSPACE_TMP, fetchItems: options?.fetchItems, }); if (tables.length === 0) { console.log("[seedWranglerDb] No datasources found, nothing to seed."); return; } // Clear every table first (defer_foreign_keys keeps the FK checks deferred to the // end of the batch), then re-insert fixtures/items. const statements: string[] = ["PRAGMA defer_foreign_keys = on;"]; statements.push(...tables.map((t) => `DELETE FROM ${escapeIdentifier(t)};`)); statements.push(...fixtures.map((f) => f.sql)); statements.push("PRAGMA defer_foreign_keys = off;"); const sql = statements.join("\n"); const tmpFile = join(WORKSPACE_TMP, "wrangler-seed.sql"); await fs.mkdir(dirname(tmpFile), { recursive: true }); await fs.writeFile(tmpFile, sql); const persistTo = resolvePersistTo(options); const wranglerConfig = join(WORKSPACE, "wrangler.json"); const args = [ "exec", "wrangler", "d1", "execute", "DB", "--local", "--yes", "--config", wranglerConfig, "--file", tmpFile, ]; if (persistTo) { args.push("--persist-to", persistTo); } const result = await execa("pnpm", args, { cwd: WORKSPACE, reject: false, all: true }); if (result.exitCode !== 0) { console.error("[seedWranglerDb] wrangler d1 execute FAILED (exit %d):\n%s", result.exitCode, result.all); throw new Error(`seedWranglerDb: wrangler d1 execute failed (exit ${result.exitCode})`); } if (result.stderr) { console.warn("[seedWranglerDb] wrangler d1 execute stderr:", result.stderr); } } /** * Bring the local preview D1 fully up to date: apply pending migrations (schema) * then seed data. This is the single entry point used at editor-open and by the * pre-commit hook; the schema always comes from the `.migrations/` files. */ export async function syncPreviewDb( WORKSPACE: string, WORKSPACE_TMP: string, options?: SeedOptions, ): Promise { await applyWranglerMigrations(WORKSPACE, options); await seedWranglerDb(WORKSPACE, WORKSPACE_TMP, options); } async function getDatasourceSeed(params: Params) { const tables: string[] = []; const fixtures: { datasource: string; sql: string }[] = []; for await (const file of fs.glob(`${params.WORKSPACE}/app/config/datasources/*.ts`)) { console.log("seedWranglerDb - processing datasource:", file); const mod = (await import(file)) as { default: Datasource }; const manifest = mod.default; tables.push(manifest.id); let examples: string | null = null; if (params.fetchItems) { const items = await params.fetchItems(manifest.id); if (items && items.length > 0) { console.log( `seedWranglerDb - using ${items.length} real item(s) from API for datasource: ${manifest.id}`, ); examples = datasourceItemsToSQL(manifest, items); } else { console.log( `seedWranglerDb - no items from API for datasource: ${manifest.id}, using static examples`, ); examples = datasourceExamplestoSQL(manifest); } } else { examples = datasourceExamplestoSQL(manifest); } if (examples !== null) { fixtures.push({ datasource: manifest.id, sql: examples }); } } return { tables, fixtures }; }