SQLFluff hooks
Last updated
Last updated
Using pre-commit with SQLFluff is a good way to provide automated linting to SQL developers. With pre-commit, you also get the benefit of only linting/fixing the files that changed.
SQLFluff comes with two pre-commit hooks:
sqlfluff-lint
: returns linting errors.
sqlfluff-fix
: attempts to fix rule violations.
sqlfluff-lint
What it does Checks SQL files against SQLFluff rules to enforce consistent formatting and catch potential SQL anti-patterns.
When to use it Use this hook when you want to enforce consistent SQL style across your project. It's particularly useful for teams working on shared SQL codebases where maintaining consistent style is important.
Arguments
--dialect
: SQL dialect to use (e.g., postgres, mysql, snowflake)
--exclude-rules
: Comma-separated list of rule codes to exclude
--rules
: Comma-separated list of rule codes to check (defaults to all)
--processes
: Number of parallel processes to use
--ignore
: Comma-separated list of paths to ignore
Example
Requirements
SQLFluff installed (pip install sqlfluff
)
.sqlfluff
config file (optional but recommended)
How it works
Hook identifies all changed SQL files in your commit
Each file is checked against enabled SQLFluff rules
Fails if any rule violations are found
Provides detailed output of rule violations and suggested fixes
sqlfluff-fix
What it does Automatically fixes SQL files to conform to SQLFluff rules and formatting standards.
When to use it Use this hook when you want to automatically format SQL files according to your team's standards. It saves time by fixing common formatting issues before they're committed.
Arguments
--dialect
: SQL dialect to use (e.g., postgres, mysql, snowflake)
--exclude-rules
: Comma-separated list of rule codes to exclude from fixing
--rules
: Comma-separated list of rule codes to fix (defaults to all fixable rules)
--processes
: Number of parallel processes to use
--ignore
: Comma-separated list of paths to ignore
Example
What it does Enables SQLFluff to understand and lint dbt-specific SQL syntax, including dbt macros, ref/source functions, and Jinja templating.
When to use it Use this configuration when you're using SQLFluff in a dbt project. It allows SQLFluff to properly parse and lint SQL files that contain dbt-specific syntax and templating.
Configuration
Add the dbt templater as an additional dependency in your pre-commit config
Example
Then, configure the dbt templater in your .sqlfluff
To point pre-commit to use your custom .sqlfluff
rules file, you need to either:
Put the .sqlfluff
file in your repository root (default behavior), or
Specify a custom path in your pre-commit configuration
Example
The --config
argument tells SQLFluff where to find your rules configuration. The path should be relative to your repository root.
See also:
SQLFluffCreate a .sqlfluff
config file for project-specific rules. See also SQLFluff configurations
Run fix before lint to automatically resolve simple issues
Use --exclude-rules
for exceptions rather than disabling rules globally
Set appropriate line length limits for your team
Document any rule exclusions in your config file
Use consistent SQL dialects across your project