Skip to content

Conversation

@rozenmd
Copy link
Contributor

@rozenmd rozenmd commented Feb 6, 2024

This PR defaults both wrangler d1 execute and wrangler d1 migrations apply to use the local development environment provided by wrangler to match the default behaviour in wrangler dev.

BREAKING CHANGE: wrangler d1 execute and wrangler d1 migrations apply now default --local to true. When running wrangler d1 execute or wrangler d1 migrations apply against a remote D1 database, you will need to provide the --remote flag.


Why?

wrangler dev used to default to remote preview Workers, and wrangler d1 execute/wrangler d1 migrations apply followed this practice. With wrangler v3, wrangler dev was updated without updating wrangler d1 execute/wrangler d1 migrations apply. This PR fixes this, and ensures wrangler dev and wrangler d1 execute/wrangler d1 migrations apply match again.

Closes STOR-2892 internally.


Preview

You can try this out via npx wrangler@d1. For example:

npx wrangler@d1 d1 execute <DBNAME> --command ="..."

What this looks like:

Local

npx wrangler@d1 d1 execute northwind-test --command="SELECT 1;"         
 ⛅️ wrangler 0.0.0-e4c703ba
---------------------------
🌀 Mapping SQL input into an array of statements
🌀 Executing on local database northwind-test (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) from .wrangler/state/v3/d1:
🌀 To execute on your remote database, add a --remote flag to your wrangler command.
┌───┐
│ 1 │
├───┤
│ 1 │
└───┘

Remote

 npx wrangler@d1 d1 execute northwind-test --command="SELECT 1;" --remote
 ⛅️ wrangler 0.0.0-e4c703ba
---------------------------
🌀 Mapping SQL input into an array of statements
🌀 Parsing 1 statements
🌀 Executing on remote database northwind-test (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx):
🌀 To execute on your local development database, remove the --remote flag from your wrangler command.
🚣 Executed 1 commands in 0.1596ms
┌───┐
│ 1 │
├───┤
│ 1 │
└───┘

Relevant docs PR: cloudflare/cloudflare-docs#13353

@changeset-bot
Copy link

changeset-bot bot commented Feb 6, 2024

🦋 Changeset detected

Latest commit: 8a0ae7a

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

This PR includes changesets to release 2 packages
Name Type
wrangler Minor
@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

@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 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/8189879018/npm-package-wrangler-4930

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

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

Or you can use npx with this latest build directly:

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

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


wrangler@3.31.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240223.1
workerd 1.20240304.0 1.20240304.0
workerd --version 1.20240304.0 2024-03-04

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

@codecov
Copy link

codecov bot commented Feb 6, 2024

Codecov Report

Attention: Patch coverage is 86.66667% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 70.46%. Comparing base (84eeee5) to head (8a0ae7a).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4930      +/-   ##
==========================================
+ Coverage   70.35%   70.46%   +0.11%     
==========================================
  Files         298      298              
  Lines       15567    15571       +4     
  Branches     4007     4008       +1     
==========================================
+ Hits        10952    10972      +20     
+ Misses       4615     4599      -16     
Files Coverage Δ
packages/wrangler/src/d1/migrations/apply.tsx 59.57% <100.00%> (ø)
packages/wrangler/src/d1/migrations/create.tsx 94.11% <ø> (ø)
packages/wrangler/src/d1/migrations/helpers.ts 94.73% <100.00%> (ø)
packages/wrangler/src/d1/migrations/list.tsx 71.79% <100.00%> (+0.74%) ⬆️
packages/wrangler/src/d1/migrations/options.ts 100.00% <ø> (ø)
packages/wrangler/src/d1/execute.tsx 60.38% <75.00%> (+0.12%) ⬆️

... and 6 files with indirect coverage changes

@rozenmd rozenmd added the breaking change Change that will result in breaking existing behavior label Feb 6, 2024
@rozenmd rozenmd marked this pull request as ready for review February 6, 2024 17:42
@rozenmd rozenmd requested review from a team as code owners February 6, 2024 17:42
@rozenmd rozenmd force-pushed the rozenmd/d1-execute-default-local branch 2 times, most recently from d7d697a to c60edc2 Compare February 13, 2024 13:01
@DaniFoldi
Copy link
Contributor

