diff --git a/CHANGELOG.md b/CHANGELOG.md index e297377..0e37590 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/README.md b/README.md index cf70350..472ae8a 100644 --- a/README.md +++ b/README.md @@ -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): ``` diff --git a/integration_tests/tests/test_helper_get_models.sql b/integration_tests/tests/test_helper_get_models.sql new file mode 100644 index 0000000..1d6e380 --- /dev/null +++ b/integration_tests/tests/test_helper_get_models.sql @@ -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) }} diff --git a/macros/helpers/helpers.sql b/macros/helpers/helpers.sql index 262c7d9..fd28c87 100644 --- a/macros/helpers/helpers.sql +++ b/macros/helpers/helpers.sql @@ -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 %} \ No newline at end of file