Skip to content

Pre/post hooks fail at every run boundary: dbt-adapters' default run_hooks emits commit; that Fabric Warehouse cannot execute #391

@sdebruyn

Description

@sdebruyn

Summary

The adapter ships no materializations/hooks.sql, so dbt-adapters' default run_hooks macro runs unchanged. That default emits commit; to exit the implicit transaction wrapping a model. Fabric Warehouse does not support BEGIN/COMMIT TRAN. Every project that uses pre-hook or post-hook therefore fails at every run boundary.

Evidence (HEAD 0de2190, v1.10.0)

dbt/include/fabric/macros/materializations/ contains models/, snapshots/, seeds/, snapshot/, but no hooks.sql:

git ls-tree 0de2190 dbt/include/fabric/macros/materializations/ --name-only
# models/
# snapshot/
# snapshots/
# seeds/

No run_hooks override exists. dbt-adapters' default — at dbt/include/global_project/macros/materializations/hooks.sql — calls commit; between hook batches.

Reproduction

Add any pre-hook or post-hook to a model in a Fabric DW dbt project:

# dbt_project.yml
models:
  my_project:
    +pre-hook:
      - "select 1"

Run dbt run. The hook block fails with a T-SQL error around commit;.

User impact

pre-hook / post-hook is a core dbt feature (docs). Any project using it on Fabric Warehouse cannot run unmodified.

Suggested fix

Add dbt/include/fabric/macros/materializations/hooks.sql overriding run_hooks (the dbt-adapters macro is not dispatched, so the override is by name):

{% macro run_hooks(hooks, inside_transaction=True) %}
  {% for hook in hooks | selectattr('transaction', 'equalto', inside_transaction) %}
    {% set rendered = render(hook.get('sql')) | trim %}
    {% if (rendered | length) > 0 %}
      {% call statement(auto_begin=inside_transaction) %}
        {{ rendered }}
      {% endcall %}
    {% endif %}
  {% endfor %}
{% endmacro %}

This is the same pattern dbt-athena uses to drop the commit; for engines without transactional DDL semantics (reference).

A working version of this fix is running in the sdebruyn/dbt-fabric fork — commit 62705a00. A PR against this repo will follow.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions