Skip to content

Commit

Permalink
Updated D1 export progress to use spinnerWhile (#6247)
Browse files Browse the repository at this point in the history
  • Loading branch information
geelen authored Jul 16, 2024
1 parent eb201a3 commit 0045fad
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions packages/wrangler/src/d1/export.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs/promises";
import path from "node:path";
import { spinner, spinnerWhile } from "@cloudflare/cli/interactive";
import chalk from "chalk";
import { Miniflare } from "miniflare";
import { fetch } from "undici";
Expand Down Expand Up @@ -169,31 +170,41 @@ async function exportRemotely(
);

logger.log(`🌀 Executing on remote database ${name} (${db.uuid}):`);
logger.log(`🌀 Creating export...`);
const dumpOptions = {
noSchema,
noData,
tables,
};

const finalResponse = await pollExport(accountId, db, dumpOptions, undefined);
const s = spinner();
const finalResponse = await spinnerWhile<ExportPollingResponse>({
spinner: s,
promise: () => pollExport(s, accountId, db, dumpOptions, undefined),
startMessage: `Creating export`,
});

if (finalResponse.status !== "complete") {
throw new APIError({ text: `D1 reset before export completed!` });
}

logger.log(`🌀 Downloading SQL to ${output}...`);
logger.log(
chalk.gray(
`You can also download your export from the following URL manually. This link will be valid for one hour: ${finalResponse.result.signedUrl}`
)
);
const contents = await fetch(finalResponse.result.signedUrl);
await fs.writeFile(output, contents.body || "");
logger.log(`Done!`);

await spinnerWhile({
startMessage: `Downloading SQL to ${output}`,
async promise() {
const contents = await fetch(finalResponse.result.signedUrl);
await fs.writeFile(output, contents.body || "");
},
});
logger.log(`🌀 Downloaded to ${output} successfully!`);
}

async function pollExport(
s: ReturnType<typeof spinner>,
accountId: string,
db: Database,
dumpOptions: {
Expand Down Expand Up @@ -224,9 +235,9 @@ async function pollExport(
if (line.startsWith(`Uploaded part`)) {
// Part numbers can be reported as complete out-of-order which looks confusing to a user. But their ID has no
// special meaning, so just make them sequential.
logger.log(`🌀 Uploaded part ${++num_parts_uploaded}`);
s.update(`Uploaded part ${++num_parts_uploaded}`);
} else {
logger.log(`🌀 ${line}`);
s.update(line);
}
});

Expand All @@ -239,6 +250,7 @@ async function pollExport(
});
} else {
return await pollExport(
s,
accountId,
db,
dumpOptions,
Expand Down

0 comments on commit 0045fad

Please sign in to comment.