Skip to content

Commit

Permalink
Enable custom overrides of data type formatting via multiple dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeatty10 committed Sep 25, 2023
1 parent c3abd44 commit 6f6aea6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@

## 🚨 Breaking change

- `include_data_types` parameter added to `generate_model_yaml` and behavior changed for `generate_source`. Both default to `true`
`include_data_types` parameter added to `generate_model_yaml` and behavior changed for `generate_source`. Both default to `true`
and are lowercase to align with the dbt style guide. Scale & precision are **not** included. Previous logic for `generate_source` defaulted to `false` and the resulting data types were uppercase and included scale & precision ([#122](https://github.com/dbt-labs/dbt-codegen/pull/122)).

[Dispatch](https://docs.getdbt.com/reference/dbt-jinja-functions/dispatch) can be used to utilize the column data type formatting of previous versions. Namely, by adding this macro to your project:
```sql
{% macro default__data_type_format_source(column) %}
{{ return(column.data_type | upper) }}
{% endmacro %}
```

And then adding this within `dbt_project.yml`:
```yaml
dispatch:
- macro_namespace: codegen
search_order: ['my_project', 'codegen']
```
## New features
- 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.
Expand Down
3 changes: 1 addition & 2 deletions macros/generate_model_yaml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

{% do model_yaml.append(' - name: ' ~ column_name | lower ) %}
{% if include_data_types %}
{% set formatted = codegen.format_column(column) %}
{% do model_yaml.append(' data_type: ' ~ formatted['data_type'] | lower) %}
{% do model_yaml.append(' data_type: ' ~ codegen.data_type_format_model(column)) %}
{% endif %}
{% do model_yaml.append(' description: "' ~ column_desc_dict.get(column.name | lower,'') ~ '"') %}
{% do model_yaml.append('') %}
Expand Down
3 changes: 1 addition & 2 deletions macros/generate_source.sql
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
{% for column in columns %}
{% do sources_yaml.append(' - name: ' ~ column.name | lower ) %}
{% if include_data_types %}
{% set formatted = codegen.format_column(column) %}
{% do sources_yaml.append(' data_type: ' ~ formatted['data_type'] | lower) %}
{% do sources_yaml.append(' data_type: ' ~ codegen.data_type_format_source(column)) %}
{% endif %}
{% if include_descriptions %}
{% do sources_yaml.append(' description: ""' ) %}
Expand Down
22 changes: 21 additions & 1 deletion macros/helpers/helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,24 @@
{% endfor %}
{% endif %}
{{ return(model_names) }}
{% endmacro %}
{% endmacro %}

{% macro data_type_format_source(column) -%}
{{ return(adapter.dispatch('data_type_format_source', 'codegen')(column)) }}
{%- endmacro %}

{# format a column data type for a source #}
{% macro default__data_type_format_source(column) %}
{% set formatted = codegen.format_column(column) %}
{{ return(formatted['data_type'] | lower) }}
{% endmacro %}

{% macro data_type_format_model(column) -%}
{{ return(adapter.dispatch('data_type_format_model', 'codegen')(column)) }}
{%- endmacro %}

{# format a column data type for a model #}
{% macro default__data_type_format_model(column) %}
{% set formatted = codegen.format_column(column) %}
{{ return(formatted['data_type'] | lower) }}
{% endmacro %}

0 comments on commit 6f6aea6

Please sign in to comment.