-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
format native queries #38113
format native queries #38113
Conversation
2f018f8
to
712c1d8
Compare
712c1d8
to
cb092ea
Compare
|
frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx
Outdated
Show resolved
Hide resolved
frontend/src/metabase/query_builder/components/NativeQueryEditor/utils.ts
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,53 @@ | |||
import { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to verify
Ensure formatting works for SQL databases with the right dialect.
Ensure there is no formatting button for nosql databases.
Ensure the formatter library loads only when user clicks on the Format Query button.
These sound like very good test cases. Let's cover them!
keywordCase: "upper", | ||
linesBetweenQueries: 2, | ||
paramTypes: { | ||
// Snippets, parameters, nested questions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting does not work for queries with optional clauses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Fixed for engines excluding mssql and sqlite due to the lack of support for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall
frontend/src/metabase/query_builder/components/NativeQueryEditor/utils.ts
Show resolved
Hide resolved
frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
describe("scenarios > question > native", { tags: "@mongo" }, () => { | ||
const MONGO_DB_NAME = "QA Mongo4"; | ||
|
||
before(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's either use beforeEach
or let's lose the describe block
and put everything inside a single it()
block.
@@ -459,6 +459,49 @@ describe("no native access", { tags: ["@external", "@quarantine"] }, () => { | |||
}); | |||
}); | |||
|
|||
describe("scenarios > question > native", { tags: "@mongo" }, () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this section related specifically to the code formatting?
const query = question.query(); | ||
const engine = Lib.engine(query); | ||
const queryText = Lib.rawNativeQuery(query); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some inline (nit) comments but overall this is 🔥
oh god 49 is gonna be so good |
* format native queries * optional clauses, mlv2, specs * remove unnecessary webpack chunk name directive * disable formatting for mssql and sqlite, address review comments
Is it going to be available for BigQuery? |
@a-escontrela yes, it will be available for BigQuery as well. Some custom rules may be added later on but are excluded from this version |
@alxnddr great feature! Big fan of it except for the default setup of a indent of 2 spaces (VS 4 spaces). Only plan on making indent size configurable soon? |
|
||
return formatSql(queryText, { | ||
language: dialect, | ||
tabWidth: 2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would love to be able to edit that one
Closes #9142
Description
It is one of my projects for our internal hackathon. This PR adds format SQL query button.
Since it is a big package which is needed only for a single button, I put it in a separate chunk and import dynamically.
The button is hidden for nosql data sources such as Mongo, Druid, Google Analytics, and some sql databases such as sqlite and mssql for which optional clauses break formatting. For other SQL databases if the formatter library supports the specific dialect of given database it will use the dialcet otherwise it will fall back to ANSI SQL formatter.
How to verify
Ensure formatting works for SQL databases with the right dialect.
Ensure there is no formatting button for nosql databases.
Ensure the formatter library loads only when user clicks on the Format Query button.
Demo
sql-formatter.mov
Checklist