Skip to content

Commit 9ab1d8c

Browse files
authored
Include built-in audits into the snapshot fingerprint calculation (#642)
1 parent db214aa commit 9ab1d8c

2 files changed

Lines changed: 32 additions & 17 deletions

File tree

sqlmesh/core/snapshot/definition.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sqlglot import exp
1111

1212
from sqlmesh.core import constants as c
13-
from sqlmesh.core.audit import Audit
13+
from sqlmesh.core.audit import BUILT_IN_AUDITS, Audit
1414
from sqlmesh.core.model import (
1515
Model,
1616
PythonModel,
@@ -786,21 +786,26 @@ def _model_metadata_hash(model: Model, audits: t.Dict[str, Audit]) -> str:
786786
]
787787

788788
for audit_name, audit_args in sorted(model.audits, key=lambda a: a[0]):
789-
if audit_name not in audits:
790-
continue
791-
792-
audit = audits[audit_name]
793-
metadata.extend(
794-
[
795-
audit.name,
796-
audit.render_query(model, **t.cast(t.Dict[str, t.Any], audit_args)).sql(
797-
identify=True, comments=True
798-
),
799-
audit.dialect,
800-
str(audit.skip),
801-
str(audit.blocking),
802-
]
803-
)
789+
metadata.append(audit_name)
790+
791+
if audit_name in BUILT_IN_AUDITS:
792+
for arg_name, arg_value in audit_args.items():
793+
metadata.append(arg_name)
794+
metadata.append(arg_value.sql(identify=True, comments=True))
795+
elif audit_name in audits:
796+
audit = audits[audit_name]
797+
metadata.extend(
798+
[
799+
audit.render_query(model, **t.cast(t.Dict[str, t.Any], audit_args)).sql(
800+
identify=True, comments=True
801+
),
802+
audit.dialect,
803+
str(audit.skip),
804+
str(audit.blocking),
805+
]
806+
)
807+
else:
808+
raise SQLMeshError(f"Unexpected audit name '{audit_name}'.")
804809

805810
# Add comments from the model query.
806811
for e, _, _ in model.render_query().walk():

tests/core/test_snapshot.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from _pytest.monkeypatch import MonkeyPatch
66
from pytest_mock.plugin import MockerFixture
7-
from sqlglot import exp, parse, parse_one
7+
from sqlglot import exp, parse, parse_one, to_column
88

99
from sqlmesh.core.config import AutoCategorizationMode, CategorizerConfig
1010
from sqlmesh.core.model import (
@@ -419,6 +419,16 @@ def test_fingerprint_jinja_macros(model: Model):
419419
assert updated_fingerprint.metadata_hash == original_fingerprint.metadata_hash
420420

421421

422+
def test_fingerprint_builtin_audits(model: Model, parent_model: Model):
423+
fingerprint = fingerprint_from_model(model, models={})
424+
425+
model = SqlModel.parse_obj(
426+
{**model.dict(), "audits": [("unique_values", {"columns": exp.convert([to_column("a")])})]}
427+
)
428+
new_fingerprint = fingerprint_from_model(model, models={})
429+
assert new_fingerprint != fingerprint
430+
431+
422432
def test_stamp(model: Model):
423433
original_fingerprint = fingerprint_from_model(model, models={})
424434

0 commit comments

Comments
 (0)