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

CT-2322/improve contracts error message #7223

Closed
wants to merge 8 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230325-192830.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Added prettier printing to ContractError class
time: 2023-03-25T19:28:30.171461-07:00
custom:
Author: kentkr
Issue: "7209"
42 changes: 38 additions & 4 deletions core/dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2132,14 +2132,48 @@ def __init__(self, yaml_columns, sql_columns):

def get_message(self) -> str:
msg = (
"Contracts are enabled for this model. "
"This model has an enforced contract that failed.\n"
"Please ensure the name, data_type, and number of columns in your `yml` file "
"match the columns in your SQL file.\n"
f"Schema File Columns: {self.yaml_columns}\n"
f"SQL File Columns: {self.sql_columns}"
# separate column headers by 20 spaces
f"{'sql col:' : <20} {'yaml col:' : <20} {'issue:' : <20}\n"
)
return msg

# list of ordered matches (or not matches) for printing
ordered_match = []
# track sql cols so we don't need another for loop later
sql_col_set = set()
# for each sql col list
for sql_col in self.sql_columns:
# add sql col to set
sql_col_set.add(sql_col['name'])
# for each yaml col list
for i, yaml_col in enumerate(self.yaml_columns):
# if name matches
if sql_col['name'] == yaml_col['name']:
# if type matches
if sql_col['formatted'] == yaml_col['formatted']:
# its a perfect match!
ordered_match += [[sql_col['formatted'], yaml_col['formatted'], '']]
break
else:
# same name, diff type
ordered_match += [[sql_col['formatted'], yaml_col['formatted'], 'Incorrect type']]
break
# if last loop, then no name match
if i == len(self.yaml_columns)-1:
ordered_match += [[sql_col['formatted'], '', 'No match']]

# now add all yaml cols without a match
for yaml_col in self.yaml_columns:
if yaml_col['name'] not in sql_col_set:
ordered_match += [['', yaml_col['name'], 'No match']]

# add each match to the message, printed in columns
for match in ordered_match:
msg += f'{match[0] : <20} {match[1] : <20} {match[2] : <20}\n'

return msg

# not modifying these since rpc should be deprecated soon
class UnknownAsyncIDException(Exception):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{%- set string_yaml_columns = stringify_formatted_columns(yaml_columns) -%}

{%- if sql_columns|length != yaml_columns|length -%}
{%- do exceptions.raise_contract_error(string_yaml_columns, string_sql_columns) -%}
{%- do exceptions.raise_contract_error(yaml_columns, sql_columns) -%}
{%- endif -%}

{%- for sql_col in sql_columns -%}
Expand All @@ -56,11 +56,11 @@
{%- endfor -%}
{%- if not yaml_col -%}
{#-- Column with name not found in yaml #}
{%- do exceptions.raise_contract_error(string_yaml_columns, string_sql_columns) -%}
{%- do exceptions.raise_contract_error(yaml_columns, sql_columns) -%}
{%- endif -%}
{%- if sql_col['formatted'] != yaml_col[0]['formatted'] -%}
{#-- Column data types don't match #}
{%- do exceptions.raise_contract_error(string_yaml_columns, string_sql_columns) -%}
{%- do exceptions.raise_contract_error(yaml_columns, sql_columns) -%}
{%- endif -%}
{%- endfor -%}

Expand Down