Skip to content

Commit

Permalink
Read upstream descriptions from sources (#154)
Browse files Browse the repository at this point in the history
* Read upstream descriptions from source

* Update README.md

* Add test

* Update test_helper_get_models.sql
  • Loading branch information
esegal authored Feb 28, 2024
1 parent cb34ce4 commit 53d787b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## New features
- `generate_model_yaml` with `upstream_descriptions=True` now reads from upstream sources in addition to models.

## Fixes
- Column `description` fields are now correctly escaped in `generate_model_yaml` ([#142](https://github.com/dbt-labs/dbt-codegen/issues/142))

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ schema.yml file.

### Arguments:
* `model_names` (required): The model(s) you wish to generate YAML for.
* `upstream_descriptions` (optional, default=False): Whether you want to include descriptions for identical column names from upstream models.
* `upstream_descriptions` (optional, default=False): Whether you want to include descriptions for identical column names from upstream models and sources.
* `include_data_types` (optional, default=True): Whether you want to add data types to your model column definitions.

### Usage:
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/models/model_from_source.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select
*
from {{ source('codegen_integration_tests__data_source_schema', 'codegen_integration_tests__data_source_table') }}
5 changes: 5 additions & 0 deletions integration_tests/models/source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ sources:
- name: codegen_integration_tests__data_source_schema
tables:
- name: codegen_integration_tests__data_source_table
columns:
- name: my_integer_col
description: My Integer Column
- name: my_bool_col
description: My Boolean Column
- name: codegen_integration_tests__data_source_table_case_sensitive
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% set actual_model_yaml = codegen.generate_model_yaml(
model_names=['model_from_source'],
upstream_descriptions=True,
include_data_types=False
)
%}

{% set expected_model_yaml %}
version: 2

models:
- name: model_from_source
description: ""
columns:
- name: my_integer_col
description: "My Integer Column"

- name: my_bool_col
description: "My Boolean Column"

{% endset %}

{{ assert_equal (actual_model_yaml | trim, expected_model_yaml | trim) }}
2 changes: 1 addition & 1 deletion integration_tests/tests/test_helper_get_models.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
{% set actual_list = codegen.get_models(prefix='model_')|sort %}
{% endif %}

{% set expected_list = ['model_data_a', 'model_struct', 'model_without_any_ctes', 'model_without_import_ctes'] %}
{% set expected_list = ['model_data_a', 'model_from_source', 'model_struct', 'model_without_any_ctes', 'model_without_import_ctes'] %}

{{ assert_equal (actual_list, expected_list) }}
16 changes: 13 additions & 3 deletions macros/helpers/helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@


{# add to an input dictionary entries containing all the column descriptions of a given model #}
{% macro add_model_column_descriptions_to_dict(model_name,dict_with_descriptions={}) %}
{% for node in graph.nodes.values() | selectattr('name', "equalto", model_name) %}
{% macro add_model_column_descriptions_to_dict(resource_type, model_name, dict_with_descriptions={}) %}
{% if resource_type == 'source' %}
{# sources aren't part of graph.nodes #}
{% set nodes = graph.sources %}
{% else %}
{% set nodes = graph.nodes %}
{% endif %}
{% for node in nodes.values()
| selectattr('resource_type', 'equalto', resource_type)
| selectattr('name', 'equalto', model_name) %}
{% for col_name, col_values in node.columns.items() %}
{% do dict_with_descriptions.update( {col_name: col_values.description} ) %}
{% endfor %}
Expand All @@ -22,7 +30,9 @@
{% if execute %}
{% set glob_dict = {} %}
{% for full_model in codegen.get_model_dependencies(model_name) %}
{% do codegen.add_model_column_descriptions_to_dict(full_model.split('.')[-1],glob_dict) %}
{% do codegen.add_model_column_descriptions_to_dict(
full_model.split('.')[0], full_model.split('.')[-1], glob_dict
) %}
{% endfor %}
{{ return(glob_dict) }}
{% endif %}
Expand Down

0 comments on commit 53d787b

Please sign in to comment.