Skip to content

Commit d8fb26f

Browse files
authored
Merge pull request dbt-msft#709 from dbt-msft/FIX/444-wrong-commit-post-hook
fix: guard run_hooks commit with @@trancount check for autocommit safety
2 parents 6b45227 + f713ed6 commit d8fb26f

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% macro run_hooks(hooks, inside_transaction=True) %}
2+
{% for hook in hooks | selectattr('transaction', 'equalto', inside_transaction) %}
3+
{% if not inside_transaction and loop.first %}
4+
{% call statement(auto_begin=inside_transaction) %}
5+
if @@trancount > 0 commit;
6+
{% endcall %}
7+
{% endif %}
8+
{% set rendered = render(hook.get('sql')) | trim %}
9+
{% if (rendered | length) > 0 %}
10+
{% call statement(auto_begin=inside_transaction) %}
11+
{{ rendered }}
12+
{% endcall %}
13+
{% endif %}
14+
{% endfor %}
15+
{% endmacro %}

tests/functional/adapter/dbt/test_hooks.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,28 @@ def test_pre_post_model_hooks_refed(self, project, dbt_profile_target):
232232

233233
class TestHookRefs(BaseHookRefs):
234234
pass
235+
236+
237+
class BaseAfterCommitModelHook(BaseTestPrePost):
238+
@pytest.fixture(scope="class")
239+
def project_config_update(self):
240+
return {
241+
"models": {
242+
"test": {
243+
"post-hook": [
244+
{"sql": "select 1", "transaction": False},
245+
],
246+
}
247+
}
248+
}
249+
250+
@pytest.fixture(scope="class")
251+
def models(self):
252+
return {"after_commit_hook_model.sql": "select 1 as id"}
253+
254+
def test_after_commit_post_hook_does_not_double_commit(self, project):
255+
run_dbt()
256+
257+
258+
class TestAfterCommitModelHook(BaseAfterCommitModelHook):
259+
pass

0 commit comments

Comments
 (0)