Skip to content

jflessau/sqlx-fmt

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

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

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.

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.

About

Format SQL in sqlx macros with sqruff.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages