Skip to content

Commit

Permalink
D1: execute --file --remote now uses dedicated import API (#5696)
Browse files Browse the repository at this point in the history
* D1: Refactored to remove batching entirely

This was only required for `execute --remote --file`, which will now have a dedicated endpoint.

* Adds R2-based SQL import into remote D1 DBs

This hits a new API endpoint 'import', that initially returns a signed R2 PUT url for uploading SQL, then returns polling info as the import completes. This completely bypasses the existing split-chunk-and-execute implementation that often left your DB in an invalid state

* D1 export: make --no-schema/--no-data work

Fixes #5445

* Extracted PollingFailure type

* Fixing json mode for remote DB execution
* Added test to cover binary sqlite3 file check

* Added spinners to the output during async operations

---------

Co-authored-by: Rahul Sethi <5822355+RamIdeas@users.noreply.github.com>
  • Loading branch information
geelen and RamIdeas committed May 16, 2024
1 parent 95a1fa1 commit 7e97ba8
Show file tree
Hide file tree
Showing 11 changed files with 406 additions and 156 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-files-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

feature: Improved `d1 execute --file --remote` performance & added support for much larger SQL files within a single transaction.
1 change: 1 addition & 0 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@
"jest": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"jest-websocket-mock": "^2.5.0",
"md5-file": "5.0.0",
"mime": "^3.0.0",
"minimatch": "^5.1.0",
"msw": "^0.49.1",
Expand Down
27 changes: 27 additions & 0 deletions packages/wrangler/src/__tests__/d1/execute.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from "node:fs";
import { join } from "path";
import { mockConsoleMethods } from "../helpers/mock-console";
import { useMockIsTTY } from "../helpers/mock-istty";
import { runInTempDir } from "../helpers/run-in-tmp";
Expand Down Expand Up @@ -87,4 +89,29 @@ describe("execute", () => {
)
);
});

it("should reject a binary SQLite DB", async () => {
setIsTTY(false);
writeWranglerToml({
d1_databases: [
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
],
});
const path = join(__dirname, "fixtures", "db.sqlite3");
fs.copyFileSync(path, "db.sqlite3");

await expect(
runWrangler(`d1 execute db --file db.sqlite3 --local --json`)
).rejects.toThrowError(
JSON.stringify(
{
error: {
text: "Provided file is a binary SQLite database file instead of an SQL text file. The execute command can only process SQL text files. Please export an SQL file from your SQLite database and try again.",
},
},
null,
2
)
);
});
});
Binary file not shown.
2 changes: 0 additions & 2 deletions packages/wrangler/src/d1/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const DEFAULT_MIGRATION_PATH = "./migrations";
export const DEFAULT_MIGRATION_TABLE = "d1_migrations";
export const LOCATION_CHOICES = ["weur", "eeur", "apac", "oc", "wnam", "enam"];
// Max number of statements to send in a single /execute call
export const DEFAULT_BATCH_SIZE = 10_000;

0 comments on commit 7e97ba8

Please sign in to comment.