From 565549a37555bcb0310b2139d6da87f1f8d16e4f Mon Sep 17 00:00:00 2001 From: Emily Rockman Date: Wed, 8 Mar 2023 11:27:56 -0600 Subject: [PATCH] support contracts on models materialized as view #584 (#360) * first pass with contract check * rename test class * clean up test * point to branch * fix whitespace * fix class name * remove dbt-core pin --- .changes/unreleased/Features-20230301-113553.yaml | 6 +++--- dbt/include/redshift/macros/adapters.sql | 5 ++++- tests/functional/adapter/test_constraints.py | 12 ++++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.changes/unreleased/Features-20230301-113553.yaml b/.changes/unreleased/Features-20230301-113553.yaml index 8ed2087ef..40cc40ed9 100644 --- a/.changes/unreleased/Features-20230301-113553.yaml +++ b/.changes/unreleased/Features-20230301-113553.yaml @@ -1,6 +1,6 @@ kind: Features -body: Implemented data_type_code_to_name for redshift +body: Enforce contracts on models materialized as tables and views time: 2023-03-01T11:35:53.98885-05:00 custom: - Author: peterallenwebb - Issue: "319" + Author: peterallenwebb emmyoop + Issue: 319 340 diff --git a/dbt/include/redshift/macros/adapters.sql b/dbt/include/redshift/macros/adapters.sql index 3d81e7e92..ede52b353 100644 --- a/dbt/include/redshift/macros/adapters.sql +++ b/dbt/include/redshift/macros/adapters.sql @@ -83,7 +83,10 @@ {{ sql_header if sql_header is not none }} - create view {{ relation }} as ( + create view {{ relation }} + {% if config.get('contract', False) -%} + {{ get_assert_columns_equivalent(sql) }} + {%- endif %} as ( {{ sql }} ) {{ bind_qualifier }}; {% endmacro %} diff --git a/tests/functional/adapter/test_constraints.py b/tests/functional/adapter/test_constraints.py index 0b8091a78..94283fc3e 100644 --- a/tests/functional/adapter/test_constraints.py +++ b/tests/functional/adapter/test_constraints.py @@ -1,7 +1,8 @@ import pytest from dbt.tests.util import relation_from_name from dbt.tests.adapter.constraints.test_constraints import ( - BaseConstraintsColumnsEqual, + BaseTableConstraintsColumnsEqual, + BaseViewConstraintsColumnsEqual, BaseConstraintsRuntimeEnforcement ) @@ -23,7 +24,7 @@ """ -class TestRedshiftConstraintsColumnsEqual(BaseConstraintsColumnsEqual): +class RedshiftColumnEqualSetup: @pytest.fixture def data_types(self, schema_int_type, int_type, string_type): # NOTE: Unlike some other adapters, we don't test array or JSON types here, because @@ -41,6 +42,13 @@ def data_types(self, schema_int_type, int_type, string_type): ] +class TestRedshiftTableConstraintsColumnsEqual(RedshiftColumnEqualSetup, BaseTableConstraintsColumnsEqual): + pass + + +class TestRedshiftViewConstraintsColumnsEqual(RedshiftColumnEqualSetup, BaseViewConstraintsColumnsEqual): + pass + class TestRedshiftConstraintsRuntimeEnforcement(BaseConstraintsRuntimeEnforcement): @pytest.fixture(scope="class") def expected_sql(self, project):