Skip to content

Commit

Permalink
chore: Setup linter and formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jo Lares authored and Jo Lares committed Feb 24, 2024
1 parent ca11073 commit a3af509
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 36 deletions.
Empty file added .env.local
Empty file.
37 changes: 37 additions & 0 deletions .sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[sqlfluff]
dialect = snowflake
templater = dbt
runaway_limit = 10
max_line_length = 80
indent_unit = space

[sqlfluff:indentation]
tab_space_size = 4

[sqlfluff:layout:type:comma]
spacing_before = touch
line_position = trailing

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

[sqlfluff:rules:aliasing.table]
aliasing = explicit

[sqlfluff:rules:aliasing.column]
aliasing = explicit

[sqlfluff:rules:aliasing.expression]
allow_scalar = False

[sqlfluff:rules:capitalisation.identifiers]
extended_capitalisation_policy = lower

[sqlfluff:rules:capitalisation.functions]
capitalisation_policy = lower

[sqlfluff:rules:capitalisation.literals]
capitalisation_policy = lower

[sqlfluff:rules:ambiguous.column_references] # Number in group by
group_by_and_order_by_style = implicit
5 changes: 5 additions & 0 deletions .sqlfluffignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
reports
target
dist
dbt_packages
macros
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pipeline as a SQL workflow using:
- Github Actions as pipeline runner, scheduler, and orchestrator.
- Python virtual environment.
- Snowflake or BigQuery as data warehouse
- [sqlfluff](https://sqlfluff.com/) for linting SQL files
- [sqlfmt](https://sqlfmt.com/) for formatting SQL files

## Getting started

Expand Down
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ config-version: 2
# This setting configures which "profile" dbt uses for this project.
profile: 'dbt_demo'

# These configurations specify where dbt should look for different types of files.
# These configurations specify WHERE dbt should look for different types of files.
# The `model-paths` config, for example, states that models in this project can be
# found in the "models/" directory. You probably won't need to change these!
model-paths: ["models"]
Expand Down
8 changes: 4 additions & 4 deletions docs/dbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dbt allows us to configure:

`tags` to support easy categorization and graph selection
custom schemas to split your models across multiple schemas
aliases if your view/table name should differ from the filename
aliases if your view/table name should differ FROM the filename
Snippets of SQL to run at the start or end of a model, known as hooks
Warehouse-specific configurations for performance (e.g. sort and dist keys on Redshift, partitions on BigQuery)

Expand Down Expand Up @@ -71,13 +71,13 @@ TODO: See https://docs.getdbt.com/docs/deploy/deployments#monitor-jobs-and-alert
- See dbt docs [Snowflake setup](https://docs.getdbt.com/docs/core/connect-data-platform/snowflake-setup#account)
- See Snowflake's [BEST PRACTICES FOR OPTIMIZING YOUR DBT AND SNOWFLAKE DEPLOYMENT](https://www.snowflake.com/wp-content/uploads/2021/10/Best-Practices-for-Optimizing-Your-dbt-and-Snowflake-Deployment.pdf)
- See post [Snowflake Queries: How Much Is Each One Costing You?](https://blog.sundeck.io/snowflake-queries-how-much-is-each-one-costing-you-83f40a61dd90)
- See Select post [Calculating cost per query in Snowflake](https://select.dev/posts/cost-per-query#complete-sql-query)
- See Select post [Calculating cost per query in Snowflake](https://SELECT.dev/posts/cost-per-query#complete-sql-query)

### Cost Monitoring tools

- See Snowflake docs on [Understanding overall cost](https://docs.snowflake.com/en/user-guide/cost-understanding-overall)
- See [Select](https://select.dev/docs/dbt-snowflake-monitoring)
- [dbt-snowflake-monitoring](https://github.com/get-select/dbt-snowflake-monitoring)
- See [Select](https://SELECT.dev/docs/dbt-snowflake-monitoring)
- [dbt-snowflake-monitoring](https://github.com/get-SELECT/dbt-snowflake-monitoring)

## Notes for BigQuery

Expand Down
6 changes: 6 additions & 0 deletions docs/git-branching-model.drawio.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Formats SQL files
sqlfmt */**/*.sql

# Formats Python files
# TODO: do not run if there is no python files
# black */**/*.py

# Format yml files
yamlfix */**/*.y*ml
1 change: 1 addition & 0 deletions lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sqlfluff lint */**/*.sql --dialect $DBT_WAREHOUSE_PROVIDER --disregard-sqlfluffignores
26 changes: 13 additions & 13 deletions models/example/my_first_dbt_model.sql
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@

/*
* Welcome to your first dbt model!
* Did you know that you can also configure models directly within SQL files?
* This will override configurations stated in dbt_project.yml
*
* Try changing "table" to "view" below
*/
{{ config(materialized="table") }}

{{ config(materialized='table') }}

WITH source_data AS (
with
source_data as (

SELECT 1 AS id
UNION ALL
SELECT null AS id
select 1 as id
union all
select null as id

)
)

SELECT *
FROM source_data
select *
from
source_data

/*
/*
* Uncomment the line below to remove records WITH null `id` values
*/

-- WHERE id IS NOT null
-- WHERE id IS NOT null
8 changes: 2 additions & 6 deletions models/example/my_second_dbt_model.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@

-- Use the `ref` function to select from other models

SELECT *
FROM {{ ref('my_first_dbt_model') }}
WHERE id = 1
-- Use the `ref` function to SELECT FROM other models
select * from {{ ref("my_first_dbt_model") }} where id = 1
19 changes: 7 additions & 12 deletions models/example/schema.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@

---
version: 2

models:
- name: my_first_dbt_model
description: "A starter dbt model"
description: A starter dbt model
columns:
- name: id
description: "The primary key for this table"
tests:
- unique

description: The primary key for this table
tests: [unique]
- name: my_second_dbt_model
description: "A starter dbt model"
description: A starter dbt model
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
description: The primary key for this table
tests: [unique, not_null]
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sqlfluff-templater-dbt==2.3.5
shandy-sqlfmt[jinjafmt]
black==24.2.0
yamlfix==1.16.0
2 changes: 2 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ fi
# Confirm that the dbt installation was successful.
dbt --version

python -m pip install -r requirements.txt

# dbt init $DBT_PROJECT_NAME

##
Expand Down

0 comments on commit a3af509

Please sign in to comment.