Skip to content

Commit

Permalink
Add project name to default search packages (#4114)
Browse files Browse the repository at this point in the history
* Add project name to default search packages

We prefer macros in the project over the ones in the namespace (package)

* Add change to change log

* Use project_name instead of project

* Raise compilation error if no macros are found

* Update change log line

* Add test for package macro override

* Add JCZuurmond to contributor

* Fix typos

* Add test that should not over ride the package

* Add doc string to tests
  • Loading branch information
JCZuurmond authored and jtcohen6 committed Oct 22, 2021
1 parent 61caa79 commit 17d7508
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
- Switch `unique_field` from abstractproperty to optional property. Add docstring ([#4025](https://github.com/dbt-labs/dbt-core/issues/4025), [#4028](https://github.com/dbt-labs/dbt-core/pull/4028))
- 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))
- 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

0 comments on commit 17d7508

Please sign in to comment.