Skip to content

Latest commit

 

History

History
1607 lines (1125 loc) · 57.3 KB

HOOKS.md

File metadata and controls

1607 lines (1125 loc) · 57.3 KB

List of pre-commit-dbt hooks

💡 Click on hook name to view the details.

Model checks:

Script checks:

Source checks:

Macro checks:

Modifiers:

dbt commands:


If you have an idea for a new hook or you found a bug, let us know

Available Hooks

check-column-desc-are-same

Check the models have the same descriptions for the same column names.

Arguments

--ignore: columns for which do not check whether have a different description.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-column-desc-are-same

When to use it

E.g. in two of your models, you have customer_id with the description This is cutomer_id, but there is one model where column customer_id has a description Something else. This hook finds discrepancies between column descriptions.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed since it also validates properties files ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml and SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • Modified yml files are scanned for a model.
  • If any column in the found model has different descriptions than others, the hook fails.
  • The description must be in either the yml file or the manifest.

check-model-columns-have-desc

Ensures that the model has columns with descriptions in the properties file (usually schema.yml).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-columns-have-desc

When to use it

You want to make sure that all specified columns in the properties files (usually schema.yml) have some description. This hook does not validate if all database columns are also present in a properties file.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed since it also validates properties files ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml and SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • Modified yml files are scanned for a model.
  • If any column in the found model does not contain a description, the hook fails.
  • The description must be in either the yml file or the manifest.

Known limitations

If you run your model and then you delete column description from a properties file, the hook success since the description is still present in manifest.json.


check-model-has-all-columns

Ensures that all columns in the database are also specified in the properties file. (usually schema.yml).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--catalog: location of catalog.json file. Usually target/catalog.json. dbt uses this file to render information like column types and table statistics into the docs site. In pre-commit-dbt is used for column operations. Default: target/catalog.json

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-all-columns

When to use it

You want to make sure that you have all the database columns listed in the properties file, or that your properties file no longer contains deleted columns.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ✅ Yes

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • The catalog is scanned for a model.
  • If there is any discrepancy between manifest and catalog models, the hook fails.

Known limitations

If you did not update the catalog and manifest results can be wrong.


check-model-has-description

Ensures that the model has a description in the properties file (usually schema.yml).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-description

When to use it

You want to make sure that all models have a description.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed since it also validates properties files ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml and SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • Modified yml files are scanned for a model.
  • If any model (from a manifest or yml files) does not have a description, the hook fails.
  • The model description must be in either the yml file or the manifest.

Known limitations

If you run your model and then you delete the description from a properties file, the hook success since the description is still present in manifest.json.


check-model-has-meta-keys

