Skip to content

Commit a955400

Browse files
Merge pull request #266 from dbt-labs/feature/display-results
Add macros to print issues to logs
2 parents 40755ee + 53fe3a4 commit a955400

3 files changed

Lines changed: 77 additions & 2 deletions

File tree

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Once you've installed the package, all you have to do is run a `dbt build --sele
4747

4848
Each test warning indicates the presence of a type of misalignment. To troubleshoot a misalignment:
4949
1. Locate the related documentation below
50-
2. Query the associated model to find the specific instances of the issue within your project
50+
2. Query the associated model to find the specific instances of the issue within your project or set up an [`on-run-end` hook](https://docs.getdbt.com/reference/project-configs/on-run-start-on-run-end) to display the rules violations in the dbt logs (see [displaying violations in the logs](#displaying-violations-in-the-logs))
5151
3. Either fix the issue(s) or [customize](#customization) the package to exclude them
5252

5353
----
@@ -1123,10 +1123,28 @@ seeds:
11231123
#### 3. Run the seed and the package
11241124

11251125
We then run both the seed and the package by executing the following command:
1126-
```
1126+
1127+
```bash
11271128
dbt build --select package:dbt_project_evaluator dbt_project_evaluator_exceptions
11281129
```
11291130

1131+
### Displaying violations in the logs
1132+
1133+
This package provides a macro that can be executed via an `on-run-end` hook to display the package results in the logs in addition to storing those in the Data Warehouse.
1134+
1135+
To use it, you can add the following line in your `dbt_project.yml`:
1136+
1137+
```yml
1138+
on-run-end: "{{ dbt_project_evaluator.print_dbt_project_evaluator_issues() }}"
1139+
```
1140+
1141+
In the case that you are storing the tables with the package results in a schema or database different from the default ones from your profile, the following parameters are available for `print_dbt_project_evaluator_issues()`:
1142+
1143+
- `schema_project_evaluator`: the schema where the tables are stored
1144+
- `db_project_evaluator`: the database where the tables are stored
1145+
1146+
# dbt_project.yml
1147+
11301148
----
11311149
## Running this package as a CI check
11321150

integration_tests_2/dbt_project.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ dispatch:
2828
- macro_namespace: dbt
2929
search_order: ['dbt_project_evaluator', 'dbt']
3030

31+
on-run-end: "{{ dbt_project_evaluator.print_dbt_project_evaluator_issues() }}"
32+
3133
models:
3234
dbt_project_evaluator_integration_tests_2:
3335
# materialize as ephemeral to prevent the fake models from executing, but keep them enabled
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{% macro _return_list_header_rows(query) %}
2+
3+
{%- call statement('get_query_results', fetch_result=True,auto_begin=false) -%}
4+
5+
{{ query }}
6+
7+
{%- endcall -%}
8+
9+
{% set sql_results=[] %}
10+
11+
{%- if execute -%}
12+
{% set sql_results_table = load_result('get_query_results').table %}
13+
{% do sql_results.append(sql_results_table.column_names) %}
14+
{% for row_data in sql_results_table.rows %}
15+
{% do sql_results.append(row_data.values()) %}
16+
{% endfor %}
17+
{%- endif -%}
18+
19+
{{ return(sql_results) }}
20+
21+
{% endmacro %}
22+
23+
24+
{% macro print_dbt_project_evaluator_issues(schema_project_evaluator=None, db_project_evaluator=None) %}
25+
26+
{%- if flags.WHICH in ["build","test"] -%}
27+
{{ print("\n### List of issues raised by dbt_project_evaluator ###") }}
28+
29+
{% for result in results | selectattr('failures') | selectattr('failures', '>', 0) %}
30+
31+
{% if result.node.fqn[0] == "dbt_project_evaluator" %}
32+
33+
{{ print("\n-- " ~ result.node.alias ~ " --") }}
34+
35+
{% set model_checked = result.node.depends_on.nodes[0].split('.')[-1] %}
36+
{% set db_schema = database_package ~ "." ~ schema_package if database_package else schema_package %}
37+
{% set db_schema_model = db_schema ~ "." ~ model_checked if db_schema else model_checked %}
38+
39+
{% set sql_statement %}
40+
select * from {{ db_schema_model }}
41+
{% endset %}
42+
43+
{%- set failures = dbt_project_evaluator._return_list_header_rows(sql_statement) -%}
44+
{% for row in failures %}
45+
{{ print(row | join(", ")) }}
46+
{% endfor %}
47+
48+
{% endif %}
49+
50+
{% endfor %}
51+
52+
{{ print("\n") }}
53+
{%- endif %}
54+
55+
{% endmacro %}

0 commit comments

Comments
 (0)