Skip to content

Commit

Permalink
Merge pull request #91 from dbt-labs/faith_ro_exploring
Browse files Browse the repository at this point in the history
Feat: Add sample macro that overrides model yaml macro in "samples" folder
  • Loading branch information
bethanyhipple-dbtlabs authored Jun 10, 2024
2 parents d71e235 + fe7ccd8 commit 7dabd82
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
56 changes: 56 additions & 0 deletions models/_samples/override_naming_model_yaml.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--this model override allows users to avoid dbt's "auto lowercasing" behavior when using codegen to generate model yaml.
--To actually use this macro, put it in the macros folder and name it generate_model_yaml.sql.
{% 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 %}
-- removing "lower" from whole macro
{% do model_yaml.append(' - name: ' ~ column.name) %}
{% do model_yaml.append(' description: "' ~ column_desc_dict.get(column.name) ~ '"') %}
{% 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 ) %}
{% 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 %}
16 changes: 16 additions & 0 deletions package-lock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
packages:
- package: dbt-labs/codegen
version: 0.9.0
- package: dbt-labs/dbt_utils
version: 1.1.0
- package: dbt-labs/audit_helper
version: 0.9.0
- package: brooklyn-data/dbt_artifacts
version: 2.6.2
- package: dbt-labs/dbt_project_evaluator
version: 0.6.0
- package: calogica/dbt_expectations
version: 0.8.5
- package: calogica/dbt_date
version: 0.7.2
sha1_hash: a17b7bdcdb4efda257369bdfd548f5104eb383a3

0 comments on commit 7dabd82

Please sign in to comment.