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" diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py index 7e37a0e5417..d54aa898713 100644 --- a/core/dbt/parser/manifest.py +++ b/core/dbt/parser/manifest.py @@ -1028,12 +1028,11 @@ 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) + 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 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"])