From e984cafdaaf95078d2af60e6b6aadf4b9654395f Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Thu, 14 Dec 2017 14:07:11 -0500 Subject: [PATCH 1/2] Fix for pre-hooks outside of transactions https://github.com/fishtown-analytics/dbt/issues/576 --- .../global_project/macros/materializations/helpers.sql | 5 +++++ 1 file changed, 5 insertions(+) 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 %} From 1139955e0806c164e9a2b068d02f07ca4b2b5f00 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Thu, 14 Dec 2017 14:23:43 -0500 Subject: [PATCH 2/2] improve tests --- .../014_hook_tests/test_model_hooks.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) 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, + ] } } }