Replies: 1 comment 4 replies
-
Is there still no way to deal with this issue? |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The following apply to all configs (
tags
,hooks
,snowflake_warehouse
,incremental_strategy
, etc) and not just theenabled
config which is the initial inspiration for this writeup.From time to time, we may want to skip the run of a model based on some dynamic condition instead of statically setting the
enabled
flag:I believe there's only a couple of ways we can have the
enabled
config be "dynamic":Using variables
Using jinja / macros with limited logic (i.e. not dependent on the execution of a SQL query)
As far as I know, there is currently no way to set the
enabled
config to be conditional based upon some result being returned by a query.Let's imagine we have a table that determines whether a particular model should be enabled (
1
) or not (0
):We add a macro that queries that table and return
True
orFalse
based on the above table:And then we use it in our model like we had previously when calling the
run_flag()
macro:You will find that your
my_table
model runs no matter what is in yourcheck_run
table. My best guess is that theenabled
config is set before any queries are executed on the database, this means thatrun_flag()
above will always returnTrue
.-! 🚨 WARNING 🚨 !- Do not use this pattern if you can avoid it. I make no guarantees that they will work well with your project or even with other core dbt functionality. Likely if you are going down this route, it is better to reconsider restructuring your entire project - for example, you could choose to add a test to model A which is selected from in model B and then use the `dbt build` command - this ensures that if a test fails in model A, model B is not run instead.
In order to achieve the above "enabled or not based on query results", one "hack" might be to essentially modify the SQL body of your model, if it was a table then we do self reference, and if it was an incremental, we apply a limit 0.
First, let's add a modified version of the macro shown above:
And in our
check_run
, let's first try enabling (1
) all of our models by letting them have the following values:Now we add 2 models to demonstrate the above working:
Now let's do a
dbt run
and inspect our debug logs:Click to expand...
And then another for good measure to check the behaviour of our incremental model:
Click to expand...
At this point, feel free to check the tables above in your data warehouse to see that they have the expected rows.
Now let's manually set1 those
run_flag
values in ourcheck_run
table to0
in order to disabled those model from being run2:And then a
dbt run
should log the following to stdout:Click to expand...
With the more detailed debug logs in our
target/logs
folder showing:Click to expand...
Basically we have successfully left the 2 models in their prior state with the above commands - pseudo dynamic enabled achieved.
Footnotes
While we are setting these manually, you can imagine them being set by some other process, perhaps a separate audit run that sets those to
0
or1
based on some other conditions for those models. ↩They are actually being "run" - we just have some logic that makes them appear to not update the actual destination table. ↩
Beta Was this translation helpful? Give feedback.
All reactions