Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbt Constraints / model contracts #341

Merged
merged 17 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changes/unreleased/Features-20221219-164808.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: Features
body: 'Support for data types constraints in Snowflake following the dbt Core feature
#6271'
time: 2022-12-19T16:48:08.771806+01:00
custom:
Author: b-per
Issue: "365"
PR: "341"
7 changes: 6 additions & 1 deletion dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
temporary
{%- elif transient -%}
transient
{%- endif %} table {{ relation }} {% if copy_grants and not temporary -%} copy grants {%- endif %} as
{%- endif %} table {{ relation }}
{% if config.get('constraints_enabled', False) %}
{{ get_assert_columns_equivalent(sql) }}
{{ get_columns_spec_ddl() }}
{% endif %}
{% if copy_grants and not temporary -%} copy grants {%- endif %} as
(
{%- if cluster_by_string is not none -%}
select * from(
Expand Down
16 changes: 16 additions & 0 deletions dbt/include/snowflake/macros/utils/get_columns_spec_ddl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% macro snowflake__get_columns_spec_ddl() %}
{# loop through user_provided_columns to create DDL with data types and constraints #}
{%- set ns = namespace(at_least_one_check=False) -%}
{%- set user_provided_columns = model['columns'] -%}
(
{% for i in user_provided_columns -%}
{%- set col = user_provided_columns[i] -%}
{% set constraints = col['constraints'] -%}
{%- set ns.at_least_one_check = ns.at_least_one_check or col['constraints_check'] %}
{{ col['name'] }} {{ col['data_type'] }} {% for x in constraints %} {{ x or "" }} {% endfor %} {{ "," if not loop.last }}
{%- endfor %}
)
{%- if ns.at_least_one_check -%}
{{exceptions.warn("We noticed you have `constraints_check` configs, these are NOT compatible with Snowflake and will be ignored")}}
{%- endif %}
{% endmacro %}
33 changes: 33 additions & 0 deletions tests/functional/adapter/test_constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from dbt.tests.util import relation_from_name
from dbt.tests.adapter.constraints.test_constraints import (
BaseConstraintsColumnsEqual,
BaseConstraintsRuntimeEnforcement
)

_expected_sql_snowflake = """
create or replace transient table {0} (
id integer not null primary key ,
color text ,
date_day date
) as (
select
1 as id,
'blue' as color,
cast('2019-01-01' as date) as date_day
);
"""

class TestSnowflakeConstraintsColumnsEqual(BaseConstraintsColumnsEqual):
pass


class TestSnowflakeConstraintsRuntimeEnforcement(BaseConstraintsRuntimeEnforcement):
@pytest.fixture(scope="class")
def expected_sql(self, project):
relation = relation_from_name(project.adapter, "my_model")
return _expected_sql_snowflake.format(relation)

@pytest.fixture(scope="class")
def expected_error_messages(self):
return ["NULL result in a non-nullable column"]