Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use full manifest in adapter instead of macro_manifest #10609

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)

Check warning on line 1031 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L1031

Added line #L1031 was not covered by tests
# 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)

Check warning on line 1035 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L1035

Added line #L1035 was not covered by tests
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"])
Loading