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

[Backport] #4114 #4117

Merged
merged 2 commits into from
Oct 22, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
- Fix multiple partial parsing errors ([#3996](https://github.com/dbt-labs/dbt/issues/3006), [#4020](https://github.com/dbt-labs/dbt/pull/4018))
- Include only relational nodes in `database_schema_set` ([#4063](https://github.com/dbt-labs/dbt-core/issues/4063), [#4077](https://github.com/dbt-labs/dbt-core/pull/4077))
- Fixed bug with `error_if` test option ([#4070](https://github.com/dbt-labs/dbt-core/pull/4070))
- Prefer macros defined in the project over the ones in a package by default ([#4106](https://github.com/dbt-labs/dbt-core/issues/4106), [#4114](https://github.com/dbt-labs/dbt-core/pull/4114))

Contributors:
- [@ljhopkins2](https://github.com/ljhopkins2) ([#4077](https://github.com/dbt-labs/dbt-core/pull/4077))
- [@JCZuurmond](https://github.com/jczuurmond) ([#4114](https://github.com/dbt-labs/dbt-core/pull/4114))

## dbt 0.21.0 (October 04, 2021)

Expand Down
10 changes: 5 additions & 5 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def dispatch(
elif isinstance(namespace, str):
search_packages = self._adapter.config.get_macro_search_order(namespace)
if not search_packages and namespace in self._adapter.config.dependencies:
search_packages = [namespace]
search_packages = [self.config.project_name, namespace]
if not search_packages:
raise CompilationException(
f'In adapter.dispatch, got a string packages argument '
Expand All @@ -164,10 +164,10 @@ def dispatch(
macro = self._namespace.get_from_package(
package_name, search_name
)
except CompilationException as exc:
raise CompilationException(
f'In dispatch: {exc.msg}',
) from exc
except CompilationException:
# Only raise CompilationException if macro is not found in
# any package
macro = None

if package_name is None:
attempts.append(search_name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro postgres__get_columns_in_relation(relation) %}
{{ return('a string') }}
{% endmacro %}
58 changes: 58 additions & 0 deletions test/integration/016_macro_tests/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,64 @@ def test_postgres_overrides(self):
self.run_dbt()


class TestMacroOverridePackage(DBTIntegrationTest):
"""
The macro in `override-postgres-get-columns-macros` should override the
`get_columns_in_relation` macro by default.
"""

@property
def schema(self):
return "test_macros_016"

@property
def models(self):
return 'override-get-columns-models'

@property
def project_config(self):
return {
'config-version': 2,
'macro-paths': ['override-postgres-get-columns-macros'],
}

@use_profile('postgres')
def test_postgres_overrides(self):
# the first time, the model doesn't exist
self.run_dbt()
self.run_dbt()


class TestMacroNotOverridePackage(DBTIntegrationTest):
"""
The macro in `override-postgres-get-columns-macros` does NOT override the
`get_columns_in_relation` macro because we tell dispatch to not look at the
postgres macros.
"""

@property
def schema(self):
return "test_macros_016"

@property
def models(self):
return 'override-get-columns-models'

@property
def project_config(self):
return {
'config-version': 2,
'macro-paths': ['override-postgres-get-columns-macros'],
'dispatch': [{'macro_namespace': 'dbt', 'search_order': ['dbt']}],
}

@use_profile('postgres')
def test_postgres_overrides(self):
# the first time, the model doesn't exist
self.run_dbt(expect_pass=False)
self.run_dbt(expect_pass=False)


class TestDispatchMacroOverrideBuiltin(TestMacroOverrideBuiltin):
# test the same functionality as above, but this time,
# dbt.get_columns_in_relation will dispatch to a default__ macro
Expand Down