diff --git a/dbt/include/global_project/macros/materializations/helpers.sql b/dbt/include/global_project/macros/materializations/helpers.sql index f6e0563dd1b..1a20316c2f3 100644 --- a/dbt/include/global_project/macros/materializations/helpers.sql +++ b/dbt/include/global_project/macros/materializations/helpers.sql @@ -1,5 +1,10 @@ {% macro run_hooks(hooks, inside_transaction=True) %} {% for hook in hooks | selectattr('transaction', 'equalto', inside_transaction) %} + {% if not inside_transaction and loop.first %} + {% call statement(auto_begin=inside_transaction) %} + commit; + {% endcall %} + {% endif %} {% call statement(auto_begin=inside_transaction) %} {{ hook.get('sql') }} {% endcall %} diff --git a/test/integration/014_hook_tests/test_model_hooks.py b/test/integration/014_hook_tests/test_model_hooks.py index 27e3174ba54..fa5ed397a16 100644 --- a/test/integration/014_hook_tests/test_model_hooks.py +++ b/test/integration/014_hook_tests/test_model_hooks.py @@ -95,8 +95,21 @@ def project_config(self): 'macro-paths': ['test/integration/014_hook_tests/macros'], 'models': { 'test': { - 'pre-hook': MODEL_PRE_HOOK, - 'post-hook': MODEL_POST_HOOK, + 'pre-hook': [ + # inside transaction (runs second) + MODEL_PRE_HOOK, + + # outside transaction (runs first) + {"sql": "vacuum {{ this.schema }}.on_model_hook", "transaction": False}, + ], + + 'post-hook':[ + # outside transaction (runs second) + {"sql": "vacuum {{ this.schema }}.on_model_hook", "transaction": False}, + + # inside transaction (runs first) + MODEL_POST_HOOK, + ] } } }