Skip to content

Commit

Permalink
Rename "constraints_enabled" to "contract" (#7002)
Browse files Browse the repository at this point in the history
* Rename "constraints_enabled" to "contract"

* Changie
  • Loading branch information
gshank authored Feb 23, 2023
1 parent 70c26f5 commit 58d1bcc
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 114 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20230217-105223.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Rename "constraint_enabled" to "contract"
time: 2023-02-17T10:52:23.212474-05:00
custom:
Author: gshank
Issue: "6748"
2 changes: 1 addition & 1 deletion core/dbt/contracts/graph/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class NodeConfig(NodeAndTestConfig):
default_factory=Docs,
metadata=MergeBehavior.Update.meta(),
)
constraints_enabled: Optional[bool] = False
contract: bool = False

# we validate that node_color has a suitable value to prevent dbt-docs from crashing
def __post_init__(self):
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/contracts/graph/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class CompiledNode(ParsedNode):
extra_ctes_injected: bool = False
extra_ctes: List[InjectedCTE] = field(default_factory=list)
_pre_injected_sql: Optional[str] = None
constraints_enabled: bool = False
contract: bool = False

@property
def empty(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

create {% if temporary: -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
{% if config.get('constraints_enabled', False) %}
{% if config.get('contract', False) %}
{{ get_assert_columns_equivalent(sql) }}
{{ get_columns_spec_ddl() }}
{% endif %}
Expand Down
8 changes: 4 additions & 4 deletions core/dbt/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,15 @@ def update_parsed_node_config(
else:
parsed_node.docs = Docs(show=docs_show)

# If we have constraints_enabled in the config, copy to node level, for backwards
# If we have "contract" in the config, copy to node level, for backwards
# compatibility with earlier node-only config.
if config_dict.get("constraints_enabled", False):
parsed_node.constraints_enabled = True
if config_dict.get("contract", False):
parsed_node.contract = True

parser_name = type(self).__name__
if parser_name == "ModelParser":
original_file_path = parsed_node.original_file_path
error_message = "\n `constraints_enabled=true` can only be configured within `schema.yml` files\n NOT within a model file(ex: .sql, .py) or `dbt_project.yml`."
error_message = "\n `contract=true` can only be configured within `schema.yml` files\n NOT within a model file(ex: .sql, .py) or `dbt_project.yml`."
raise ParsingError(
f"Original File Path: ({original_file_path})\nConstraints must be defined in a `yml` schema configuration file like `schema.yml`.\nOnly the SQL table materialization is supported for constraints. \n`data_type` values must be defined for all columns and NOT be null or blank.{error_message}"
)
Expand Down
5 changes: 1 addition & 4 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,10 +930,7 @@ def parse_patch(self, block: TargetBlock[NodeTarget], refs: ParserRef) -> None:

def validate_constraints(self, patched_node):
error_messages = []
if (
patched_node.resource_type == "model"
and patched_node.config.constraints_enabled is True
):
if patched_node.resource_type == "model" and patched_node.config.contract is True:
validators = [
self.constraints_schema_validator(patched_node),
self.constraints_materialization_validator(patched_node),
Expand Down
2 changes: 1 addition & 1 deletion plugins/postgres/dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{%- elif unlogged -%}
unlogged
{%- endif %} table {{ relation }}
{% if config.get('constraints_enabled', False) %}
{% if config.get('contract', False) %}
{{ get_assert_columns_equivalent(sql) }}
{{ get_columns_spec_ddl() }} ;
insert into {{ relation }} {{ get_column_names() }}
Expand Down
56 changes: 28 additions & 28 deletions schemas/dbt/manifest/v8.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions test/unit/test_contracts_graph_compiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def basic_compiled_model():
alias='bar',
tags=[],
config=NodeConfig(),
constraints_enabled=False,
contract=False,
meta={},
compiled=True,
extra_ctes=[InjectedCTE('whatever', 'select * from other')],
Expand Down Expand Up @@ -193,12 +193,12 @@ def basic_compiled_dict():
'meta': {},
'grants': {},
'packages': [],
'constraints_enabled': False,
'contract': False,
'docs': {'show': True},
},
'docs': {'show': True},
'columns': {},
'constraints_enabled': False,
'contract': False,
'meta': {},
'compiled': True,
'extra_ctes': [{'id': 'whatever', 'sql': 'select * from other'}],
Expand Down Expand Up @@ -403,7 +403,7 @@ def basic_compiled_schema_test_node():
alias='bar',
tags=[],
config=TestConfig(severity='warn'),
constraints_enabled=False,
contract=False,
meta={},
compiled=True,
extra_ctes=[InjectedCTE('whatever', 'select * from other')],
Expand Down Expand Up @@ -504,7 +504,7 @@ def basic_compiled_schema_test_dict():
},
'docs': {'show': True},
'columns': {},
'constraints_enabled': False,
'contract': False,
'meta': {},
'compiled': True,
'extra_ctes': [{'id': 'whatever', 'sql': 'select * from other'}],
Expand Down
44 changes: 22 additions & 22 deletions test/unit/test_contracts_graph_parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def populated_node_config_dict():
'grants': {},
'packages': [],
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
}


Expand Down Expand Up @@ -159,12 +159,12 @@ def base_parsed_model_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'deferred': False,
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'columns': {},
'meta': {},
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
Expand Down Expand Up @@ -259,11 +259,11 @@ def complex_parsed_model_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'columns': {
'a': {
'name': 'a',
Expand Down Expand Up @@ -320,7 +320,7 @@ def complex_parsed_model_object():
)


{'enabled': True, 'tags': [], 'meta': {}, 'materialized': 'ephemeral', 'persist_docs': {}, 'quoting': {}, 'column_types': {'a': 'text'}, 'on_schema_change': 'ignore', 'grants': {}, 'packages': [], 'docs': {'show': True}, 'constraints_enabled': False, 'post-hook': [{'sql': 'insert into blah(a, b) select "1", 1', 'transaction': True}], 'pre-hook': []}
{'enabled': True, 'tags': [], 'meta': {}, 'materialized': 'ephemeral', 'persist_docs': {}, 'quoting': {}, 'column_types': {'a': 'text'}, 'on_schema_change': 'ignore', 'grants': {}, 'packages': [], 'docs': {'show': True}, 'contract': False, 'post-hook': [{'sql': 'insert into blah(a, b) select "1", 1', 'transaction': True}], 'pre-hook': []}

{'column_types': {'a': 'text'}, 'enabled': True, 'materialized': 'ephemeral', 'persist_docs': {}, 'post-hook': [{'sql': 'insert into blah(a, b) select "1", 1', 'transaction': True}], 'pre-hook': [], 'quoting': {}, 'tags': [], 'on_schema_change': 'ignore', 'meta': {}, 'grants': {}, 'docs': {'show': True}, 'packages': []}

Expand Down Expand Up @@ -459,7 +459,7 @@ def basic_parsed_seed_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'deferred': False,
Expand Down Expand Up @@ -550,7 +550,7 @@ def complex_parsed_seed_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'deferred': False,
Expand Down Expand Up @@ -807,11 +807,11 @@ def base_parsed_hook_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'columns': {},
'meta': {},
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
Expand Down Expand Up @@ -886,11 +886,11 @@ def complex_parsed_hook_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'columns': {
'a': {
'name': 'a',
Expand Down Expand Up @@ -1038,7 +1038,7 @@ def basic_parsed_schema_test_dict():
'schema': 'dbt_test__audit',
},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'columns': {},
'test_metadata': {
'name': 'foo',
Expand Down Expand Up @@ -1115,7 +1115,7 @@ def complex_parsed_schema_test_dict():
'schema': 'dbt_test__audit',
},
'docs': {'show': False},
'constraints_enabled': False,
'contract': False,
'columns': {
'a': {
'name': 'a',
Expand Down Expand Up @@ -1236,7 +1236,7 @@ def basic_timestamp_snapshot_config_dict():
'grants': {},
'packages': [],
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
}


Expand Down Expand Up @@ -1273,7 +1273,7 @@ def complex_timestamp_snapshot_config_dict():
'grants': {},
'packages': [],
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
}


Expand Down Expand Up @@ -1334,7 +1334,7 @@ def basic_check_snapshot_config_dict():
'grants': {},
'packages': [],
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
}


Expand Down Expand Up @@ -1371,7 +1371,7 @@ def complex_set_snapshot_config_dict():
'grants': {},
'packages': [],
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
}


Expand Down Expand Up @@ -1481,11 +1481,11 @@ def basic_timestamp_snapshot_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'columns': {},
'meta': {},
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
Expand Down Expand Up @@ -1623,11 +1623,11 @@ def basic_check_snapshot_dict():
'meta': {},
'grants': {},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'packages': [],
},
'docs': {'show': True},
'constraints_enabled': False,
'contract': False,
'columns': {},
'meta': {},
'checksum': {'name': 'sha256', 'checksum': 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'},
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
'depends_on', 'database', 'schema', 'name', 'resource_type',
'package_name', 'path', 'original_file_path', 'raw_code', 'language',
'description', 'columns', 'fqn', 'build_path', 'compiled_path', 'patch_path', 'docs',
'deferred', 'checksum', 'unrendered_config', 'created_at', 'config_call_dict', 'relation_name', 'constraints_enabled'
'deferred', 'checksum', 'unrendered_config', 'created_at', 'config_call_dict', 'relation_name', 'contract'
})

