From 28ec244fd6b10d5a3aa6be11a46dfabdd7cf62b9 Mon Sep 17 00:00:00 2001 From: "triedandtested-dev (Bryan Dunkley)" Date: Mon, 27 Sep 2021 23:13:26 +0100 Subject: [PATCH 1/4] Add unique_key to NodeConfig `unique_key` can be a string or a list. --- core/dbt/contracts/graph/model_config.py | 1 + 1 file changed, 1 insertion(+) diff --git a/core/dbt/contracts/graph/model_config.py b/core/dbt/contracts/graph/model_config.py index 7a5b621fbf2..8bd63c11d11 100644 --- a/core/dbt/contracts/graph/model_config.py +++ b/core/dbt/contracts/graph/model_config.py @@ -409,6 +409,7 @@ class NodeConfig(NodeAndTestConfig): metadata=MergeBehavior.Update.meta(), ) full_refresh: Optional[bool] = None + unique_key: Optional[Union[str, List[str]]] = None on_schema_change: Optional[str] = 'ignore' @classmethod From 90f13d398ff90ebbd89d9c968db2ed8088caa5ea Mon Sep 17 00:00:00 2001 From: "triedandtested-dev (Bryan Dunkley)" Date: Sun, 14 Nov 2021 19:32:47 +0000 Subject: [PATCH 2/4] merge.sql update to work with unique_key as list extend the functionality to support both single and multiple keys Signed-off-by: triedandtested-dev (Bryan Dunkley) --- .../macros/materializations/common/merge.sql | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/core/dbt/include/global_project/macros/materializations/common/merge.sql b/core/dbt/include/global_project/macros/materializations/common/merge.sql index 979bc7f27f6..95bf9b03913 100644 --- a/core/dbt/include/global_project/macros/materializations/common/merge.sql +++ b/core/dbt/include/global_project/macros/materializations/common/merge.sql @@ -22,10 +22,19 @@ {%- set sql_header = config.get('sql_header', none) -%} {% if unique_key %} - {% set unique_key_match %} - DBT_INTERNAL_SOURCE.{{ unique_key }} = DBT_INTERNAL_DEST.{{ unique_key }} - {% endset %} - {% do predicates.append(unique_key_match) %} + {% if unique_key is sequence and unique_key is not mapping and unique_key is not string %} + {% for key in unique_key %} + {% set this_key_match %} + DBT_INTERNAL_SOURCE.{{ key }} = DBT_INTERNAL_DEST.{{ key }} + {% endset %} + {% do predicates.append(this_key_match) %} + {% endfor %} + {% else %} + {% set unique_key_match %} + DBT_INTERNAL_SOURCE.{{ unique_key }} = DBT_INTERNAL_DEST.{{ unique_key }} + {% endset %} + {% do predicates.append(unique_key_match) %} + {% endif %} {% else %} {% do predicates.append('FALSE') %} {% endif %} From 98ca865c8d2a9fdb29668fa5dee07821d947d13d Mon Sep 17 00:00:00 2001 From: "triedandtested-dev (Bryan Dunkley)" Date: Sun, 14 Nov 2021 20:10:50 +0000 Subject: [PATCH 3/4] Updated test to include unique_key Signed-off-by: triedandtested-dev (Bryan Dunkley) --- test/integration/047_dbt_ls_test/test_ls.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/047_dbt_ls_test/test_ls.py b/test/integration/047_dbt_ls_test/test_ls.py index 90345e1e7ec..041a0b654ea 100644 --- a/test/integration/047_dbt_ls_test/test_ls.py +++ b/test/integration/047_dbt_ls_test/test_ls.py @@ -128,6 +128,7 @@ def expect_analyses_output(self): 'schema': None, 'alias': None, 'meta': {}, + 'unique_key': None }, 'unique_id': 'analysis.test.a', 'original_file_path': normalize('analyses/a.sql'), From ea9cbb15f1d5b24d6d95a93d405a2dccab386aaf Mon Sep 17 00:00:00 2001 From: "triedandtested-dev (Bryan Dunkley)" Date: Sun, 14 Nov 2021 20:14:16 +0000 Subject: [PATCH 4/4] updated tests Signed-off-by: triedandtested-dev (Bryan Dunkley) --- test/integration/029_docs_generate_tests/test_docs_generate.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/integration/029_docs_generate_tests/test_docs_generate.py b/test/integration/029_docs_generate_tests/test_docs_generate.py index 513adbf5c4f..0201660fde1 100644 --- a/test/integration/029_docs_generate_tests/test_docs_generate.py +++ b/test/integration/029_docs_generate_tests/test_docs_generate.py @@ -910,6 +910,7 @@ def rendered_model_config(self, **updates): 'full_refresh': None, 'on_schema_change': 'ignore', 'meta': {}, + 'unique_key': None, } result.update(updates) return result @@ -934,6 +935,7 @@ def rendered_seed_config(self, **updates): 'schema': None, 'alias': None, 'meta': {}, + 'unique_key': None, } result.update(updates) return result