Skip to content

Commit

Permalink
use full manifest in adapter instead of macro_manifest (#10609)
Browse files Browse the repository at this point in the history
* use full manifest in adapter instead of macro_manifest

* Add test case

* Add changelog entry

* Remove commented code.

---------

Co-authored-by: Peter Allen Webb <peter.webb@dbtlabs.com>
  • Loading branch information
gshank and peterallenwebb authored Aug 29, 2024
1 parent 555ff80 commit 9b7f4ff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240829-105701.yaml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 2 additions & 3 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions tests/functional/context_methods/test_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

0 comments on commit 9b7f4ff

Please sign in to comment.