REQUIRED_COMPILED_NODE_KEYS = frozenset(REQUIRED_PARSED_NODE_KEYS | {
Expand Down
8 changes: 4 additions & 4 deletions tests/adapter/dbt/tests/adapter/constraints/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
models:
- name: my_model
config:
constraints_enabled: true
contract: true
columns:
- name: id
quote: true
Expand All @@ -73,7 +73,7 @@
data_type: date
- name: my_model_error
config:
constraints_enabled: true
contract: true
columns:
- name: id
data_type: integer
Expand All @@ -88,7 +88,7 @@
data_type: date
- name: my_model_wrong_order
config:
constraints_enabled: true
contract: true
columns:
- name: id
data_type: integer
Expand All @@ -103,7 +103,7 @@
data_type: date
- name: my_model_wrong_name
config:
constraints_enabled: true
contract: true
columns:
- name: id
data_type: integer
Expand Down
12 changes: 6 additions & 6 deletions tests/adapter/dbt/tests/adapter/constraints/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ def test__constraints_wrong_column_order(self, project):
manifest = get_manifest(project.project_root)
model_id = "model.test.my_model_wrong_order"
my_model_config = manifest.nodes[model_id].config
constraints_enabled_actual_config = my_model_config.constraints_enabled
contract_actual_config = my_model_config.contract

assert constraints_enabled_actual_config is True
assert contract_actual_config is True

expected_compile_error = "Please ensure the name, order, and number of columns in your `yml` file match the columns in your SQL file."
expected_schema_file_columns = "Schema File Columns: ['ID', 'COLOR', 'DATE_DAY']"
Expand All @@ -58,9 +58,9 @@ def test__constraints_wrong_column_names(self, project):
manifest = get_manifest(project.project_root)
model_id = "model.test.my_model_wrong_name"
my_model_config = manifest.nodes[model_id].config
constraints_enabled_actual_config = my_model_config.constraints_enabled
contract_actual_config = my_model_config.contract

assert constraints_enabled_actual_config is True
assert contract_actual_config is True

expected_compile_error = "Please ensure the name, order, and number of columns in your `yml` file match the columns in your SQL file."
expected_schema_file_columns = "Schema File Columns: ['ID', 'COLOR', 'DATE_DAY']"
Expand Down Expand Up @@ -165,8 +165,8 @@ def test__constraints_enforcement_rollback(
manifest = get_manifest(project.project_root)
model_id = "model.test.my_model"
my_model_config = manifest.nodes[model_id].config
constraints_enabled_actual_config = my_model_config.constraints_enabled
assert constraints_enabled_actual_config is True
contract_actual_config = my_model_config.contract
assert contract_actual_config is True

# Its result includes the expected error messages
self.assert_expected_error_messages(failing_results[0].message, expected_error_messages)
Expand Down
Loading

0 comments on commit 58d1bcc

Please sign in to comment.