Skip to content

Commit

Permalink
add generate_surrogate_key macro throw deprecation warning in surroga…
Browse files Browse the repository at this point in the history
…te key
  • Loading branch information
dave-connors-3 committed Sep 23, 2022
1 parent ba7ee52 commit aad2868
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
56 changes: 56 additions & 0 deletions macros/sql/generate_surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{%- macro generate_surrogate_key(field_list) -%}
{# needed for safe_add to allow for non-keyword arguments see SO post #}
{# https://stackoverflow.com/questions/13944751/args-kwargs-in-jinja2-macros #}
{% set frustrating_jinja_feature = varargs %}
{{ return(adapter.dispatch('generate_surrogate_key', 'dbt_utils')(field_list, *varargs)) }}
{% endmacro %}

{%- macro default__generate_surrogate_key(field_list) -%}

{%- if varargs|length >= 1 or field_list is string %}

{%- set error_message = '
Warning: the `surrogate_key` macro now takes a single list argument instead of \
multiple string arguments. Support for multiple string arguments will be \
deprecated in a future release of dbt-utils. The {}.{} model triggered this warning. \
'.format(model.package_name, model.name) -%}

{%- do exceptions.warn(error_message) -%}

{# first argument is not included in varargs, so add first element to field_list_xf #}
{%- set field_list_xf = [field_list] -%}

{%- for field in varargs %}
{%- set _ = field_list_xf.append(field) -%}
{%- endfor -%}

{%- else -%}

{# if using list, just set field_list_xf as field_list #}
{%- set field_list_xf = field_list -%}

{%- endif -%}

{% if var('surrogate_key_treat_nulls_as_empty_strings', False) %}
{% set default_null_value = "" %}
{% else %}
{% set default_null_value = '_dbt_utils_surrogate_key_null_'%}
{% endif %}

{%- set fields = [] -%}

{%- for field in field_list_xf -%}

{%- set _ = fields.append(
"coalesce(cast(" ~ field ~ " as " ~ type_string() ~ "), '" ~ default_null_value ~"')"
) -%}

{%- if not loop.last %}
{%- set _ = fields.append("'-'") -%}
{%- endif -%}

{%- endfor -%}

{{ hash(concat(fields)) }}

{%- endmacro -%}
46 changes: 7 additions & 39 deletions macros/sql/surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -1,51 +1,19 @@
{%- macro surrogate_key(field_list) -%}
{# needed for safe_add to allow for non-keyword arguments see SO post #}
{# https://stackoverflow.com/questions/13944751/args-kwargs-in-jinja2-macros #}
{% set frustrating_jinja_feature = varargs %}
{{ return(adapter.dispatch('surrogate_key', 'dbt_utils')(field_list, *varargs)) }}
{% endmacro %}

{%- macro default__surrogate_key(field_list) -%}

{%- if varargs|length >= 1 or field_list is string %}

{%- set error_message = '
Warning: the `surrogate_key` macro now takes a single list argument instead of \
multiple string arguments. Support for multiple string arguments will be \
deprecated in a future release of dbt-utils. The {}.{} model triggered this warning. \
Warning: the `dbt_utils.surrogate_key` macro has been fully deprecated in favor of \
the new `generate_surrogate_key` macro. This new macro adjusts the default string value \
passed to the coalesce function. To achieve parity with the original macro, \
add a variable called `surrogate_key_treat_nulls_as_empty_strings` to your project with a \
value of True. \
The {}.{} model triggered this warning. \
'.format(model.package_name, model.name) -%}

{%- do exceptions.warn(error_message) -%}

{# first argument is not included in varargs, so add first element to field_list_xf #}
{%- set field_list_xf = [field_list] -%}

{%- for field in varargs %}
{%- set _ = field_list_xf.append(field) -%}
{%- endfor -%}

{%- else -%}

{# if using list, just set field_list_xf as field_list #}
{%- set field_list_xf = field_list -%}

{%- endif -%}


{%- set fields = [] -%}

{%- for field in field_list_xf -%}

{%- set _ = fields.append(
"coalesce(cast(" ~ field ~ " as " ~ type_string() ~ "), '')"
) -%}

{%- if not loop.last %}
{%- set _ = fields.append("'-'") -%}
{%- endif -%}

{%- endfor -%}

{{ hash(concat(fields)) }}
{%- do exceptions.raise_compiler_error(error_message) -%}

{%- endmacro -%}

0 comments on commit aad2868

Please sign in to comment.