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

feat(fmt): support SQL #26750

Merged
merged 3 commits into from
Nov 19, 2024
Merged

feat(fmt): support SQL #26750

merged 3 commits into from
Nov 19, 2024

Conversation

M4RC3L05
Copy link
Contributor

@M4RC3L05 M4RC3L05 commented Nov 5, 2024

This PR closes adds support for sql in deno fmt subcommand.
This PR was based on previous PRs adding formatters to other languages.
The idea of this PR is to see if it would be a good idea to support formatting sql files in deno.

Closes: #25024

First time working on any substantial rust code so bear with me :)

@CLAassistant
Copy link

CLAassistant commented Nov 5, 2024

CLA assistant check
All committers have signed the CLA.

Comment on lines +544 to +543
lines_between_queries: 2,
uppercase: Some(true),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be beneficial to control this options?
For me it would be a nice to have to control the uppercase, as i like my sql to be lowercase, but all good if not.

Copy link
Member

Choose a reason for hiding this comment

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

I think for the first pass we shouldn't have it; we also have no "knobs" for controlling options for HTML/CSS/YAML formatters for now.

@@ -0,0 +1,18 @@
select * from foo;
Copy link
Member

Choose a reason for hiding this comment

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

Could you actually move these files to tests/specs/fmt/sql instead? I didn't even know we had a test here and I think we should remove these altogether in favor of using spec tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is the idea to remove the tests in tests/integration/fmt_tests.rs?

Copy link
Member

@bartlomieju bartlomieju Nov 6, 2024

Choose a reason for hiding this comment

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

Yeah, I would remove them altogether, but we can do that in a follow up PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

other question, how does one run the spec tests? i tried cargo test spec::fmt::sql but did not work i think...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just noticed the README lol

Copy link
Member

Choose a reason for hiding this comment

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

cargo test specs::fmt::sql will work - notice the "s" in specs :)

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Looks good, I'm going to tentatively add a v2.1 milestone and discuss with the team during the next meeting.

@bartlomieju bartlomieju added this to the 2.1.0 milestone Nov 5, 2024
@M4RC3L05 M4RC3L05 force-pushed the feature/fmt-sql branch 2 times, most recently from 0b38765 to ae74f31 Compare November 6, 2024 01:40
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

Please update cli/schemas/config-file.v1.json to include fmt-sql in "unstable" option - you can search for fmt-component and put it next to it.

cli/tools/fmt.rs Outdated
Comment on lines 547 to 552
.to_owned();

// Add single new line to the end of file.
formatted_str.push_str("\n");

Ok(Some(formatted_str.to_owned()))
Copy link
Member

Choose a reason for hiding this comment

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

It seems we're allocating twice here. Do we really need to trim_end() and then push a new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It appers that sqlformat does not add a new line at the end and there is no option to control that.

In my opinion, it would preferably be better to have this supported by sqlformat directly, i did it here because it does not support.

Gonna open an issue with sqlformat to see if this is something they would like to have support for.

Copy link
Member

Choose a reason for hiding this comment

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

I guess it's fine then. Maybe just remove the first to_owned() since sqlformat already returns an owned String.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Already did.

@M4RC3L05
Copy link
Contributor Author

M4RC3L05 commented Nov 6, 2024

Please update cli/schemas/config-file.v1.json to include fmt-sql in "unstable" option - you can search for fmt-component and put it next to it.

Is there any code testes that need to be done here?

@bartlomieju
Copy link
Member

Please update cli/schemas/config-file.v1.json to include fmt-sql in "unstable" option - you can search for fmt-component and put it next to it.

Is there any code testes that need to be done here?

No, just updating the file is fine 👍

Signed-off-by: m4rc3l05 <[email protected]>
@bartlomieju
Copy link
Member

Discussed this internally during the CLI meeting; some team members are skeptical because of so-so abilities of the formatter - eg. @lucacasonato raised that this formatter doesn't handle RETURNING well or results in strange formatting of JOIN statements.

Not clear if we're gonna land it for Deno v2.1; but we'll open some issues with their repo to see if it can be improved.

@bartlomieju
Copy link
Member

I spoke with @lucacasonato about it and we tried formatting all SQL queries used in jsr repo: bartlomieju/try-deno-fmt-sql#1. I filed a couple issues with sqlformat-rs: shssoichiro/sqlformat-rs#65 and shssoichiro/sqlformat-rs#66.

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @M4RC3L05. Gonna land it as unstable for Deno v2.1.

@bartlomieju bartlomieju enabled auto-merge (squash) November 19, 2024 20:26
@bartlomieju bartlomieju merged commit c55e936 into denoland:main Nov 19, 2024
17 checks passed
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.

Feat: Support formatting sql files with deno fmt
3 participants