Snowflake-Labs / dbt_constraints

This package generates database constraints based on the tests in a dbt project
Apache License 2.0
139 stars 27 forks source link

Create constraint only on table recreation #45

Closed kamilamarcinekpgs closed 11 months ago

kamilamarcinekpgs commented 1 year ago

It would be great, if creations of constraints could be disabled for incremental materialization, and enabled for materialize=table and for full refresh.

sfc-gh-dflippo commented 1 year ago

dbt Constraints does not recreate a constraint if it is already in place. The expectation is that an incremental materialization would only add the constraint when a --full-refresh operation is performed and subsequent executions would just verify that the constraints are still there.

not_null constraints aren't really being added on subsequent executions on Snowflake. From my testing I determined that it was faster to alter the table than have logic to identify which columns are already not-null. Snowflake doesn't take any time to validate not_null constraints because it already knows from its statistics in cloud services whether any micro-partitions contain a null in a column.

If you are asking for the tests in this package to be executed selectively, that is a function controlled by dbt Core, not dbt Constraints. The tests in this package operate like any other dbt test and execute when dbt chooses to execute them.