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(sage-monorepo): integrate SQL formatter and linter (ARCH-296) #2863

Merged
merged 7 commits into from
Sep 27, 2024

Conversation

tschaffter
Copy link
Member

@tschaffter tschaffter commented Sep 27, 2024

Closes https://sagebionetworks.jira.com/browse/ARCH-296

Changelog

  • Add and configure SQL plugin for Prettier to format SQL files
  • Add and configure SQLFluff to lint SQL files
  • Configure lint-staged to format and lint staged SQL files
  • Add and configure SQLFluff VS Code extension to the dev container
  • Configure SQLFluff to use the mysql dialect by default.
  • Demonstrate how to specify a dialect to SQLFluff per file.

Notes

  • SQLFluff configuration is defined in the root pyproject.toml since that's also where Black configuration is located. It is also possible to specify SQLFluff in other files, including .sqlfluff.
  • SQLFluff configuration supports inheritance (see docs). If needed, projects could extend the root configuration defined in pyproject.toml.
  • SQLFluff VS Code extension has a formatter and can format on save. I've disabled this feature because I'd prefer to use Prettier, which is more generalizable. A future task could be to evaluate how Prettier's plugin for SQL and SQLFluff format files.

References

Preview

Format SQL files on Save with Prettier

Before:

CREATE USER keycloak
WITH ENCRYPTED PASSWORD 'changeme';
CREATE DATABASE keycloak;
GRANT all PRIVILEGES ON DATABASE keycloak TO keycloak;

Upon saving:

CREATE USER keycloak
WITH
  ENCRYPTED PASSWORD 'changeme';

CREATE DATABASE keycloak;

GRANT all PRIVILEGES ON DATABASE keycloak TO keycloak;

Lint SQL files with SQLFluff CLI

$ poetry run sqlfluff lint apps/openchallenges/user-service/session.sql
== [apps/openchallenges/user-service/session.sql] FAIL                                                           
L:   3 | P:   1 | AM04 | Query produces an unknown number of result columns.
                       | [ambiguous.column_count]
L:   3 | P:   1 | LT09 | Select targets should be on a new line unless there is
                       | only one select target. [layout.select_targets]
L:   4 | P:   1 | LT02 | Expected indent of 4 spaces. [layout.indent]
L:   6 | P:   1 | LT02 | Expected indent of 4 spaces. [layout.indent]
L:  11 | P:   1 | LT02 | Expected indent of 4 spaces. [layout.indent]

Lint SQL files with SQLFluff VS Code extension

image

@tschaffter tschaffter self-assigned this Sep 27, 2024
@tschaffter tschaffter marked this pull request as ready for review September 27, 2024 04:01
@tschaffter tschaffter merged commit 9c1b90d into Sage-Bionetworks:main Sep 27, 2024
8 of 9 checks passed
@tschaffter tschaffter deleted the sage-monorepo/format-sql branch September 27, 2024 04:37
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.

1 participant