Skip to content

sqlx-fmt

Actions
Check if SQL code in sqlx macros needs formatting using sqruff
v0.3.0
Latest
Star (8)

sqlx-fmt

CI

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.

Installation

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 .

Usage

# format files
sqlx-fmt format --path path_to_files

# check formatting
sqlx-fmt check --path path_to_files

Note that files in the target directory are ignored.

Example

sqlx-fmt format --path ./src --config .sqruff
Example .sqruff config

sqruff config docs

[sqruff]
dialect = postgres
rules = ambiguous,capitalisation,convention,layout,references

[sqruff:indentation]
indent_unit = space
tab_space_size = 4
indented_joins = True

GitHub Action

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"

Inputs

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.

Development

TDD is encouraged! To run the tests, use cargo test (or cargo nextest run if you have cargo-nextest installed).

License

This project is licensed under the MIT License - see LICENSE file.

This project uses sqruff, which is licensed under the Apache License 2.0.

sqlx-fmt is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.

About

Check if SQL code in sqlx macros needs formatting using sqruff
v0.3.0
Latest

sqlx-fmt is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.