-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #340 from dbt-labs/feature-allow-excluding-models-…
…packages Feature to allow excluding models and packages
- Loading branch information
Showing
37 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Excluding packages or sources/models based on their path | ||
|
||
!!! note | ||
|
||
This section is describing how to entirely exclude models/sources and packages to be evaluated. | ||
If you want to document exceptions to the rules, see the section [on exceptions](exceptions.md) | ||
and if you want to deactivate entire tests you can follow instructions from [this page](customization.md) | ||
|
||
There might be cases where you want to exclude models/sources from being tested: | ||
|
||
- they could come from a package for which you have no control over | ||
- you might be refactoring your project and wanting to exclude entire folders to follow best-practices in the new models | ||
|
||
In that case, this package provides the ability to exclude whole packages and/or models and sources based on their path | ||
|
||
## Configuration | ||
|
||
The variables `exclude_packages` and `exclude_paths_from_project` allow you to define a list of regex patterns to exclude from being reported as errors. | ||
|
||
- `exclude_packages` accepts a list of package names to exclude from the tool. To exclude all packages except the current project, you can set it to `["all"]` | ||
- `exclude_paths_from_project` accepts a list of regular expressions of paths to exclude for the current project | ||
- **for models**, the regex provided will try to match the pattern in the string `<path/to/model.sql>`, allowing to exclude packages, but also whole folders or individual models | ||
- **for sources**, the regex will try to match the pattern in `<path/to/sources.yml>:<source_name>.<source_table_name>` *(the pattern is different than for models because the path itself doesn't let us exclude individual sources)* | ||
|
||
!!! note | ||
|
||
We currently don't allow excluding metrics and exposures, as if those need to be entirely excluded they could be deactivated from the project. | ||
|
||
If you have a specific use case requiring this ability, please raise a GitHub issue to explain the situation you'd like to solve and we can revisit this decision ! | ||
|
||
### Example to exclude a whole package | ||
|
||
```yaml title="dbt_project.yml" | ||
vars: | ||
exclude_packages: ["upstream_package"] | ||
``` | ||
### Example to exclude models/sources in a given path | ||
```yaml title="dbt_project.yml" | ||
vars: | ||
exclude_paths_from_project: ["/models/legacy/"] | ||
``` | ||
### Example to exclude both a package and models/sources in 2 different paths | ||
```yaml title="dbt_project.yml" | ||
vars: | ||
exclude_packages: ["upstream_package"] | ||
exclude_paths_from_project: ["/models/legacy/", "/my_date_spine.sql"] | ||
``` | ||
## Tips and tricks | ||
Regular expressions are very powerful but can become complex. After defining your value for `exclude_paths_from_project`, we recommend running the package and inspecting the model `int_all_graph_resources`, checking if the value in the column `is_excluded` matches your expectation. | ||
|
||
A useful tool to debug regular expression is [regex101](https://regex101.com/). You can provide a pattern and a list of strings to see which ones actually match the pattern. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
integration_tests/models/marts/intermediate/to_exclude/int_model_10.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- depends on: {{ ref('fct_model_9') }} | ||
|
||
select 1 as id |
3 changes: 3 additions & 0 deletions
3
integration_tests/models/marts/intermediate/to_exclude/int_model_11.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- depends on: {{ ref('fct_model_9') }} | ||
|
||
select 1 as id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{% macro set_is_excluded(resource, resource_type) %} | ||
{{ return(adapter.dispatch('set_is_excluded', 'dbt_project_evaluator')(resource, resource_type)) }} | ||
{% endmacro %} | ||
|
||
{% macro default__set_is_excluded(resource, resource_type) %} | ||
|
||
{% set re = modules.re %} | ||
{%- set ns = namespace(exclude=false) -%} | ||
|
||
{% if resource_type == 'node' %} | ||
{%- set resource_path = resource.original_file_path | replace("\\","\\\\") -%} | ||
{% elif resource_type == 'source' %} | ||
{%- set resource_path = resource.original_file_path | replace("\\","\\\\") ~ ":" ~ resource.fqn[-2] ~ "." ~ resource.fqn[-1] -%} | ||
{% else %} | ||
{{ exceptions.raise_compiler_error( | ||
"`set_is_excluded()` macro does not support resource type: " ~ resource_type | ||
) }} | ||
{% endif %} | ||
|
||
|
||
{#- we exclude the resource if it is from the current project and matches the pattern -#} | ||
{%- for exclude_paths_pattern in var('exclude_paths_from_project',[]) -%} | ||
{%- set matched_path = re.search(exclude_paths_pattern, resource_path, re.IGNORECASE) -%} | ||
{%- if matched_path and resource.package_name == project_name %} | ||
{% set ns.exclude = true %} | ||
{%- endif -%} | ||
{%- endfor -%} | ||
|
||
{#- we exclude the resource if the package if it is listed in `exclude_packages` or if it is "all" -#} | ||
{%- if ( | ||
resource.package_name != project_name) | ||
and (resource.package_name in var('exclude_packages',[]) or 'all' in var('exclude_packages',[])) | ||
-%} | ||
{% set ns.exclude = true %} | ||
{%- endif -%} | ||
|
||
{{ return(ns.exclude) }} | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.