From 17d7508f260fd4733fdfbe519bc0f145e2b403df Mon Sep 17 00:00:00 2001 From: Cor Date: Fri, 22 Oct 2021 13:15:31 +0000 Subject: [PATCH] Add project name to default search packages (#4114) * 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 --- CHANGELOG.md | 2 + core/dbt/context/providers.py | 10 ++-- .../macros.sql | 3 + .../016_macro_tests/test_macros.py | 58 +++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 test/integration/016_macro_tests/override-postgres-get-columns-macros/macros.sql diff --git a/CHANGELOG.md b/CHANGELOG.md index bc4bd140a96..0b6fb7f7e34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py index 6f20d287f49..69194bd7a78 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -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 ' @@ -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) diff --git a/test/integration/016_macro_tests/override-postgres-get-columns-macros/macros.sql b/test/integration/016_macro_tests/override-postgres-get-columns-macros/macros.sql new file mode 100644 index 00000000000..4de53ba4a18 --- /dev/null +++ b/test/integration/016_macro_tests/override-postgres-get-columns-macros/macros.sql @@ -0,0 +1,3 @@ +{% macro postgres__get_columns_in_relation(relation) %} + {{ return('a string') }} +{% endmacro %} diff --git a/test/integration/016_macro_tests/test_macros.py b/test/integration/016_macro_tests/test_macros.py index a88ec672447..336596fa594 100644 --- a/test/integration/016_macro_tests/test_macros.py +++ b/test/integration/016_macro_tests/test_macros.py @@ -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