Repository: dbt-labs/dbt-core

Issue:
Enable deprecation: adapter-macro
## adapter-macro

Introduced in: v0.18
In the last week, raised by: 203 projects (~3%)

Pretty simple: we deprecated the `adapter_macro` context member in favor of `adapter.dispatch`, and the former currently serves as a passthrough to the latter:

https://github.com/dbt-labs/dbt/blob/484517416f3ec7affb22f3f1be4f19b2ef825332/core/dbt/context/providers.py#L1152-L1212

Let's keep `adapter_macro`, but change it to immediately raise an exception that encourages users to switch it out for `adapter.dispatch` instead (perhaps with a link to https://docs.getdbt.com/reference/dbt-jinja-functions/dispatch). Then, we can remove `adapter-macro` from the list of active deprecations.


_Originally posted by @jtcohen6 in https://github.com/dbt-labs/dbt/issues/2798#issuecomment-919998174_


Discussion:

Resolving patch:
diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py
--- a/core/dbt/context/providers.py
+++ b/core/dbt/context/providers.py
@@ -1151,65 +1151,17 @@ def sql_now(self) -> str:
 
     @contextmember
     def adapter_macro(self, name: str, *args, **kwargs):
-        """Find the most appropriate macro for the name, considering the
-        adapter type currently in use, and call that with the given arguments.
-
-        If the name has a `.` in it, the first section before the `.` is
-        interpreted as a package name, and the remainder as a macro name.
-
-        If no adapter is found, raise a compiler exception. If an invalid
-        package name is specified, raise a compiler exception.
-
-
-        Some examples:
-
-            {# dbt will call this macro by name, providing any arguments #}
-            {% macro create_table_as(temporary, relation, sql) -%}
-
-              {# dbt will dispatch the macro call to the relevant macro #}
-              {{ adapter_macro('create_table_as', temporary, relation, sql) }}
-            {%- endmacro %}
-
-
-            {#
-                If no macro matches the specified adapter, "default" will be
-                used
-            #}
-            {% macro default__create_table_as(temporary, relation, sql) -%}
-               ...
-            {%- endmacro %}
-
-
-
-            {# Example which defines special logic for Redshift #}
-            {% macro redshift__create_table_as(temporary, relation, sql) -%}
-               ...
-            {%- endmacro %}
-
-
-
-            {# Example which defines special logic for BigQuery #}
-            {% macro bigquery__create_table_as(temporary, relation, sql) -%}
-               ...
-            {%- endmacro %}
+        """This was deprecated in v0.18 in favor of adapter.dispatch
         """
-        deprecations.warn('adapter-macro', macro_name=name)
-        original_name = name
-        package_name = None
-        if '.' in name:
-            package_name, name = name.split('.', 1)
-
-        try:
-            macro = self.db_wrapper.dispatch(
-                macro_name=name, macro_namespace=package_name
-            )
-        except CompilationException as exc:
-            raise CompilationException(
-                f'In adapter_macro: {exc.msg}\n'
-                f"    Original name: '{original_name}'",
-                node=self.model
-            ) from exc
-        return macro(*args, **kwargs)
+        msg = (
+            'The "adapter_macro" macro has been deprecated. Instead, use '
+            'the `adapter.dispatch` method to find a macro and call the '
+            'result.  For more information, see: '
+            'https://docs.getdbt.com/reference/dbt-jinja-functions/dispatch)'
+            ' adapter_macro was called for: {macro_name}'
+            .format(macro_name=name)
+        )
+        raise CompilationException(msg)
 
 
 class MacroContext(ProviderContext):
diff --git a/core/dbt/deprecations.py b/core/dbt/deprecations.py
--- a/core/dbt/deprecations.py
+++ b/core/dbt/deprecations.py
@@ -82,15 +82,6 @@ class ModelsKeyNonModelDeprecation(DBTDeprecation):
     '''
 
 
-class AdapterMacroDeprecation(DBTDeprecation):
-    _name = 'adapter-macro'
-    _description = '''\
-    The "adapter_macro" macro has been deprecated. Instead, use the
-    `adapter.dispatch` method to find a macro and call the result.
-    adapter_macro was called for: {macro_name}
-    '''
-
-
 class PackageRedirectDeprecation(DBTDeprecation):
     _name = 'package-redirect'
     _description = '''\
@@ -140,7 +131,6 @@ def warn(name, *args, **kwargs):
     DispatchPackagesDeprecation(),
     NotADictionaryDeprecation(),
     ModelsKeyNonModelDeprecation(),
-    AdapterMacroDeprecation(),
     PackageRedirectDeprecation()
 ]
 


Test patch:
diff --git a/test/integration/012_deprecation_tests/adapter-macro-macros/macros.sql b/test/integration/012_deprecation_tests/adapter-macro-macros/macros.sql
deleted file mode 100644
--- a/test/integration/012_deprecation_tests/adapter-macro-macros/macros.sql
+++ /dev/null
@@ -1,17 +0,0 @@
-{% macro some_macro(arg1, arg2) -%}
-    {{ adapter_macro('some_macro', arg1, arg2) }}
-{%- endmacro %}
-
-
-{% macro default__some_macro(arg1, arg2) %}
-    {% do exceptions.raise_compiler_error('not allowed') %}
-{% endmacro %}
-
-{% macro postgres__some_macro(arg1, arg2) -%}
-    {{ arg1 }}{{ arg2 }}
-{%- endmacro %}
-
-
-{% macro some_other_macro(arg1, arg2) -%}
-	{{ adapter_macro('test.some_macro', arg1, arg2) }}
-{%- endmacro %}
diff --git a/test/integration/012_deprecation_tests/test_deprecations.py b/test/integration/012_deprecation_tests/test_deprecations.py
--- a/test/integration/012_deprecation_tests/test_deprecations.py
+++ b/test/integration/012_deprecation_tests/test_deprecations.py
@@ -35,97 +35,6 @@ def test_postgres_deprecations(self):
         self.assertEqual(expected, deprecations.active_deprecations)
 
 
-class TestAdapterMacroDeprecation(BaseTestDeprecations):
-    @property
-    def models(self):
-        return self.dir('adapter-macro-models')
-
-    @property
-    def project_config(self):
-        return {
-            'config-version': 2,
-            'macro-paths': [self.dir('adapter-macro-macros')]
-        }
-
-    @use_profile('postgres')
-    def test_postgres_adapter_macro(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        self.run_dbt()
-        expected = {'adapter-macro'}
-        self.assertEqual(expected, deprecations.active_deprecations)
-
-    @use_profile('postgres')
-    def test_postgres_adapter_macro_fail(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        with self.assertRaises(dbt.exceptions.CompilationException) as exc:
-            self.run_dbt(['--warn-error', 'run'])
-        exc_str = ' '.join(str(exc.exception).split())  # flatten all whitespace
-        assert 'The "adapter_macro" macro has been deprecated' in exc_str
-
-    @use_profile('redshift')
-    def test_redshift_adapter_macro(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        # pick up the postgres macro
-        self.run_dbt()
-        expected = {'adapter-macro'}
-        self.assertEqual(expected, deprecations.active_deprecations)
-        
-    @use_profile('bigquery')
-    def test_bigquery_adapter_macro(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        # picked up the default -> error
-        with self.assertRaises(dbt.exceptions.CompilationException) as exc:
-            self.run_dbt(expect_pass=False)
-        exc_str = ' '.join(str(exc.exception).split())  # flatten all whitespace
-        assert 'not allowed' in exc_str  # we saw the default macro
-
-
-class TestAdapterMacroDeprecationPackages(BaseTestDeprecations):
-    @property
-    def models(self):
-        return self.dir('adapter-macro-models-package')
-
-    @property
-    def project_config(self):
-        return {
-            'config-version': 2,
-            'macro-paths': [self.dir('adapter-macro-macros')]
-        }
-
-    @use_profile('postgres')
-    def test_postgres_adapter_macro_pkg(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        self.run_dbt()
-        expected = {'adapter-macro'}
-        self.assertEqual(expected, deprecations.active_deprecations)
-
-    @use_profile('postgres')
-    def test_postgres_adapter_macro_pkg_fail(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        with self.assertRaises(dbt.exceptions.CompilationException) as exc:
-            self.run_dbt(['--warn-error', 'run'])
-        exc_str = ' '.join(str(exc.exception).split())  # flatten all whitespace
-        assert 'The "adapter_macro" macro has been deprecated' in exc_str
-
-    @use_profile('redshift')
-    def test_redshift_adapter_macro_pkg(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        # pick up the postgres macro
-        self.assertEqual(deprecations.active_deprecations, set())
-        self.run_dbt()
-        expected = {'adapter-macro'}
-        self.assertEqual(expected, deprecations.active_deprecations)
-
-    @use_profile('bigquery')
-    def test_bigquery_adapter_macro_pkg(self):
-        self.assertEqual(deprecations.active_deprecations, set())
-        # picked up the default -> error
-        with self.assertRaises(dbt.exceptions.CompilationException) as exc:
-            self.run_dbt(expect_pass=False)
-        exc_str = ' '.join(str(exc.exception).split())  # flatten all whitespace
-        assert 'not allowed' in exc_str  # we saw the default macro
-
-
 class TestDispatchPackagesDeprecation(BaseTestDeprecations):
     @property
     def models(self):
diff --git a/test/integration/012_deprecation_tests/adapter-macro-models/model.sql b/test/integration/016_macro_tests/deprecated-adapter-macro-models/model.sql
similarity index 100%
rename from test/integration/012_deprecation_tests/adapter-macro-models/model.sql
rename to test/integration/016_macro_tests/deprecated-adapter-macro-models/model.sql
diff --git a/test/integration/016_macro_tests/deprecated-adapter-macro/model.sql b/test/integration/016_macro_tests/deprecated-adapter-macro/model.sql
new file mode 100644
--- /dev/null
+++ b/test/integration/016_macro_tests/deprecated-adapter-macro/model.sql
@@ -0,0 +1,3 @@
+{% macro some_macro(arg1, arg2) -%}
+    {{ adapter_macro('some_macro', arg1, arg2) }}
+{%- endmacro %}
diff --git a/test/integration/016_macro_tests/test_macros.py b/test/integration/016_macro_tests/test_macros.py
--- a/test/integration/016_macro_tests/test_macros.py
+++ b/test/integration/016_macro_tests/test_macros.py
@@ -164,3 +164,28 @@ def packages_config(self):
     def test_postgres_overrides(self):
         self.run_dbt(["deps"])
         super().test_postgres_overrides()
+
+
+class TestAdapterMacroDeprecated(DBTIntegrationTest):
+
+    @property
+    def schema(self):
+        return "test_macros_016"
+
+    @property
+    def models(self):
+        return "deprecated-adapter-macro-models"
+
+    @property
+    def project_config(self):
+        return {
+            'config-version': 2,
+            "macro-paths": ["deprecated-adapter-macro"]
+        }
+
+    @use_profile('postgres')
+    def test_postgres_invalid_macro(self):
+        with pytest.raises(dbt.exceptions.CompilationException) as exc:
+            self.run_dbt(['run'])
+
+        assert 'The "adapter_macro" macro has been deprecated' in str(exc.value)