Hi,
I am running an incremental model in DBT as per my requirement but I have to take care of one extra step where I have to do an update before my main query block is executed.
I can use pre_hook to do an update which will solve my problem but when i run this incremental model for the first time in DBT, the table would not have existed and my pre_hook would fail.
Any suggestion on how I can handle this scenario, by enabling my pre_hook only after checking if the table structure exists would be helpful.
What if in your pre-hook you call a macro with the is_incremental() function inside of it?
I had a similar situation doing a delete before reloading. You can add the following code to your pre-hook macro. It defines a relation that can then be assessed for whether it exists:
{%- set target_relation = adapter.get_relation(
database=this.database,
schema=this.schema,
identifier=this.name) -%}
{%- set table_exists=target_relation is not none -%}
{%- if table_exists -%}
[do your update here]
{%- endif -%}
1 Like
Thanks @ScottT I will try that out
Hi @ScottT. Does this work in a prehook which is only computed at compilation? I seem to be having issues with this in a macro and a prehook
Yes, it would be the basis of a macro called by a pre or post hook. I know there are limitations as to when things are compiled versus what can be dynamic. A reference could be passed rather than using this if that helps.