Ensures that the model has a list of valid meta keys. (usually schema.yml).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--meta-keys: list of the required keys in the meta part of the model.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-meta-keys
   args: ['--meta-keys', 'foo', 'bar', "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

If every model needs to have certain meta keys.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed since it also validates properties files ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml and SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • Modified yml files are scanned for a model.
  • If any model (from a manifest or yml files) does not have specified meta keys, the hook fails.
  • The meta keys must be in either the yml file or the manifest.

Known limitations

If you run your model and then you delete meta keys from a properties file, the hook success since the meta keys is still present in manifest.json.


check-model-has-properties-file

Ensures that the model has a properties file (schema file).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-properties-file

When to use it

You want to make sure that every model has a properties file.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • If any model does not have patch_path, the hook fails.

Known limitations

You need to create a schema file and then rerun your model (dbt run or dbt compile), otherwise, this hook will fail.


check-model-has-tests-by-name

Ensures that the model has a number of tests of a certain name (e.g. data, unique).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--tests: key-value pairs of test names. Key is the name of test and value is required minimal number of tests eg. --test unique=1 not_null=2 (do not put spaces before or after the = sign).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-tests-by-name
   args: ["--tests", "unique=1", "data=1", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that every model has certain tests.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • If any model does not have the number of required tests, the hook fails.

check-model-has-tests-by-type

Ensures that the model has a number of tests of a certain type (data, schema).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--tests: key-value pairs of test types. Key is the type of test (data or schema) and value is required eg. --test data=1 schema=2 (do not put spaces before or after the = sign).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-tests-by-type
   args: ["--tests", "schema=1", "data=1", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that every model has certain tests.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • If any model does not have the number of required tests, the hook fails.

check-model-has-tests-by-group

Ensures that the model has a number of tests from a group of tests.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--tests: list of test names. --test_cnt: number of tests required across test group.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-tests-by-group
   args: ["--tests", "unique", "unique_where", "--test-cnt", "1", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that every model has one (or more) of a group of eligible tests (e.g. a set of unique tests).

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • If any model does not have the number of required tests, the hook fails.

check-model-has-tests

Ensures that the model has a number of tests.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--test-cnt: Minimum number of tests required.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-has-tests
   args: ["--test-cnt", "2", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that every model was tested.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • If any model does not have a number of required tests, the hook fails.

check-model-parents-and-childs

Ensures the model has a specific number (max/min) of parents or/and childs.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--min-parent-cnt: Minimal number of parent sources and models. --max-parent-cnt: Maximal number of parent sources and models. --min-child-cnt: Minimal number of child models. --max-child-cnt: Maximal number of child models.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-parents-and-childs
   args: ["--min-parent-cnt", "2", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to find orphaned models (empty file, hard-coded reference, etc.). Or you want to make sure that every model is used somewhere so you are not e.g. materializing unused tables.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a parent and child models.
  • If any model does not have a number of required parents/childs, the hook fails.

check-model-parents-database

Ensures the parent models or sources are from certain database.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--whitelist: list of allowed databases. --blacklist: list of disabled databases.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-parents-database
   args: ["--blacklist", "SRC", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to be sure that certain models are using only models from specified database(s).

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a parent models/sources.
  • If any marent model does not have allowed or has disabled databases, the hook fails.

check-model-parents-schema

Ensures the parent models or sources are from certain schema.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--whitelist: list of allowed schemas. --blacklist: list of disabled schemas.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-parents-schema
   args: ["--blacklist", "stage", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to be sure that certain models are using only models from specified schema(s).

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a parent models/sources.
  • If any marent model does not have allowed or has disabled schemas, the hook fails.

check-model-tags

Ensures that the model has only valid tags from the provided list.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--tags: A list of tags that models can have.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-model-tags
   args: ["--tags", "foo", "bar", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

Make sure you did not typo in tags.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • If any model has different tags than specified, the hook fails.

check-script-ref-and-source

Ensures that the script contains only existing sources or macros.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-script-ref-and-source

When to use it

Make sure you have only valid ref and sources in your script and you do not want to wait for dbt run to find them. This hook also finds all missing ref and sources, not find first missing only.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

check-script-semicolon

Ensure that the script does not have a semicolon at the end of the file.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-script-semicolon

When to use it

Make sure you did not provide a semicolon at the end of the file.

How it works

  • Hook takes all changed SQL files.
  • It parses SQL and finds all sources and refs. If those objects do not exist in manifest.json, the hook fails.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • If the file contains a semicolon at the end of the file, the hook fails.

check-script-has-no-table-name

Ensures that the script is using only source or ref macro to specify the table name.

Arguments

--ignore-dotless-table: consider all tables without dot in name as CTE

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-script-has-no-table-name

When to use it

To make sure that you have only refs and sources in your SQL files.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • SQL is parsed and if it contains direct tables (not ref() or source()), the hook fails.

check-source-columns-have-desc

Ensures that the source has columns with descriptions in the properties file (usually schema.yml).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-columns-have-desc

When to use it

You want to make sure that all specified columns in the properties files (usually schema.yml) have some description. This hook does not validate if all database columns are also present in a properties file.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If any column in the source does not contain a description, the hook fails.

check-source-has-all-columns

Ensures that all columns in the database are also specified in the properties file. (usually schema.yml).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--catalog: location of catalog.json file. Usually target/catalog.json. dbt uses this file to render information like column types and table statistics into the docs site. In pre-commit-dbt is used for column operations. Default: target/catalog.json

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-has-all-columns

When to use it

You want to make sure that you have all the database columns listed in the properties file, or that your properties file no longer contains deleted columns.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ No ✅ Yes

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • The catalog is scanned for a model.
  • If there is any discrepancy between found yml sources and catalog sources, the hook fails.

Known limitations

If you did not update the catalog and manifest results can be wrong.


check-source-table-has-description

Ensures that the source table has a description in the properties file (usually schema.yml).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-table-has-description

When to use it

You want to make sure that all sources have a description.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source table does not have a description, the hook fails.

check-source-has-freshness

Ensures that the source has freshness options in the properties file (usually schema.yml).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-has-freshness
   args: ["--freshness", "error_after", "warn_after", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that all freshness is correctly set.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source does not have freshness correctly set, the hook fails.

check-source-has-loader

Ensures that the source has a loader option in the properties file (usually schema.yml).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-has-loader

When to use it

You want to make sure that the source has loader specified.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source does not have a loader set, the hook fails.

check-source-has-meta-keys

Ensures that the source has a list of valid meta keys. (usually schema.yml).

Arguments

--meta-keys: list of the required keys in the meta part of the model.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-has-meta-keys
   args: ['--meta-keys', 'foo', 'bar', "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

If every source needs to have certain meta keys.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed since it also validates properties files ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source does not have the required meta keys set, the hook fails.

check-source-has-tests-by-name

Ensures that the source has a number of tests of a certain name (e.g. data, unique).

Arguments

--tests: key-value pairs of test names. Key is the name of test and value is required minimal number of tests eg. --test unique=1 not_null=2 (do not put spaces before or after the = sign).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-has-tests-by-name
   args: ["--tests", "unique=1", "data=1", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that every source has certain tests.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source does not have the required test names, the hook fails.

check-source-has-tests-by-type

Ensures that the source has a number of tests of a certain type (data, schema).

Arguments

--tests: key-value pairs of test types. Key is a type of test (data or schema) and value is required eg. --test data=1 schema=2 (do not put spaces before or after the = sign).

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-has-tests-by-type
   args: ["--tests", "schema=1", "data=1", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that every source has certain tests.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source does not have the required test types, the hook fails.

check-source-has-tests

Ensures that the source has a number of tests.

Arguments

--tests: Minimum number of tests required.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-has-tests
   args: ["--tests", "2", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to make sure that every source was tested.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source does not have the required test count, the hook fails.

check-source-tags

Ensures that the source has only valid tags from the provided list.

Arguments

--tags: A list of tags that sources can have.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-tags
   args: ["--tags", "foo", "bar", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

Make sure you did not typo in tags.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • If the source has different tags than specified, the hook fails.

check-source-childs

Ensures the source has a specific number (max/min) of childs.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json
--min-child-cnt: Minimal number of child models. --max-child-cnt: Maximal number of child models.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-source-childs
   args: ["--min-parent-cnt", "2", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want to find orphaned sources without any dependencies. Or you want to make sure that every source is used somewhere.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml.
  • All sources from yml file are parsed.
  • The manifest is scanned for child models.
  • If any source does not have a number of required childs, the hook fails.

check-macro-has-description

Ensures that the macro has a description in the properties file (usually macro.yml).

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: check-macro-has-description

When to use it

You want to make sure that all macros have a description.

Requirements

Macro exists in manifest.json 1 Macro exists in catalog.json 2
❌ Not needed since it also validates properties files ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed yml and SQL files.
  • The macro name is obtained from the SQL file name.
  • The manifest is scanned for a macro.
  • Modified yml files are scanned for a macro.
  • If any macro (from a manifest or yml files) does not have a description, the hook fails.
  • The macro description must be in either the yml file or the manifest.

Known limitations

If you run your model and then you delete the description from a properties file, the hook success since the description is still present in manifest.json.


generate-missing-sources

If any source is missing this hook tries to create it.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json.
--schema-file: Location of schema.yml file. Where new source tables should be created.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: generate-missing-sources
   args: ["--schema-file", "models/schema.yml", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You are too lazy to define schemas manually :D.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed since this hook tries to generate even non-existent source ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • SQL is parsed to find all sources.
  • If the source exists in the manifest, nothing is done.
  • If not, a new source is created in specified schema-file and the hook fails.

Known limitations

Source "envelope" has to exist in specified schema-file, something like this:

version: 2
sources:
- name: <source_name>

Otherwise, it is not possible to automatically generate a new source table.

Unfortunately, this hook breaks your formatting.


unify-column-description

Unify column descriptions across all models.

Arguments

--ignore: Columns for which do not check whether have a different description.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: unify-column-description
   args: ["--ignore", "foo", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You want the descriptions of the same columns to be the same. E.g. in two of your models, you have customer_id with the description This is cutomer_id, but there is one model where column customer_id has a description Something else. This hook finds discrepancies between column descriptions and replaces them. So as the results all columns going to have the description This is customer_id

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed since this hook is using only yaml files ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed YAML files.
  • From those files columns are parsed and compared.
  • If one column name has more than one (not empty) description, the description with the most occurrences is taken and the hook fails.
  • If it is not possible to decide which description is dominant, no changes are made.

Known limitations

If it is not possible to decide which description is dominant, no changes are made.


replace-script-table-names

Replace table names with source or ref macros in the script.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: replace-script-table-names

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You are running and debugging your SQL in the editor. This editor does not know source or ref macros. So every time you copy the script from the editor into dbt project you need to rewrite all table names to source or ref. That's boring and error-prone. If you run this hook it will replace all table names with macros instead of you.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • SQL is parsed and table names are found.
  • Firstly it tries to find table name in models - ref.
  • Then it tries to find a table in sources - source.
  • If nothing is found it creates unknown source as source('<schema_name>', '<table_name>')
  • If the script contains only ref and source macros, the hook success.

generate-model-properties-file

Generate model properties file if does not exist.

Arguments

--manifest: location of manifest.json file. Usually target/manifest.json. This file contains a full representation of dbt project. Default: target/manifest.json.
--catalog: location of catalog.json file. Usually target/catalog.json. dbt uses this file to render information like column types and table statistics into the docs site. In pre-commit-dbt is used for column operations. Default: target/catalog.json
--properties-file: Location of file where new model properties should be generated. Suffix has to be yml or yaml. It can also include {database}, {schema}, {name} and {alias} variables. E.g. /models/{schema}/{name}.yml for model foo.bar will create properties file in /models/foo/bar.yml. If path already exists, properties are appended.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: generate-model-properties-file
   args: ["--properties-file", "/models/{schema}/{name}.yml", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.

When to use it

You are running and debugging your SQL in the editor. This editor does not know source or ref macros. So every time you copy the script from the editor into dbt project you need to rewrite all table names to source or ref. That's boring and error-prone. If you run this hook it will replace all table names with macros instead of you.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
✅ Yes ❌ Yes

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • The model name is obtained from the SQL file name.
  • The manifest is scanned for a model.
  • The catalog is scanned for a model.
  • If the model does not have patch_path in the manifest, the new schema is written to the specified path. The hook fails.

Known limitations

Unfortunately, this hook breaks your formatting in the written file.


remove-script-semicolon

Remove the semicolon at the end of the script.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: remove-script-semicolon

When to use it

You are too lazy or forgetful to delete one character at the end of the script.

Requirements

Model exists in manifest.json 1 Model exists in catalog.json 2
❌ Not needed ❌ Not needed

1 It means that you need to run dbt run, dbt compile before run this hook.
2 It means that you need to run dbt docs generate before run this hook.

How it works

  • Hook takes all changed SQL files.
  • If the file contains a semicolon at the end of the file, it is removed and the hook fails.

dbt-clean

Run the dbt clean command. Deletes all folders specified in the clean-targets.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-clean

dbt-compile

Run the dbt compile command. Generates executable SQL from source model, test, and analysis files.

Arguments

--global-flags: Global dbt flags applicable to all subcommands. Instead of dash - please use +.
--cmd-flags: Command-specific dbt flags. Instead of dash - please use +.
--model-prefix: Prefix dbt selector, for selecting parents.
--model-postfix: Postfix dbt selector, for selecting children.
--models: pre-commit-dbt is by default running changed files. If you need to override that, e.g. in case of Slim CI (state:modified), you can use this option.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-compile
   args: ["--model-prefix". "+", "--"]

or

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-compile
   args: ["--models", "state:modified", "--cmd-flags", "++defer", "++state", "path/to/artifacts", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.


dbt-deps

Run dbt deps command. Pulls the most recent version of the dependencies listed in your packages.yml.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-deps

dbt-docs-generate

Run dbt docs generate command. The command is responsible for generating your project's documentation website.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-docs-generate

dbt-run

Run dbt run command. Executes compiled SQL model files.

Arguments

--global-flags: Global dbt flags applicable to all subcommands. Instead of dash - please use +.
--cmd-flags: Command-specific dbt flags. Instead of dash - please use +.
--model-prefix: Prefix dbt selector, for selecting parents.
--model-postfix: Postfix dbt selector, for selecting children.
--models: pre-commit-dbt is by default running changed files. If you need to override that, e.g. in case of Slim CI (state:modified), you can use this option.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-run
   args: ["--model-prefix", "+", "--"]

or

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-run
   args: ["--models", "state:modified", "--cmd-flags", "++defer", "++state", "path/to/artifacts", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.


dbt-test

Run dbt test command. Runs tests on data in deployed models.

Arguments

--global-flags: Global dbt flags applicable to all subcommands. Instead of dash - please use +.
--cmd-flags: Command-specific dbt flags. Instead of dash - please use +.
--model-prefix: Prefix dbt selector, for selecting parents.
--model-postfix: Postfix dbt selector, for selecting children. --models: pre-commit-dbt is by default running changed files. If you need to override that, e.g. in case of Slim CI (state:modified), you can use this option.

Example

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-test
   args: ["--model-prefix", "+", "--"]

or

repos:
- repo: https://github.com/offbi/pre-commit-dbt
 rev: v1.0.0
 hooks:
 - id: dbt-test
   args: ["--models", "state:modified", "--cmd-flags", "++defer", "++state", "path/to/artifacts", "--"]

⚠️ do not forget to include -- as the last argument. Otherwise pre-commit would not be able to separate a list of files with args.