Using Pre-Hooks to Define Table DDL

Hi @MegaMane , That does help a lot.

Unfortunately, I’m going to avoid directly answering the question at hand because I’d need to experiment with it. I have encountered strange situations where variables inside the config block evaluate differently than I would expect them to. I think this has to do with timing of when the config block is evaluated.

When I read your original post, I it reminded me of that timing issue I encountered in the past. dbt already evaluated “snapshot does not exist” at some point, then the pre-hook runs. I could be wrong, but as I said, I would have to play with it.

I will offer you another idea instead.

You can set a variable called table_exists. Then in the SQL you can do this pseudo code: if table_exists then do the self reference query thing, else do a different CTE that doesn’t self reference… end if.

You can set the table_exists variable by querying the information schema, or do what Claire did here ((use adapter.get_relation):

Hopefully this helps in some small way and sorry for not answering the original question!