From db6b0f604b8fe4b477a37ea609336b5ea56d4441 Mon Sep 17 00:00:00 2001 From: Yu Ishikawa Date: Mon, 22 Aug 2022 21:39:00 +0900 Subject: [PATCH] Dynamically switch references for `model_run_results_v4` (#50) Signed-off-by: Yu ISHIKAWA Signed-off-by: Yu ISHIKAWA --- .../macros/get_dbt_minor_version.sql | 28 +++++++++++++++++++ .../model_run_results_v4.sql | 11 ++++++++ 2 files changed, 39 insertions(+) create mode 100644 dbt_artifacts_models/macros/get_dbt_minor_version.sql diff --git a/dbt_artifacts_models/macros/get_dbt_minor_version.sql b/dbt_artifacts_models/macros/get_dbt_minor_version.sql new file mode 100644 index 0000000..fc34ff6 --- /dev/null +++ b/dbt_artifacts_models/macros/get_dbt_minor_version.sql @@ -0,0 +1,28 @@ +{% macro get_dbt_minor_version(version) %} + {% set re = modules.re %} + {% set pattern = '[\.][^\.]+$' %} + {% set dbt_minor_version = re.sub(pattern, '', version) %} + {{ return(dbt_minor_version) }} +{% endmacro %} + +{% macro test_get_dbt_minor_version() %} + {% set result = get_dbt_minor_version("1.0.3") %} + {% if result != "1.0" %} + {{ exceptions.raise_compiler_error("Expected 1.0, but " ~ result) }} + {% endif %} + + {% set result = get_dbt_minor_version("1.1.0") %} + {% if result != "1.1" %} + {{ exceptions.raise_compiler_error("Expected 1.1, but " ~ result) }} + {% endif %} + + {% set result = get_dbt_minor_version("1.1.1") %} + {% if result != "1.1" %} + {{ exceptions.raise_compiler_error("Expected 1.1, but " ~ result) }} + {% endif %} + + {% set result = get_dbt_minor_version("1.2.0-rc1") %} + {% if result != "1.2" %} + {{ exceptions.raise_compiler_error("Expected 1.2, but " ~ result) }} + {% endif %} +{% endmacro %} diff --git a/dbt_artifacts_models/models/marts/v4/run_results_v4/model_run_results_v4/model_run_results_v4.sql b/dbt_artifacts_models/models/marts/v4/run_results_v4/model_run_results_v4/model_run_results_v4.sql index 0113fb6..61d7052 100644 --- a/dbt_artifacts_models/models/marts/v4/run_results_v4/model_run_results_v4/model_run_results_v4.sql +++ b/dbt_artifacts_models/models/marts/v4/run_results_v4/model_run_results_v4/model_run_results_v4.sql @@ -1,3 +1,6 @@ +{% set re = modules.re %} +{% set dbt_minor_version = get_dbt_minor_version(version=dbt_version) %} + {% set project = var('dbt_artifacts_loader')['project'] %} {% set dataset = var('dbt_artifacts_loader')['dataset'] %} @@ -22,9 +25,17 @@ WITH run_results AS ( run_results.*, (SELECT AS STRUCT models.*) AS model, FROM {{ ref("expanded_run_results_v4") }} AS run_results + {% if dbt_minor_version == "1.0" %} LEFT OUTER JOIN {{ ref("parsed_model_node_v4") }} AS models ON run_results.unique_id = models.unique_id AND ABS(DATETIME_DIFF(run_results.metadata.generated_at, models.metadata.generated_at, DAY)) <= 2 + {% elif dbt_minor_version == "1.1" %} + LEFT OUTER JOIN {{ ref("parsed_model_node_v5") }} AS models + ON run_results.unique_id = models.unique_id + AND ABS(DATETIME_DIFF(run_results.metadata.generated_at, models.metadata.generated_at, DAY)) <= 2 + {% else %} + {{ exceptions.raise_compiler_error("Unexpected dbt version: " ~ dbt_minor_version) }} + {% endif %} WHERE models.unique_id IS NOT NULL AND timing_name IN ("execute") )