Skip to content

Commit

Permalink
Initial work on contract constraints
Browse files Browse the repository at this point in the history
Initial work on dbt constraints using contracts. Items to note:
* contracts do not appear to be enforced if model schema.yml is namespaced with dbt_business_intelligence
* main fix here is addition of fabric__table_columns_and_constraints macro, which is a redirect to get around this issue: dbt-msft/dbt-sqlserver#494
  • Loading branch information
ataft committed Jun 17, 2024
1 parent 26f2032 commit 471f58d
Show file tree
Hide file tree
Showing 7 changed files with 574 additions and 830 deletions.
9 changes: 2 additions & 7 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,13 @@ models:
+schema: observability
sources:
+enabled: false
+grants:
select: ['business_analyst_role']

seeds:
dbt_business_intelligence:
schema: staging # all seeds in this project will use the mapping schema by default

dispatch:
- macro_namespace: dbt_utils
search_order: ['tsql_utils', 'dbt_utils']
search_order: ['dbt_business_intelligence', 'tsql_utils', 'dbt_utils']
- macro_namespace: dbt_date
search_order: ['tsql_utils', 'dbt_date']

on-run-end:
- "{{ dbt_observability.upload_results(results) }}"
search_order: ['dbt_business_intelligence', 'tsql_utils', 'dbt_date']
4 changes: 4 additions & 0 deletions macros/fabric__table_columns_and_constraints.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% macro fabric__table_columns_and_constraints(relation) -%}
{%- do log("REDIRECT!!!: " ~ relation, info=true) -%}
{{ fabric__build_columns_constraints(relation) }}
{% endmacro %}
29 changes: 29 additions & 0 deletions macros/generate_surrogate_key.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{%- macro sqlserver__generate_surrogate_key(field_list) -%}

{%- if var('surrogate_key_treat_nulls_as_empty_strings', False) -%}
{%- set default_null_value = "" -%}
{%- else -%}
{%- set default_null_value = '_dbt_utils_surrogate_key_null_' -%}
{%- endif -%}

{%- set fields = [] -%}

{%- for field in field_list -%}

{%- do fields.append(
"coalesce(cast(" ~ field ~ " as " ~ dbt.type_string() ~ "), '" ~ default_null_value ~"')"
) -%}

{%- if not loop.last %}
{%- do fields.append("'-'") -%}
{%- endif -%}

{%- endfor -%}

{%- if fields|length > 1 %}
{{ dbt.hash(dbt.concat(fields)) }}
{%- else -%}
{{ dbt.hash(fields[0]) }}
{%- endif -%}

{%- endmacro -%}
283 changes: 0 additions & 283 deletions macros/sqlserver__create_constraints.sql

This file was deleted.

Loading

0 comments on commit 471f58d

Please sign in to comment.