Skip to content

Commit

Permalink
SQL Pre-Commit Hook (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnirmal authored Jul 10, 2024
1 parent 8f91c54 commit 86894fa
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
repos:
- repo: local
hooks:
- id: sqlfluff-changed-files
name: sqlfluff-changed-files
entry: ./dbt_scripts/run_sqlfluff_on_changed_files.sh
language: script
files: \.sql$
25 changes: 25 additions & 0 deletions .sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# .sqlfluff configuration file
[sqlfluff]
dialect = snowflake
templater = jinja

# General linting rules
[sqlfluff:rules]
max_line_length = 100 # Set maximum line length
tab_space_size = 4 # Define tab space size

# Specific rule configurations
[sqlfluff:rules:L003] # Indentation should be a multiple of 4 spaces
tab_space_size = 4

[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = upper

[sqlfluff:rules:capitalisation.identifiers] # Inconsistent capitalisation of unquoted identifiers
extended_capitalisation_policy = consistent

[sqlfluff:rules:references.consistent] # Avoid using aliases in join clauses
single_table_references = consistent

[sqlfluff:layout:type:comma]
line_position = leading
19 changes: 19 additions & 0 deletions dbt_scripts/run_sqlfluff_on_changed_files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# Get the list of changed SQL files
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.sql$')

if [ -z "$CHANGED_FILES" ]; then
echo "No changed SQL files to lint"
exit 0
fi

# Run SQLFluff on each changed SQL file
for file in $CHANGED_FILES; do
sqlfluff fix "$file"
if [ $? -ne 0 ]; then
echo "SQLFluff found issues in $file"
exit 1
fi
done

exit 0
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
{{ config(materialized="table") }}
select symbol, contract_address, num_decimals, coingecko_id, initial_supply
from
SELECT
symbol
, contract_address
, num_decimals
, coingecko_id
, initial_supply
FROM
(
values
('USDC', 'TEkxiTehnzSmSe2XqrBj4w32RUN966rdz8', 6, 'usd-coin', 0),
('USDT', 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', 6, 'tether', 0)
) as results(symbol, contract_address, num_decimals, coingecko_id, initial_supply)
VALUES
('USDC', 'TEkxiTehnzSmSe2XqrBj4w32RUN966rdz8', 6, 'usd-coin', 0)
, ('USDT', 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', 6, 'tether', 0)
)
AS results (
symbol, contract_address, num_decimals, coingecko_id, initial_supply
)
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ dbt-extractor==0.5.1
dbt-semantic-interfaces==0.4.3
dbt-snowflake==1.7.1
pandas==2.0.2
numpy==1.26.4
numpy==1.26.4
sqlfluff==3.1.0
pre-commit
3 changes: 3 additions & 0 deletions syncenv
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
python3 -m venv venv
. ./venv/bin/activate
pip install -r requirements.txt

chmod +x dbt_scripts/run_sqlfluff_on_changed_files.sh

source .env.local

Expand Down

0 comments on commit 86894fa

Please sign in to comment.