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

[D1] add a json option to execute #2627

Merged
merged 17 commits into from
Jan 27, 2023
Merged

[D1] add a json option to execute #2627

merged 17 commits into from
Jan 27, 2023

Conversation

rozenmd
Copy link
Contributor

@rozenmd rozenmd commented Jan 26, 2023

What this PR solves / how to test:

With a workaround, it's possible to make wrangler return almost clean JSON:

🌀 Mapping SQL input into an array of statements
🌀 Parsing 1 statements
🚣 Executed 1 command in 32.160559952259064ms
[
  {
    "results": [
      {
        "test1": "test",
        "test2": "test",
        "test3": 0,
        "test4": "test"
      }
    ],
    "success": true,
    "meta": {
      "duration": 32.160559952259064,
      "last_row_id": null,
      "changes": null,
      "served_by": "primary-fc09486d-3681-4906-a109-0b5e3d7737ab.db3",
      "internal_stats": null
    }
  }
]

We should just return JSON (and nothing else) when the user asks for it - hence --json

Before:

rozenmd@cflaptop test1 % npx wrangler d1 execute test --command="select * from customers"
▲ [WARNING] Processing wrangler.toml configuration:

    - D1 Bindings are currently in alpha to allow the API to evolve before general availability.
      Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
      Note: Run this command with the environment variable NO_D1_WARNING=true to hide this message
  
      For example: `export NO_D1_WARNING=true && wrangler <YOUR COMMAND HERE>`


--------------------
🚧 D1 is currently in open alpha and is not recommended for production data and traffic
🚧 Please report any bugs to https://github.com/cloudflare/wrangler2/issues/new/choose
🚧 To request features, visit https://community.cloudflare.com/c/developers/d1
🚧 To give feedback, visit https://discord.gg/cloudflaredev
--------------------

🌀 Mapping SQL input into an array of statements
🌀 Parsing 1 statements
🌀 Executing on test (xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx):
🚣 Executed 1 command in 11.846710999961942ms
┌────────────┬─────────────────────┬───────────────────┐
│ CustomerID │ CompanyName         │ ContactName       │
├────────────┼─────────────────────┼───────────────────┤
│ 1          │ Alfreds Futterkiste │ Maria Anders      │
├────────────┼─────────────────────┼───────────────────┤
│ 4          │ Around the Horn     │ Thomas Hardy      │
├────────────┼─────────────────────┼───────────────────┤
│ 11         │ Bs Beverages        │ Victoria Ashworth │
├────────────┼─────────────────────┼───────────────────┤
│ 13         │ Bs Beverages        │ Random Name       │
└────────────┴─────────────────────┴───────────────────┘

After:

rozenmd@cflaptop test1 % npx wrangler d1 execute test --command="select * from customers" --json
[
  {
    "results": [
      {
        "CustomerID": 1,
        "CompanyName": "Alfreds Futterkiste",
        "ContactName": "Maria Anders"
      },
      {
        "CustomerID": 4,
        "CompanyName": "Around the Horn",
        "ContactName": "Thomas Hardy"
      },
      {
        "CustomerID": 11,
        "CompanyName": "Bs Beverages",
        "ContactName": "Victoria Ashworth"
      },
      {
        "CustomerID": 13,
        "CompanyName": "Bs Beverages",
        "ContactName": "Random Name"
      }
    ],
    "success": true,
    "meta": {
      "duration": 1.662519000004977,
      "last_row_id": null,
      "changes": null,
      "served_by": "primary-xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.db3",
      "internal_stats": null
    }
  }
]

Associated docs issues/PR:

- [insert associated docs issue(s)/PR(s)]

Author has included the following, where applicable:

- [ ] Tests

  • Changeset

Reviewer has performed the following, where applicable:

- [ ] Checked for inclusion of relevant tests

  • Checked for inclusion of a relevant changeset
    - [ ] Checked for creation of associated docs updates
  • Manually pulled down the changes and spot-tested

Resolves #2415

@rozenmd rozenmd self-assigned this Jan 26, 2023
@changeset-bot
Copy link

changeset-bot bot commented Jan 26, 2023

🦋 Changeset detected

Latest commit: 7deb4b4

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

This PR includes changesets to release 1 package
Name Type
wrangler 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

@rozenmd rozenmd marked this pull request as ready for review January 26, 2023 14:17
@rozenmd rozenmd requested a review from a team as a code owner January 26, 2023 14:17
@codecov
Copy link

codecov bot commented Jan 26, 2023

Codecov Report

Merging #2627 (7876519) into main (04d8a31) will increase coverage by 0.02%.
The diff coverage is 6.66%.

❗ Current head 7876519 differs from pull request most recent head 7deb4b4. Consider uploading reports for the commit 7deb4b4 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2627      +/-   ##
==========================================
+ Coverage   73.32%   73.34%   +0.02%     
==========================================
  Files         159      159              
  Lines        9852     9853       +1     
  Branches     2627     2627              
==========================================
+ Hits         7224     7227       +3     
+ Misses       2628     2626       -2     
Impacted Files Coverage Δ
packages/wrangler/src/d1/execute.tsx 16.66% <6.66%> (-0.14%) ⬇️
packages/wrangler/src/publish/publish.ts 86.70% <0.00%> (ø)
packages/wrangler/src/git-client.ts 81.25% <0.00%> (+4.16%) ⬆️
...ackages/wrangler/src/__tests__/helpers/mock-bin.ts 100.00% <0.00%> (+5.26%) ⬆️

@github-actions
Copy link
Contributor

github-actions bot commented Jan 26, 2023

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/runs/4026409715/npm-package-wrangler-2627

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

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

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/runs/4026409715/npm-package-wrangler-2627 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/runs/4026409715/npm-package-cloudflare-pages-shared-2627

@rozenmd rozenmd requested a review from geelen January 26, 2023 16:53
@rozenmd rozenmd requested review from petebacondarwin and a team January 27, 2023 14:11
@penalosa
Copy link
Contributor

I'm a bit iffy on this being an execute specific flag—there have been requests for JSON output across the rest of Wrangler in the past, and that may be something to consider here?

Copy link
Contributor

@penalosa penalosa left a comment

Choose a reason for hiding this comment

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

There are a lot of checks for if(!json). Would setting the log level to "none" have the same effect?

packages/wrangler/src/d1/execute.tsx Outdated Show resolved Hide resolved
packages/wrangler/src/d1/execute.tsx Outdated Show resolved Hide resolved
@rozenmd
Copy link
Contributor Author

rozenmd commented Jan 27, 2023

I'm a bit iffy on this being an execute specific flag—there have been requests for JSON output across the rest of Wrangler in the past, and that may be something to consider here?

@penalosa - if it was decided to make --json a global wrangler flag, it'd still be preferable to implement them in each command individually to avoid a long running branch as each command's JSON output was implemented (IMO)

@rozenmd rozenmd requested review from penalosa and a team January 27, 2023 15:41
Copy link
Contributor

@penalosa penalosa left a comment

Choose a reason for hiding this comment

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

LGTM! Pending tests

packages/wrangler/src/d1/execute.tsx Outdated Show resolved Hide resolved
packages/wrangler/src/d1/execute.tsx Outdated Show resolved Hide resolved
@rozenmd rozenmd merged commit 6f0f2ba into main Jan 27, 2023
@rozenmd rozenmd deleted the rozenmd/d1-exec-json-output branch January 27, 2023 16:41
@github-actions github-actions bot mentioned this pull request Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🚀 Feature Request: quiet mode for wrangler
4 participants