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

Add generic rule: models should implement uniqueness test for their PK #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/dbt_score/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class Model(HasColumnsMixin):
config: dict[str, Any]
meta: dict[str, Any]
columns: list[Column]
constraints: list[dict[str, Any]]
package_name: str
database: str
schema: str
Expand Down Expand Up @@ -212,6 +213,7 @@ def from_node(
config=node_values["config"],
meta=node_values["meta"],
columns=cls._get_columns(node_values, test_values),
constraints=node_values["constraints"],
package_name=node_values["package_name"],
database=node_values["database"],
schema=node_values["schema"],
Expand Down
44 changes: 43 additions & 1 deletion src/dbt_score/rules/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,49 @@ def sql_has_reasonable_number_of_lines(
def has_example_sql(model: Model) -> RuleViolation | None:
"""The documentation of a model should have an example query."""
if model.language == "sql":
if "```sql" not in model.description:
if "```sql" not in (model.description or ""):
return RuleViolation(
"The model description does not include an example SQL query."
)

@rule
def has_uniqueness_test(model: Model) -> RuleViolation | None:
"""Model has uniqueness test for primary key."""
if model.config.get("materialized") not in {"table", "incremental"}:
return None
Comment on lines +58 to +59
Copy link
Contributor

@druzhinin-kirill druzhinin-kirill Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you like us to implement this with a rule filter? Maybe even the whole pk discovery can be a filter, and this filter can be also part of the public dbt-score API on par with default rules 🤔

So people who implement their own rules for pk'ed tables but use the same filter implementation💡


# Extract PK
pk_columns = None
Copy link
Contributor

@druzhinin-kirill druzhinin-kirill Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be an empty list, and we only verify pk_columns if the length is >0 (=1)?
I think this way we can ensure pk_columns is always a list

# At column level?
for column in model.columns:
for constraint in column.constraints:
if constraint.type == "primary_key":
pk_columns = [column.name]
break
else:
continue
break
# Or at table level?
if pk_columns is None:
for constraint in model.constraints:
if constraint["type"] == "primary_key":
pk_columns = constraint["columns"]
break

if pk_columns is None: # No PK, no need for uniqueness test
return None

# Look for matching uniqueness test
if len(pk_columns) == 1:
for column in model.columns:
if column.name == pk_columns[0]:
for data_test in column.tests:
if data_test.type == "unique":
return None

for data_test in model.tests:
if data_test.type == "unique_combination_of_columns":
if set(data_test.kwargs.get("combination_of_columns")) == set(pk_columns):
return None

return RuleViolation("There is no uniqueness test defined and matching the PK.")
3 changes: 3 additions & 0 deletions tests/resources/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"tags": []
}
},
"constraints": [],
"package_name": "package",
"database": "db",
"schema": "schema",
Expand Down Expand Up @@ -56,6 +57,7 @@
"tags": []
}
},
"constraints": [],
"package_name": "package",
"database": "db",
"schema": "schema",
Expand Down Expand Up @@ -86,6 +88,7 @@
"tags": []
}
},
"constraints": [],
"package_name": "package2",
"database": "db",
"schema": "schema",
Expand Down
Loading