dbt-labs / dbt-codegen

Macros that generate dbt code
https://hub.getdbt.com/dbt-labs/codegen/latest/
Apache License 2.0
459 stars 99 forks source link

Leverage generate_model_yaml to create LookML code #131

Closed victorurbinajz closed 1 year ago

victorurbinajz commented 1 year ago

Describe the feature

Describe alternatives you've considered

{% macro generate_view_code_looker(model_names=[], upstream_descriptions=False, include_data_types=True) %}

{% set model_yaml=[] %}
{% do model_yaml.append('\n') %}    

{% 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('view: ' ~ model ~ ' {') %}
        {% do model_yaml.append('  sql_table_name: analytics.' ~ model ~ ' ;;') %}
        {% do model_yaml.append('\n') %}
        {% 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 = generate_column_looker(column, model_yaml, column_desc_dict, include_data_types) -%}
        {% endfor %}
    {% endfor %}
{% endif %}
{% do model_yaml.append('\n') %}
{% do model_yaml.append('}') %}

{% if execute %}

{% set joined = model_yaml | join ('\n') -%}
{{ log(joined, info=True) }}
{% do return(joined) %}

{% endif %}

{% endmacro %}


- How to run this macro?

dbt run-operation generate_view_code_looker --args '{"model_names": ["model_name"]}'

Alternatively, the code can be put into a yml file

dbt run-operation generate_view_code_looker --args '{"model_names": ["model_name"]}' > sample_file.yml



### Additional context
I'm using Snowflake, that's the only place where I've tested this macro.

### Who will this benefit?
- Use case: this macro can be used to create the LookML basic  code to create Looker views. It's a good starting point because it already adds the data type and declares all the dimensions available.

### Are you interested in contributing this feature?
- For sure. I'd love to contribute.
graciegoheen commented 1 year ago

Hey! If you're adding contacts to your dbt models, you may want to check out the dbt-meshify package (specifically the add-contract command).

The scope of the dbt-codegen package is generating code for use within your dbt project. Since LookML code is separate from a dbt project, I'm going to close this issue for now.