A CLI and GitHub Action to format SQL code within sqlx macros in Rust files using sqruff.
This is not an official sqlx project, just something I always wanted to have.
This project is a WIP.
Install sqruff if you haven't already: cargo install sqruff
Then install sqlx-fmt with:
git clone https://github.com/jflessau/sqlx-fmt.git
cd sqlx-fmt
cargo install --path .# format files
sqlx-fmt format --path path_to_files
# check formatting
sqlx-fmt check --path path_to_filessqlx-fmt format --path ./src --config .sqruffExample .sqruff config
[sqruff] dialect = postgres rules = ambiguous,capitalisation,convention,layout,references [sqruff:indentation] indent_unit = space tab_space_size = 4 indented_joins = True
Use the format checker as a step in GitHub Actions:
steps:
- name: checkout code
uses: actions/checkout@v4
- name: run format checker
uses: jflessau/sqlx-fmt@main
with:
context: "./code_to_format"
config-file: "./code_to_format/.sqruff"
fail-on-unformatted: "false"| Input | Required | Default | Description |
|---|---|---|---|
context |
no | . |
Path to the directory/file to format/check, e.g. ./src |
config-file |
no | .sqruff |
Path to the sqruff config file. Default config is used if the file is not found. |
fail-on-unformatted |
no | true |
If 'true', the action will fail if any unformatted files are found. |
TDD is encouraged! To run the tests, use cargo test.
This project is licensed under the MIT License - see LICENSE file.
This project uses sqruff, which is licensed under the Apache License 2.0.