forked from dbt-labs/dbt-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_model_yaml.sql
54 lines (42 loc) · 2 KB
/
generate_model_yaml.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{% macro generate_column_yaml(column, model_yaml, column_desc_dict, parent_column_name="") %}
{% if parent_column_name %}
{% set column_name = parent_column_name ~ "." ~ column.name %}
{% else %}
{% set column_name = column.name %}
{% endif %}
{% do model_yaml.append(' - name: ' ~ column_name | lower ) %}
{% do model_yaml.append(' description: "' ~ column_desc_dict.get(column.name | lower,'') ~ '"') %}
{% do model_yaml.append('') %}
{% if column.fields|length > 0 %}
{% for child_column in column.fields %}
{% set model_yaml = codegen.generate_column_yaml(child_column, model_yaml, column_desc_dict, parent_column_name=column_name) %}
{% endfor %}
{% endif %}
{% do return(model_yaml) %}
{% endmacro %}
{% macro generate_model_yaml(model_names=[], upstream_descriptions=False) %}
{% set model_yaml=[] %}
{% do model_yaml.append('version: 2') %}
{% do model_yaml.append('') %}
{% do model_yaml.append('models:') %}
{% if model_names is string %}
{{ exceptions.raise_compiler_error("The `model_names` argument must always be a list, even if there is only one model.") }}
{% else %}
{% for model in model_names %}
{% do model_yaml.append(' - name: ' ~ model | lower) %}
{% do model_yaml.append(' description: ""') %}
{% do model_yaml.append(' columns:') %}
{% set relation=ref(model) %}
{%- set columns = adapter.get_columns_in_relation(relation) -%}
{% set column_desc_dict = codegen.build_dict_column_descriptions(model) if upstream_descriptions else {} %}
{% for column in columns %}
{% set model_yaml = codegen.generate_column_yaml(column, model_yaml, column_desc_dict) %}
{% endfor %}
{% endfor %}
{% endif %}
{% if execute %}
{% set joined = model_yaml | join ('\n') %}
{{ log(joined, info=True) }}
{% do return(joined) %}
{% endif %}
{% endmacro %}