-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[D1] initial
wrangler d1 export
(#5425)
* V1 of Wrangler support for D1 export * run prettier * update test, fix bug * don't read the config file twice * feat: implement `wrangler d1 export` --------- Co-authored-by: Glen Maddern <glen@cloudflare.com>
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"wrangler": minor | ||
--- | ||
|
||
feat: implement `wrangler d1 export` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import fs from "node:fs/promises"; | ||
import { fetch } from "undici"; | ||
import { printWranglerBanner } from ".."; | ||
import { fetchResult } from "../cfetch"; | ||
import { readConfig } from "../config"; | ||
import { UserError } from "../errors"; | ||
import { logger } from "../logger"; | ||
import { requireAuth } from "../user"; | ||
import { Name } from "./options"; | ||
import { getDatabaseByNameOrBinding } from "./utils"; | ||
import type { Config } from "../config"; | ||
import type { | ||
CommonYargsArgv, | ||
StrictYargsOptionsToInterface, | ||
} from "../yargs-types"; | ||
import type { Database } from "./types"; | ||
|
||
export function Options(yargs: CommonYargsArgv) { | ||
return Name(yargs) | ||
.option("local", { | ||
type: "boolean", | ||
describe: "Export from your local DB you use with wrangler dev", | ||
conflicts: "remote", | ||
}) | ||
.option("remote", { | ||
type: "boolean", | ||
describe: "Export from your live D1", | ||
conflicts: "local", | ||
}) | ||
.option("no-schema", { | ||
type: "boolean", | ||
describe: "Only output table contents, not the DB schema", | ||
conflicts: "no-data", | ||
}) | ||
.option("no-data", { | ||
type: "boolean", | ||
describe: | ||
"Only output table schema, not the contents of the DBs themselves", | ||
conflicts: "no-schema", | ||
}) | ||
.option("table", { | ||
type: "string", | ||
describe: "Specify which tables to include in export", | ||
}) | ||
.option("output", { | ||
type: "string", | ||
describe: "Which .sql file to output to", | ||
demandOption: true, | ||
}); | ||
} | ||
|
||
type HandlerOptions = StrictYargsOptionsToInterface<typeof Options>; | ||
export const Handler = async (args: HandlerOptions): Promise<void> => { | ||
const { local, remote, name, output, noSchema, noData, table } = args; | ||
await printWranglerBanner(); | ||
const config = readConfig(args.config, args); | ||
|
||
if (local) | ||
throw new UserError( | ||
`Local imports/exports will be coming in a future version of Wrangler.` | ||
); | ||
if (!remote) | ||
throw new UserError(`You must specify either --local or --remote`); | ||
|
||
// Allow multiple --table x --table y flags or none | ||
const tables: string[] = table | ||
? Array.isArray(table) | ||
? table | ||
: [table] | ||
: []; | ||
|
||
const result = await exportRemotely( | ||
config, | ||
name, | ||
output, | ||
tables, | ||
noSchema, | ||
noData | ||
); | ||
return result; | ||
}; | ||
|
||
type ExportMetadata = { | ||
signedUrl: string; | ||
}; | ||
|
||
async function exportRemotely( | ||
config: Config, | ||
name: string, | ||
output: string, | ||
tables: string[], | ||
noSchema?: boolean, | ||
noData?: boolean | ||
) { | ||
const accountId = await requireAuth(config); | ||
const db: Database = await getDatabaseByNameOrBinding( | ||
config, | ||
accountId, | ||
name | ||
); | ||
|
||
logger.log(`🌀 Executing on remote database ${name} (${db.uuid}):`); | ||
logger.log(`🌀 Creating export...`); | ||
const metadata = await fetchResult<ExportMetadata>( | ||
`/accounts/${accountId}/d1/database/${db.uuid}/export`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
outputFormat: "file", | ||
dumpOptions: { | ||
noSchema, | ||
noData, | ||
tables, | ||
}, | ||
}), | ||
} | ||
); | ||
|
||
logger.log(`🌀 Downloading SQL to ${output}`); | ||
const contents = await fetch(metadata.signedUrl); | ||
await fs.writeFile(output, contents.body || ""); | ||
logger.log(`Done!`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters