Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/generate multiple models yaml #85

Merged
merged 12 commits into from
Nov 23, 2022
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ select * from renamed
4. Paste the output in to a model, and refactor as required.

## generate_model_yaml ([source](macros/generate_model_yaml.sql))
This macro generates the YAML for a model, which you can then paste into a
This macro generates the YAML for a model or list of models, which you can then paste into a
schema.yml file.

### Arguments:
* `model_name` (required): The model you wish to generate YAML for.
* `model_name` (required): The model or list of models you wish to generate YAML for.
* `upstream_descriptions` (optional, default=False): Whether you want to include descriptions for identical column names from upstream models.

### Usage:
Expand All @@ -149,6 +149,12 @@ Alternatively, call the macro as an [operation](https://docs.getdbt.com/docs/usi
$ dbt run-operation generate_model_yaml --args '{"model_name": "customers"}'
```

or

```
$ dbt run-operation generate_model_yaml --args '{"model_name": ["customers", "orders"]}'
```

3. The YAML for a base model will be logged to the command line

```
Expand Down
38 changes: 24 additions & 14 deletions macros/generate_model_yaml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,34 @@
{% do return(model_yaml) %}
{% endmacro %}

{% macro generate_model_yaml(model_name, upstream_descriptions=False) %}
{% macro generate_model_yaml(model_name=[], upstream_descriptions=False) %}

{% set model_yaml=[] %}
{% set column_desc_dict = codegen.build_dict_column_descriptions(model_name) if upstream_descriptions else {} %}
{% set model_yaml=[] %}
{% set column_desc_dict = codegen.build_dict_column_descriptions(model_name) if upstream_descriptions else {} %}

{% do model_yaml.append('version: 2') %}
{% do model_yaml.append('') %}
{% do model_yaml.append('models:') %}
{% do model_yaml.append(' - name: ' ~ model_name | lower) %}
{% do model_yaml.append(' description: ""') %}
{% do model_yaml.append(' columns:') %}
{% do model_yaml.append('version: 2') %}
{% do model_yaml.append('') %}
{% do model_yaml.append('models:') %}

{% if model_name is string %}
{% set model_name_list=[] %}
{% do model_name_list.append(model_name) %}
{% else %}
{% set model_name_list=model_name %}
{% endif %}

{% for model in model_name_list %}
{% do model_yaml.append(' - name: ' ~ model | lower) %}
{% do model_yaml.append(' description: ""') %}
{% do model_yaml.append(' columns:') %}

{% set relation=ref(model_name) %}
{%- set columns = adapter.get_columns_in_relation(relation) -%}
{% set relation=ref(model) %}
{%- set columns = adapter.get_columns_in_relation(relation) -%}

{% for column in columns %}
{% set model_yaml = codegen.generate_column_yaml(column, model_yaml, column_desc_dict) %}
{% endfor %}
{% for column in columns %}
{% set model_yaml = codegen.generate_column_yaml(column, model_yaml, column_desc_dict) %}
{% endfor %}
{% endfor %}

{% if execute %}

Expand Down