Skip to content

Commit e24e5dc

Browse files
authored
Merge pull request #115 from erkanncelen/main
issue106/get_models helper macro
2 parents 8b3ee74 + b45a1d2 commit e24e5dc

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Addition of the [create_base_models](macros/create_base_models.sql)
1616
This macro generates a series of terminal commands (appended w) bash script which creates a new file in your dbt project based off the results of the [generate_base_model](macros/generate_base_model.sql) macro. Therefore, instead of outputting in the terminal, it will create the file for you.
1717
- Add `include_data_types` flag to `generate_source` macro ([#76](https://github.com/dbt-labs/dbt-codegen/pull/76))
18+
- Add `get_models` macro in helper macros. This macro retrieves a list of models with specified prefix at the specified directory. It is designed to make creating yamls for multiple models easier.
1819

1920
## Fixes
2021
- Fix handling of nested `STRUCT` fields in BigQuery ([#98](https://github.com/dbt-labs/dbt-codegen/issues/98), [#105](https://github.com/dbt-labs/dbt-codegen/pull/105))

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,15 @@ schema.yml file.
208208
) }}
209209
```
210210

211+
You can use the helper function codegen.get_models and specify a directory and/or prefix to get a list of all matching models, to be passed into model_names list.
212+
213+
```
214+
{% set models_to_generate = codegen.get_models(directory='marts', prefix='fct_') %}
215+
{{ codegen.generate_model_yaml(
216+
model_names = models_to_generate
217+
) }}
218+
```
219+
211220
Alternatively, call the macro as an [operation](https://docs.getdbt.com/docs/using-operations):
212221

213222
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- depends_on: {{ ref('model_data_a') }}
2+
-- depends_on: {{ ref('model_struct') }}
3+
-- depends_on: {{ ref('model_without_import_ctes') }}
4+
-- depends_on: {{ ref('model_without_any_ctes') }}
5+
6+
{% if execute %}
7+
{% set actual_list = codegen.get_models(prefix='model_')|sort %}
8+
{% endif %}
9+
10+
{% set expected_list = ['model_data_a', 'model_struct', 'model_without_any_ctes', 'model_without_import_ctes'] %}
11+
12+
{{ assert_equal (actual_list, expected_list) }}

macros/helpers/helpers.sql

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,35 @@
2626
{% endfor %}
2727
{{ return(glob_dict) }}
2828
{% endif %}
29+
{% endmacro %}
30+
31+
{# build a list of models looping through all models in the project #}
32+
{# filter by directory or prefix arguments, if provided #}
33+
{% macro get_models(directory=None, prefix=None) %}
34+
{% set model_names=[] %}
35+
{% set models = graph.nodes.values() | selectattr('resource_type', "equalto", 'model') %}
36+
{% if directory and prefix %}
37+
{% for model in models %}
38+
{% set model_path = "/".join(model.path.split("/")[:-1]) %}
39+
{% if model_path == directory and model.name.startswith(prefix) %}
40+
{% do model_names.append(model.name) %}
41+
{% endif %}
42+
{% endfor %}
43+
{% elif directory %}
44+
{% for model in models %}
45+
{% set model_path = "/".join(model.path.split("/")[:-1]) %}
46+
{% if model_path == directory %}
47+
{% do model_names.append(model.name) %}
48+
{% endif %}
49+
{% endfor %}
50+
{% elif prefix %}
51+
{% for model in models if model.name.startswith(prefix) %}
52+
{% do model_names.append(model.name) %}
53+
{% endfor %}
54+
{% else %}
55+
{% for model in models %}
56+
{% do model_names.append(model.name) %}
57+
{% endfor %}
58+
{% endif %}
59+
{{ return(model_names) }}
2960
{% endmacro %}

0 commit comments

Comments
 (0)