Hi 👋,

While I do agree with the local-first approach this PR proposes for D1, merging it without other products following will add additional complexity for users - which commands need --local, and which ones do --remote? Until now, it used to be
dev is local-first (since v3, gives a nice warning and usually not destructive anyways, so easy to fix), and storage products are remote first. I feel like one command is trying to accomplish too much, could it be moved to wrangler local d1 or wrangler prod d1?

cc @admah

@elithrar
Copy link
Contributor

could it be moved to wrangler local d1 or wrangler prod d1?

We should definitely avoid subcommand groups that further nest products. prod also has very different interpretations.

@rozenmd
Copy link
Contributor Author

rozenmd commented Feb 13, 2024

wrangler d1 execute is used by folks to load and interact with their databases during dev.

An extremely common point of confusion is that wrangler dev and wrangler d1 execute do not talk to the same environments any more (they used to before wrangler v3).

@DaniFoldi
Copy link
Contributor

DaniFoldi commented Feb 13, 2024

An extremely common point of confusion is that wrangler dev and wrangler d1 execute do not talk to the same environments any more

The same could be said about KV, for example:

We should definitely avoid subcommand groups that further nest products.

I agree that it wouldn't be the best solution, just an idea. Could also be wrangler-local, or a "switch" from remote to local mode that persists across commands, or something else. I'm just saying that wrangler d1 isn't the only one which is remote-first while dev isn't.

@joshthoward
Copy link
Contributor

Hey @DaniFoldi.

This is a good callout, but right now we have a decision to make before we go GA on whether we want to be inconsistent with KV or inconsistent with dev and the latter has led to substantial confusion among beta customers. Defaulting to local is also a much safer alternative because it reduces the likelihood that someone would inadvertently touch to production.

@vy-ton
Copy link

vy-ton commented Mar 5, 2024

@joshthoward @rozenmd If wrangler d1 execute defaults to local first, so should wrangler d1 migrations apply?

@joshthoward
Copy link
Contributor

@vy-ton is this not what happens? It's what Max included in the description.

@vy-ton
Copy link

vy-ton commented Mar 5, 2024

@joshthoward My migration executed remotely (I see the tables in dash)

 ⛅️ wrangler 3.31.0
-------------------
✔ Select an account › D1 Sandbox
Migrations to be applied:
┌───────────────────────┐
│ name                  │
├───────────────────────┤
│ 0000_create-table.sql │
└───────────────────────┘
✔ About to apply 1 migration(s)
Your database may not be available to serve requests during the migration, continue? … yes
🌀 Mapping SQL input into an array of statements
🌀 Parsing 30 statements
🌀 Executing on remote database duck (7b0fab2c-9ab5-4325-adb9-c3850548ebbc):
🌀 To execute on your local development database, pass the --local flag to 'wrangler d1 execute'
🚣 Executed 30 commands in 2.7105ms
┌───────────────────────┬────────┐
│ name                  │ status │
├───────────────────────┼────────┤
│ 0000_create-table.sql │ ✅       │
└───────────────────────┴────────┘```

@rozenmd
Copy link
Contributor Author

rozenmd commented Mar 5, 2024

@vy-ton @joshthoward this PR is not ready for review

@rozenmd rozenmd marked this pull request as draft March 5, 2024 22:20
@rozenmd rozenmd force-pushed the rozenmd/d1-execute-default-local branch from c60edc2 to 8733b9c Compare March 6, 2024 14:14
@rozenmd rozenmd marked this pull request as ready for review March 6, 2024 17:41
@rozenmd rozenmd requested a review from petebacondarwin March 6, 2024 17:41
@rozenmd rozenmd force-pushed the rozenmd/d1-execute-default-local branch from 771202e to 6e08564 Compare March 7, 2024 13:04
@rozenmd rozenmd requested review from a team and nora-soderlund and removed request for nora-soderlund March 7, 2024 17:16
@rozenmd rozenmd merged commit 2680462 into main Mar 11, 2024
@rozenmd rozenmd deleted the rozenmd/d1-execute-default-local branch March 11, 2024 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Change that will result in breaking existing behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants