Skip to content

feat(fmt): support SQL - #26750

Merged
bartlomieju merged 3 commits into
denoland:mainfrom
M4RC3L05:feature/fmt-sql
Nov 19, 2024
Merged

feat(fmt): support SQL#26750
bartlomieju merged 3 commits into
denoland:mainfrom
M4RC3L05:feature/fmt-sql

Conversation

@M4RC3L05

@M4RC3L05 M4RC3L05 commented Nov 5, 2024

Copy link
Copy Markdown
Contributor

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

CLAassistant commented Nov 5, 2024

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Comment thread cli/tools/fmt.rs
Comment on lines +544 to +543
lines_between_queries: 2,
uppercase: Some(true),

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

@bartlomieju bartlomieju Nov 6, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 :)

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

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

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

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.

Comment thread 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

M4RC3L05 commented Nov 6, 2024

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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 <15786310+M4RC3L05@users.noreply.github.com>
@bartlomieju

Copy link
Copy Markdown
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
Copy Markdown
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.

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

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.

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