forked from dbt-labs/dbt-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generate_surrogate_key macro throw deprecation warning in surroga…
…te key
- Loading branch information
1 parent
ba7ee52
commit aad2868
Showing
2 changed files
with
63 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%} |