UNPKG

convex

Version:

Client for the Convex Cloud

128 lines (127 loc) 3.81 kB
"use strict"; import chalk from "chalk"; import path from "path"; import { bundleSchema } from "../../bundler/index.js"; import { version } from "../version.js"; import { changeSpinner, logFailure, logFinishedStep, logError } from "../../bundler/context.js"; import { poll, logAndHandleFetchError, deploymentFetch, fetchDeprecationCheckWarning } from "./utils.js"; export async function pushSchema(ctx, origin, adminKey, schemaDir, dryRun) { if (!ctx.fs.exists(path.resolve(schemaDir, "schema.ts"))) { return {}; } const bundles = await bundleSchema(ctx, schemaDir); changeSpinner(ctx, "Checking for index or schema changes..."); let data; const fetch = deploymentFetch(origin); try { const res = await fetch("/api/prepare_schema", { method: "POST", headers: { "Convex-Client": `npm-cli-${version}`, "Content-Type": "application/json" }, body: JSON.stringify({ bundle: bundles[0], adminKey, dryRun }) }); fetchDeprecationCheckWarning(ctx, res); data = await res.json(); } catch (err) { logFailure(ctx, `Error: Unable to run schema validation on ${origin}`); return await logAndHandleFetchError(ctx, err); } const schemaId = data.schemaId; changeSpinner( ctx, "Backfilling indexes and checking that documents match your schema..." ); const schemaState = await waitForReadySchema(ctx, origin, adminKey, schemaId); logIndexChanges(ctx, data, dryRun); return { schemaId, schemaState }; } async function waitForReadySchema(ctx, origin, adminKey, schemaId) { const path2 = `api/schema_state/${schemaId}`; const depFetch = deploymentFetch(origin); const fetch = async () => { try { const resp = await depFetch(path2, { headers: { Authorization: `Convex ${adminKey}`, "Convex-Client": `npm-cli-${version}`, "Content-Type": "application/json" } }); const data2 = await resp.json(); return data2; } catch (err) { logFailure( ctx, `Error: Unable to build indexes and run schema validation on ${origin}` ); return await logAndHandleFetchError(ctx, err); } }; const validate = (data2) => data2.indexes.every((index) => index.backfill.state === "done") && data2.schemaState.state !== "pending"; const data = await poll(fetch, validate); switch (data.schemaState.state) { case "failed": logFailure(ctx, "Schema validation failed"); logError(ctx, chalk.red(`${data.schemaState.error}`)); return await ctx.crash(1, { "invalid filesystem or db data": data.schemaState.tableName ?? null }); case "overwritten": logFailure(ctx, `Schema was overwritten by another push.`); return await ctx.crash(1, "fatal"); case "validated": logFinishedStep(ctx, "Schema validation complete."); break; case "active": break; } return data.schemaState; } function logIndexChanges(ctx, indexes, dryRun) { if (indexes.dropped.length > 0) { let indexDiff = ""; for (const index of indexes.dropped) { indexDiff += ` [-] ${stringifyIndex(index)} `; } indexDiff = indexDiff.slice(0, -1); logFinishedStep( ctx, `${dryRun ? "Would delete" : "Deleted"} table indexes: ${indexDiff}` ); } if (indexes.added.length > 0) { let indexDiff = ""; for (const index of indexes.added) { indexDiff += ` [+] ${stringifyIndex(index)} `; } indexDiff = indexDiff.slice(0, -1); logFinishedStep( ctx, `${dryRun ? "Would add" : "Added"} table indexes: ${indexDiff}` ); } } function stringifyIndex(index) { return `${index.table}.${index.name} ${JSON.stringify(index.fields)}`; } //# sourceMappingURL=indexes.js.map