From f1889240b1dc504b13627e5aa87b46aaffd4191e Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Mon, 26 Aug 2024 16:08:58 -0400 Subject: [PATCH 1/4] use full manifest in adapter instead of macro_manifest --- core/dbt/parser/manifest.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py index 7e37a0e5417..293653a8681 100644 --- a/core/dbt/parser/manifest.py +++ b/core/dbt/parser/manifest.py @@ -1028,12 +1028,12 @@ def build_manifest_state_check(self): return state_check def save_macros_to_adapter(self, adapter): - macro_manifest = MacroManifest(self.manifest.macros) - adapter.set_macro_resolver(macro_manifest) + # macro_manifest = MacroManifest(self.manifest.macros) + adapter.set_macro_resolver(self.manifest) # This executes the callable macro_hook and sets the # query headers # This executes the callable macro_hook and sets the query headers - query_header_context = generate_query_header_context(adapter.config, macro_manifest) + query_header_context = generate_query_header_context(adapter.config, self.manifest) self.macro_hook(query_header_context) # This creates a MacroManifest which contains the macros in From 394b024ffa08d850efdafd69c4d275b850c67dba Mon Sep 17 00:00:00 2001 From: Peter Allen Webb Date: Thu, 29 Aug 2024 10:40:50 -0400 Subject: [PATCH 2/4] Add test case --- .../context_methods/test_env_vars.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/functional/context_methods/test_env_vars.py b/tests/functional/context_methods/test_env_vars.py index 30c56551c09..d1441dcb7e2 100644 --- a/tests/functional/context_methods/test_env_vars.py +++ b/tests/functional/context_methods/test_env_vars.py @@ -193,3 +193,36 @@ def test_env_vars_secrets(self, project): assert not ("secret_variable" in log_output) assert "regular_variable" in log_output del os.environ["DBT_DEBUG"] + + +class TestEnvVarInCreateSchema: + """Test that the env_var() method works in overrides of the create_schema + macro, which is called during a different phase of execution than most + macros, causing problems.""" + + @pytest.fixture(scope="class", autouse=True) + def setup(self): + os.environ["DBT_TEST_ENV_VAR"] = "1" + + @pytest.fixture(scope="class") + def macros(self): + return { + "macros.sql": """ + {% macro create_schema(relation) %} + {%- call statement('create_schema') -%} + SELECT {{ env_var('DBT_TEST_ENV_VAR') }} as TEST + {% endcall %} + {% endmacro %}% + """ + } + + @pytest.fixture(scope="class") + def models(self): + return { + "mymodel.sql": """ + SELECT 1 as TEST -- {%- do adapter.create_schema(this) -%} + """ + } + + def test_env_var_in_create_schema(self, project): + run_dbt(["run"]) From 2d02438bf0139e219d7bd12909b16135877eb9e7 Mon Sep 17 00:00:00 2001 From: Peter Allen Webb Date: Thu, 29 Aug 2024 10:57:52 -0400 Subject: [PATCH 3/4] Add changelog entry --- .changes/unreleased/Fixes-20240829-105701.yaml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changes/unreleased/Fixes-20240829-105701.yaml diff --git a/.changes/unreleased/Fixes-20240829-105701.yaml b/.changes/unreleased/Fixes-20240829-105701.yaml new file mode 100644 index 00000000000..170f2463fa2 --- /dev/null +++ b/.changes/unreleased/Fixes-20240829-105701.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: Allow the use of env_var function in certain macros in which it was previously + unavailable. +time: 2024-08-29T10:57:01.160613-04:00 +custom: + Author: peterallenwebb + Issue: "10609" From 0bc31c63e07d608699c5780e2c4312421526dc71 Mon Sep 17 00:00:00 2001 From: Peter Allen Webb Date: Thu, 29 Aug 2024 10:59:56 -0400 Subject: [PATCH 4/4] Remove commented code. --- core/dbt/parser/manifest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py index 293653a8681..d54aa898713 100644 --- a/core/dbt/parser/manifest.py +++ b/core/dbt/parser/manifest.py @@ -1028,7 +1028,6 @@ def build_manifest_state_check(self): return state_check def save_macros_to_adapter(self, adapter): - # macro_manifest = MacroManifest(self.manifest.macros) adapter.set_macro_resolver(self.manifest) # This executes the callable macro_hook and sets the # query headers