Skip to content

Commit

Permalink
Merge pull request #115 from erkanncelen/main
Browse files Browse the repository at this point in the history
issue106/get_models helper macro
  • Loading branch information
joellabes authored Feb 1, 2023
2 parents 3e4699c + 8d112be commit fe0b776
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Addition of the [create_base_models](macros/create_base_models.sql)
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.
- Add `include_data_types` flag to `generate_source` macro ([#76](https://github.com/dbt-labs/dbt-codegen/pull/76))
- 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.

## Fixes
- 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))
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ schema.yml file.
) }}
```

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.

```
{% set models_to_generate = codegen.get_models(directory='marts', prefix='fct_') %}
{{ codegen.generate_model_yaml(
model_names = models_to_generate
) }}
```

Alternatively, call the macro as an [operation](https://docs.getdbt.com/docs/using-operations):

```
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/tests/test_helper_get_models.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- depends_on: {{ ref('model_data_a') }}
-- depends_on: {{ ref('model_struct') }}
-- depends_on: {{ ref('model_without_import_ctes') }}
-- depends_on: {{ ref('model_without_any_ctes') }}

{% if execute %}
{% 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'] %}

{{ assert_equal (actual_list, expected_list) }}
31 changes: 31 additions & 0 deletions macros/helpers/helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,35 @@
{% endfor %}
{{ return(glob_dict) }}
{% endif %}
{% endmacro %}

{# build a list of models looping through all models in the project #}
{# filter by directory or prefix arguments, if provided #}
{% macro get_models(directory=None, prefix=None) %}
{% set model_names=[] %}
{% set models = graph.nodes.values() | selectattr('resource_type', "equalto", 'model') %}
{% if directory and prefix %}
{% for model in models %}
{% set model_path = "/".join(model.path.split("/")[:-1]) %}
{% if model_path == directory and model.name.startswith(prefix) %}
{% do model_names.append(model.name) %}
{% endif %}
{% endfor %}
{% elif directory %}
{% for model in models %}
{% set model_path = "/".join(model.path.split("/")[:-1]) %}
{% if model_path == directory %}
{% do model_names.append(model.name) %}
{% endif %}
{% endfor %}
{% elif prefix %}
{% for model in models if model.name.startswith(prefix) %}
{% do model_names.append(model.name) %}
{% endfor %}
{% else %}
{% for model in models %}
{% do model_names.append(model.name) %}
{% endfor %}
{% endif %}
{{ return(model_names) }}
{% endmacro %}

0 comments on commit fe0b776

Please sign in to comment.