Skip to content

Commit 594f799

Browse files
authored
Merge pull request #368 from dbt-labs/tobie/primary_key_expansion
Tobie/primary key expansion
2 parents 2180997 + 37690f8 commit 594f799

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

dbt_project.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ vars:
5757

5858
primary_key_test_macros: [["dbt.test_unique", "dbt.test_not_null"], ["dbt_utils.test_unique_combination_of_columns"]]
5959

60+
# -- Graph variables --
61+
# node types to test for primary key coverage. acceptable node types: model, source, snapshot, seed
62+
enforced_primary_key_node_types: ["model"]
63+
6064
# -- DAG variables --
6165
models_fanout_threshold: 3
6266

docs/customization/overriding-variables.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Currently, this package uses different variables to adapt the models to your obj
99
| `test_coverage_target` | the minimum acceptable test coverage percentage | 100% |
1010
| `documentation_coverage_target` | the minimum acceptable documentation coverage percentage | 100% |
1111
| `primary_key_test_macros` | the set(s) of dbt tests used to check validity of a primary key | `[["dbt.test_unique", "dbt.test_not_null"], ["dbt_utils.test_unique_combination_of_columns"]]` |
12+
| `enforced_primary_key_node_types` | the set of node types for you you would like to enforce primary key test coverage. Valid options to include are `model`, `source`, `snapshot`, `seed` | `["model"]`
1213

1314
**Usage notes for `primary_key_test_macros:`**
1415

@@ -21,7 +22,7 @@ For each entry in the parent list, the logic in `int_model_test_summary` will ev
2122

2223
Each set of test(s) that define a primary key requirement must be grouped together in a sub-list to ensure they are evaluated together (e.g. [`dbt.test_unique`, `dbt.test_not_null`] ).
2324

24-
*While it's not explicitly tested in this package, we strongly encourage adding a `not_null` test on each of the columns listed in the `dbt_utils.unique_combination_of_columns` tests.*
25+
*While it's not explicitly tested in this package, we strongly encourage adding a `not_null` test on each of the columns listed in the `dbt_utils.unique_combination_of_columns` tests. Alternatively, on Snowflake, consider `dbt_constraints.test_primary_key` in the [dbt Constraints](https://github.com/Snowflake-Labs/dbt_constraints) package, which enforces each field in the primary key is non null.*
2526

2627
```yaml title="dbt_project.yml"
2728
# set your test and doc coverage to 75% instead

docs/rules/testing.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Apply a [uniqueness test](https://docs.getdbt.com/reference/resource-properties/
2121
Additional tests can be configured by applying a [generic test](https://docs.getdbt.com/docs/building-a-dbt-project/tests#generic-tests) in the model's `.yml` entry or by creating a [singular test](https://docs.getdbt.com/docs/building-a-dbt-project/tests#singular-tests)
2222
in the `tests` directory of you project.
2323

24+
**Enforcing on more node types(Advanced)**
25+
26+
You can optionally extend this test to apply to more node types (`source`,`snapshot`, `seed`). By configuring the variable `enforced_primary_key_node_types` to be a set of node types for which you wish to enforce primary key test coverage in addition to (or instead of) just models. Check out the [overriding variables section](../customization/overriding-variables.md) for instructions
27+
28+
Snapshots should always have a multi-field primary key in order to function, while sources and seeds may not. Depending on your expectations for duplicates and null values, different kinds of primary key tests may be appropriate. Consider your use case carefully.
29+
2430
---
2531

2632
## Test Coverage

models/marts/tests/fct_missing_primary_key_tests.sql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
{{
2-
config(
3-
alias = 'my_alias',
4-
)
5-
}}
6-
71
with
82

93
tests as (
104
select * from {{ ref('int_model_test_summary') }}
5+
where resource_type in
6+
(
7+
{% for resource_type in var('enforced_primary_key_node_types') %}'{{ resource_type }}'{% if not loop.last %},{% endif %}
8+
{% endfor %}
9+
)
1110
),
1211

1312
final as (

models/marts/tests/fct_test_coverage.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ with
22

33
test_counts as (
44
select * from {{ ref('int_model_test_summary') }}
5+
where resource_type = 'model'
56
),
67

78
conversion as (

models/marts/tests/intermediate/int_model_test_summary.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ agg_test_relationships as (
5656
final as (
5757
select
5858
all_graph_resources.resource_name,
59+
all_graph_resources.resource_type,
5960
all_graph_resources.model_type,
6061
coalesce(agg_test_relationships.is_primary_key_tested, FALSE) as is_primary_key_tested,
6162
coalesce(agg_test_relationships.number_of_tests_on_model, 0) as number_of_tests_on_model
6263
from all_graph_resources
6364
left join agg_test_relationships
6465
on all_graph_resources.resource_id = agg_test_relationships.direct_parent_id
65-
where all_graph_resources.resource_type = 'model'
66+
where
67+
all_graph_resources.resource_type in ('model', 'seed', 'source', 'snapshot')
6668
)
6769

6870
select * from final

0 commit comments

Comments
 (0)