Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest surrogate key macro #298

Merged
merged 3 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions macros/generate_surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{#
Since folks commonly install dbt_artifacts alongside a myriad of other packages,
we copy the dbt_utils implementation of the surrogate_key macro so we don't have
any dependencies to make conflicts worse!

This version is:
URL: https://github.com/dbt-labs/dbt-utils/blob/main/macros/sql/generate_surrogate_key.sql
Commit SHA: eaa0e41b033bdf252eff0ae014ec11888f37ebff
Date: 2023-04-28
#}

{%- macro generate_surrogate_key(field_list) -%}
{# Note - update the reference to `dbt_utils` to `dbt_artifacts` here #}
{{ return(adapter.dispatch('generate_surrogate_key', 'dbt_artifacts')(field_list)) }}
{% endmacro %}

{%- macro default__generate_surrogate_key(field_list) -%}

{%- 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 -%}

{%- do fields.append(
"coalesce(cast(" ~ field ~ " as " ~ dbt.type_string() ~ "), '" ~ default_null_value ~"')"
) -%}

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

{%- endfor -%}

{{ dbt.hash(dbt.concat(fields)) }}

{%- endmacro -%}
56 changes: 0 additions & 56 deletions macros/surrogate_key.sql

This file was deleted.

2 changes: 1 addition & 1 deletion models/staging/stg_dbt__exposures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as exposure_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as exposure_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__model_executions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as model_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as model_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__models.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as model_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as model_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__seed_executions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as seed_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as seed_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__seeds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as seed_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as seed_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__snapshot_executions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as snapshot_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as snapshot_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__snapshots.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as snapshot_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as snapshot_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__sources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as source_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as source_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__test_executions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as test_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as test_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down
2 changes: 1 addition & 1 deletion models/staging/stg_dbt__tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ with base as (
enhanced as (

select
{{ dbt_artifacts.surrogate_key(['command_invocation_id', 'node_id']) }} as test_execution_id,
{{ dbt_artifacts.generate_surrogate_key(['command_invocation_id', 'node_id']) }} as test_execution_id,
command_invocation_id,
node_id,
run_started_at,
Expand Down