Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added d1 export --local support #6073

Merged
merged 1 commit into from
Jun 28, 2024
Merged

Added d1 export --local support #6073

merged 1 commit into from
Jun 28, 2024

Conversation

geelen
Copy link
Contributor

@geelen geelen commented Jun 18, 2024

This uses a new miniflare-only debug pragma PRAGMA miniflare_d1_export to trigger the dump as D1 provides no in-Worker API (which Miniflare uses) to generate exports.

I'm open to other ways to implement this.

What this PR solves / how to test

Fixes limitation with the previous iteration of the export functionality. Previously, all d1 export --local commands would fail.

Author has addressed the following

  • Tests
    • TODO (before merge)
    • Included
    • Not necessary because:
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required / Maybe required
    • Not required because: covered by local tests
  • Changeset (Changeset guidelines)
    • TODO (before merge)
    • Included
    • Not necessary because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Not necessary because: documentation doesn't say --local doesn't work, it just emphasises --remote (which still makes sense to be the default).
@geelen geelen requested review from a team as code owners June 18, 2024 06:54
Copy link

changeset-bot bot commented Jun 18, 2024

🦋 Changeset detected

Latest commit: 6f94501

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
miniflare Minor
wrangler Minor
@cloudflare/pages-shared Patch
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Jun 18, 2024

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9691836885/npm-package-wrangler-6073

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/6073/npm-package-wrangler-6073

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9691836885/npm-package-wrangler-6073 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9691836885/npm-package-create-cloudflare-6073 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9691836885/npm-package-cloudflare-kv-asset-handler-6073
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9691836885/npm-package-miniflare-6073
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9691836885/npm-package-cloudflare-pages-shared-6073
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/9691836885/npm-package-cloudflare-vitest-pool-workers-6073

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.62.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240620.0
workerd 1.20240620.1 1.20240620.1
workerd --version 1.20240620.1 2024-06-20

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

@geelen geelen force-pushed the glen/d1-export-local branch 3 times, most recently from 3025559 to b2ada25 Compare June 24, 2024 23:09
Copy link
Contributor

@justin-mp justin-mp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own education, does this PR mean that PRAGMA miniflare_d1_export(?,?,?); is part of our public API now? I don't think we want it to be so. How do we prevent people from writing that in their own queries?

packages/miniflare/src/workers/d1/dumpSql.ts Outdated Show resolved Hide resolved
packages/miniflare/src/workers/d1/dumpSql.ts Outdated Show resolved Hide resolved
}
}

if (!noSchema) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter that this query appears first in shell.c.in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it? This is supposed to match up with https://github.com/sqlite/sqlite/blob/105c20648e1b05839fd0638686b95f2e3998abcb/src/shell.c.in#L8472, i.e. directly after run_schema_dump_query (which dumps schema & data), it checks SHFLG_DumpDataOnly) is not set before exporting all ('index','trigger','view') rows of sqlite_schema

Have I got that wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you're correct.

runWrangler("d1 export db --local --output /tmp/test.sql")
).rejects.toThrowError(
`Local imports/exports will be coming in a future version of Wrangler.`
it("should handle local", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably add tests for the edge cases like quoted names adding the "IF NOT EXISTS" as well as properly quoting things like keywords.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have all these inside the D1 codebase, but see #6073 (comment) I think it makes sense for this file to live in a separate package with a whole suite of tests (as well as a test runner that runs native SQLite for comparison like the D1 suite does).

Imo that's a follow-up task. For the moment I'm just keeping the two implementations in sync manually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it could be a follow-up task.

@geelen
Copy link
Contributor Author

geelen commented Jun 27, 2024

For my own education, does this PR mean that PRAGMA miniflare_d1_export(?,?,?); is part of our public API now? I don't think we want it to be so. How do we prevent people from writing that in their own queries?

It's part of miniflare's implementation of D1, but not present anywhere else. It's not accessible from wrangler d1 execute --local since you can't pass parameters but technically if someone wrote it in a worker and passed in at least two params they'd get a sql dump in local dev (and an error in production).

We could refactor dumpSql in this PR to use the public D1 API rather than the internal DO one (and then wrangler export would just send lots of queries to the D1 rather than a single debug pragma), but that would mean it's much harder to reuse the specific dumpSql code that's present in D1's internal codebase. And so I've erred on the side of making it easier to keep them consistent and accept leaking an implementation detail.

Ideally, Miniflare would make it possible to do an end-run around the D1 shim and hit an object directly with an RPC call, but as far as I can find out that's not possible.

This uses a new miniflare-only debug pragma `PRAGMA miniflare_d1_export` to trigger the dump as D1 provides no in-Worker API (which Miniflare uses) to generate exports.
}
}

if (!noSchema) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you're correct.

runWrangler("d1 export db --local --output /tmp/test.sql")
).rejects.toThrowError(
`Local imports/exports will be coming in a future version of Wrangler.`
it("should handle local", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it could be a follow-up task.

@geelen geelen merged commit 7ed675e into main Jun 28, 2024
19 checks passed
@geelen geelen deleted the glen/d1-export-local branch June 28, 2024 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants