Skip to content

Commit 12c1cbb

Browse files
Contract enforcement on temporary tables (#8889) (#8902)
* add test * fix test * first pass with constraint error * add back column checks for temp tables * changelog * Update .changes/unreleased/Fixes-20231024-145504.yaml (cherry picked from commit 98310b6) Co-authored-by: Emily Rockman <[email protected]>
1 parent ced70d5 commit 12c1cbb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Add back contract enforcement for temporary tables on postgres
3+
time: 2023-10-24T14:55:04.051683-05:00
4+
custom:
5+
Author: emmyoop
6+
Issue: "8857"

plugins/postgres/dbt/include/postgres/macros/adapters.sql

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
unlogged
1111
{%- endif %} table {{ relation }}
1212
{% set contract_config = config.get('contract') %}
13-
{% if contract_config.enforced and (not temporary) %}
13+
{% if contract_config.enforced %}
1414
{{ get_assert_columns_equivalent(sql) }}
15-
{{ get_table_columns_and_constraints() }} ;
15+
{% endif -%}
16+
{% if contract_config.enforced and (not temporary) -%}
17+
{{ get_table_columns_and_constraints() }} ;
1618
insert into {{ relation }} (
1719
{{ adapter.dispatch('get_column_names', 'dbt')() }}
1820
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
from dbt.tests.util import run_dbt, write_file
3+
4+
5+
my_model_sql = """
6+
select 'some string' as string_column
7+
"""
8+
9+
my_model_int_sql = """
10+
select 123 as int_column
11+
"""
12+
13+
model_schema_yml = """
14+
models:
15+
- name: my_model
16+
config:
17+
materialized: incremental
18+
on_schema_change: append_new_columns
19+
contract: {enforced: true}
20+
columns:
21+
- name: string_column
22+
data_type: text
23+
"""
24+
25+
26+
class TestIncrementalModelContractEnforcement:
27+
@pytest.fixture(scope="class")
28+
def models(self):
29+
return {
30+
"my_model.sql": my_model_sql,
31+
"schema.yml": model_schema_yml,
32+
}
33+
34+
def test_contracted_incremental(self, project):
35+
results = run_dbt()
36+
assert len(results) == 1
37+
# now update the column type in the model to break the contract
38+
write_file(my_model_int_sql, project.project_root, "models", "my_model.sql")
39+
40+
expected_msg = "This model has an enforced contract that failed."
41+
results = run_dbt(expect_pass=False)
42+
assert len(results) == 1
43+
msg = results[0].message
44+
assert expected_msg in msg

0 commit comments

Comments
 (0)