databricks / dbt-databricks

A dbt adapter for Databricks.
https://databricks.com
Apache License 2.0
226 stars 119 forks source link

Auto-named constraints does not take into account for the model/table name #747

Closed elca-anh closed 3 months ago

elca-anh commented 3 months ago

Describe the feature

Column constraints can be named explicitly or automatically in the YAML model documentation. When done automatically, the name is created by hashing the name of the column. Since constraint name shall be unique per schema, this is far from ideal as multiple columns might have the same name. Explicit naming is not magic either since there can be no expansion of the name. In our case, we use prefixes to have several versions of the models in the dev environment. Since there are no macros within the YAML we cannot use the prefix in the constraint name.

Current code in dbt-databricks\dbt\include\databricks\macros\relations\constraints.sql:107: {% set name = constraint.get("name") %} {% if not name and local_md5 %} {{ exceptions.warn("Constraint of type " ~ type ~ " with no name provided. Generating hash instead.") }} {%- set name = local_md5 (column.get("name", "") ~ ";" ~ expression ~ ";") -%} {% endif %}

Describe alternatives you've considered

Add the model name as part of the hash

Additional context

Constraint documentation on models within YAML file.

Who will this benefit?

Model documentation

Are you interested in contributing this feature?

Yes, I can contribute on the validation of this feature and other tests

benc-db commented 3 months ago

This is a valid request that I do not have capacity to fix at the moment. I would be happy to provide feedback on a PR if you open one. Thanks 👍

elca-anh commented 3 months ago

Hello, I will do it as soon as I get some time to test it

elca-anh commented 3 months ago

To test the constraint naming in test_contraints_macros.py, I need the macro "local_md5" or to mock it (better). How can I do that?