pre-commit-dbt
List of pre-commit hooks to ensure the quality of your dbt projects.
BETA NOTICE: This tool is still BETA and may have some bugs, so please be forgiving!
Goal
Quick ensure the quality of your dbt
projects.
dbt
is awesome, but when a number of models, sources, and macros grow it starts to be challenging to maintain quality. People often forget to update columns in schema files, add descriptions, or test. Besides, with the growing number of objects, dbt slows down, users stop running models/tests (because they want to deploy the feature quickly), and the demands on reviews increase.
If this is the case, pre-commit-dbt
is here to help you!
pre-commit-dbt
hooks
List of
Model checks:
check-column-desc-are-same
: Check column descriptions are the same.check-model-columns-have-desc
: Check the model columns have description.check-model-has-all-columns
: Check the model has all columns in the properties file.check-model-has-description
: Check the model has description.check-model-has-meta-keys
: Check the model has keys in the meta part.check-model-has-properties-file
: Check the model has properties file.check-model-has-tests-by-name
: Check the model has a number of tests by test name.check-model-has-tests-by-type
: Check the model has a number of tests by test type.check-model-has-tests
: Check the model has a number of tests.check-model-tags
: Check the model has valid tags.
Script checks:
check-script-semicolon
: Check the script does not contain a semicolon.check-script-has-no-table-name
: Check the script has not table name (is not usingsource()
orref()
macro for all tables).check-script-ref-and-source
: Check the script has only existing refs and sources.
Source checks:
check-source-columns-have-desc
: Check for source column descriptions.check-source-has-all-columns
: Check the source has all columns in the properties file.check-source-table-has-description
: Check the source table has description.check-source-has-freshness
: Check the source has the freshness.check-source-has-loader
: Check the source has loader option.check-source-has-meta-keys
: Check the source has keys in the meta part.check-source-has-tests-by-name
: Check the source has a number of tests by test name.check-source-has-tests-by-type
: Check the source has a number of tests by test type.check-source-has-tests
: Check the source has a number of tests.check-source-tags
: Check the source has valid tags.
Modifiers:
generate-missing-sources
: If any source is missing this hook tries to create it.generate-model-properties-file
: Generate model properties file.unify-column-description
: Unify column descriptions across all models.replace-script-table-names
: Replace table names withsource()
orref()
macros in the script.remove-script-semicolon
: Remove the semicolon at the end of the script.
dbt commands:
dbt-clean
: Rundbt clean
command.dbt-compile
: Rundbt compile
command.dbt-deps
: Rundbt deps
command.dbt-docs-generate
: Rundbt docs generate
command.dbt-run
: Rundbt run
command.dbt-test
: Rundbt test
command.
Install
For detailed installation and usage, instructions see pre-commit.com site.
pip install pre-commit
Setup
- Create a file named
.pre-commit-config.yaml
in yourdbt
root folder. - Add list of hooks you want to run befor every commit. E.g.:
repos:
- repo: https://github.com/offbi/pre-commit-dbt
rev: v0.1.1
hooks:
- id: check-script-semicolon
- id: check-script-has-no-table-name
- id: dbt-test
- id: dbt-docs-generate
- id: check-model-has-all-columns
name: Check columns - core
files: ^models/core
- id: check-model-has-all-columns
name: Check columns - mart
files: ^models/mart
- id: check-model-columns-have-desc
files: ^models/mart
- Optionally, run
pre-commit install
to set up the git hook scripts. With this,pre-commit
will run automatically ongit commit
! You can also manually runpre-commit run
after youstage
all files you want to run. Orpre-commit run --all-files
to run the hooks against all of the files (not onlystaged
).
Run as Github Action
Unfortunately, you cannot natively use pre-commit-dbt
if you are using dbt Cloud. But you can run checks after you push changes into Github.
To do that, make a file .github/workflows/pre-commit.yml
.
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
- uses: pre-commit/[email protected]
To run only changed files:
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: actions/[email protected]
- id: file_changes
uses: trilom/[email protected]
with:
output: ' '
- uses: pre-commit/[email protected]
with:
extra_args: --files ${{ steps.file_changes.outputs.files}}
To be able to run modifiers
you need to use only private repository and change your .github/workflows/pre-commit.yml
to:
name: pre-commit
on:
pull_request:
push:
branches: [main]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
with:
fetch-depth: 0
- uses: actions/[email protected]
- id: file_changes
uses: trilom/[email protected]
with:
output: ' '
- uses: pre-commit/[email protected]
with:
extra_args: --files ${{ steps.file_changes.outputs.files}}
token: ${{ secrets.GITHUB_TOKEN }}
For more informations about pre-commit/action
visit https://github.com/pre-commit/action.