I would like to run a DELETE statement before the execution of my model. My model is called ga_events
. The DELETE statement should be based on another model called stg_events_inc
. There fore I created a macro:
{%- macro get_delete_statement(table, stg_table) -%}
DELETE FROM {{ table }} WHERE event_date BETWEEN (SELECT MIN(event_date) FROM {{ stg_table }}) AND (SELECT MAX(event_date) FROM {{ stg_table }});
{%- endmacro -%}
I’m calling the macro from the ga_events
model in the config block, like so:
{{
config(
materialized = 'incremental',
pre_hook = get_delete_statement(this, ref('stg_events_inc')),
on_schema_change = 'fail'
)
}}
The final compilated DELETE statement looks like this:
DELETE FROM `project`.`schema`.`ga_events` WHERE event_date BETWEEN (SELECT MIN(event_date) FROM `project`.`schema`.`ga_events`) AND (SELECT MAX(event_date) FROM `project`.`schema`.`ga_events`);
I cannot understand why it completely ignores the stg_events_inc reference in the WHERE clause.