Skip to content

Commit a19f7cb

Browse files
committed
fix!: include referenced Jinja macros in standalone audit metadata hash
StandaloneAudit.metadata_hash ignored the audit's trimmed Jinja macro registry, so editing only the body of a Jinja macro used by a standalone audit changed the rendered audit query but not the audit's fingerprint. `sqlmesh plan` did not pick up the change. The registry's hash values are now included when it holds macros (root macros or package macros). Audits without Jinja macros keep their exact hash, including registries that only carry refs/sources/vars globals. Standalone audits that use Jinja macros, including dbt data tests, get a new metadata hash once on upgrade. This is a metadata-only change: no backfill. Signed-off-by: mday-io <mdaytn@gmail.com>
1 parent 5a09168 commit a19f7cb

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎sqlmesh/core/audit/definition.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ def metadata_hash(self) -> str:
291291

292292
data.append(self.query_.sql)
293293
data.extend([e.sql for e in self.expressions_ or []])
294+
295+
# Only include the registry when it holds macros, so that audits without Jinja
296+
# macros keep their existing hash. The registry is trimmed to the referenced macros.
297+
# The refs / sources / vars from `global_objs` are therefore hashed only when macros
298+
# are present, which matches how models hash their registry.
299+
if self.jinja_macros.root_macros or self.jinja_macros.packages:
300+
data.extend(self.jinja_macros.data_hash_values)
301+
294302
self._metadata_hash = hash_data(data)
295303
return self._metadata_hash
296304

‎tests/core/test_audit.py‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import typing as t
23
import pytest
34
from sqlglot import exp, parse_one
45

@@ -1233,3 +1234,117 @@ def test_audit_formatting_flag_serde():
12331234

12341235
deserialized_audit = ModelAudit.parse_raw(audit_json)
12351236
assert deserialized_audit.dict() == audit.dict()
1237+
1238+
1239+
def _load_standalone_audit_with_macros(
1240+
macros: str,
1241+
query_body: str,
1242+
package_macros: t.Optional[t.Dict[str, str]] = None,
1243+
global_objs: t.Optional[t.Dict[str, t.Any]] = None,
1244+
) -> StandaloneAudit:
1245+
expressions = parse(
1246+
f"""
1247+
AUDIT (
1248+
name my_audit,
1249+
standalone true,
1250+
);
1251+
1252+
JINJA_QUERY_BEGIN;
1253+
{query_body}
1254+
JINJA_END;
1255+
"""
1256+
)
1257+
jinja_macros = JinjaMacroRegistry(global_objs=global_objs or {})
1258+
jinja_macros.add_macros(MacroExtractor().extract(macros))
1259+
for package, package_source in (package_macros or {}).items():
1260+
jinja_macros.add_macros(MacroExtractor().extract(package_source), package=package)
1261+
audit = load_audit(expressions, jinja_macros=jinja_macros)
1262+
assert isinstance(audit, StandaloneAudit)
1263+
return audit
1264+
1265+
1266+
def test_standalone_audit_metadata_hash_includes_referenced_jinja_macros():
1267+
query = "SELECT * FROM db.tbl WHERE id <> {{ jone() }}"
1268+
unused = "{% macro unused() %}x{% endmacro %}"
1269+
1270+
audit_1 = _load_standalone_audit_with_macros(
1271+
"{% macro jone() %}1{% endmacro %}" + unused, query
1272+
)
1273+
audit_1_again = _load_standalone_audit_with_macros(
1274+
"{% macro jone() %}1{% endmacro %}" + unused, query
1275+
)
1276+
audit_2 = _load_standalone_audit_with_macros(
1277+
"{% macro jone() %}2{% endmacro %}" + unused, query
1278+
)
1279+
1280+
assert audit_1.metadata_hash == audit_1_again.metadata_hash
1281+
assert audit_1.metadata_hash != audit_2.metadata_hash
1282+
1283+
1284+
def test_standalone_audit_metadata_hash_ignores_unreferenced_jinja_macros():
1285+
query = "SELECT * FROM db.tbl WHERE id <> {{ jone() }}"
1286+
jone = "{% macro jone() %}1{% endmacro %}"
1287+
1288+
audit_1 = _load_standalone_audit_with_macros(
1289+
jone + "{% macro unused() %}x{% endmacro %}", query
1290+
)
1291+
audit_2 = _load_standalone_audit_with_macros(
1292+
jone + "{% macro unused() %}y{% endmacro %}", query
1293+
)
1294+
1295+
assert "unused" not in audit_1.jinja_macros.root_macros
1296+
assert audit_1.metadata_hash == audit_2.metadata_hash
1297+
1298+
1299+
def test_standalone_audit_metadata_hash_unchanged_without_jinja_macros():
1300+
audit = load_audit(
1301+
parse(
1302+
"""
1303+
AUDIT (
1304+
name my_audit,
1305+
owner owner_name,
1306+
standalone true,
1307+
);
1308+
1309+
SELECT * FROM db.tbl WHERE id IS NULL
1310+
"""
1311+
)
1312+
)
1313+
assert isinstance(audit, StandaloneAudit)
1314+
assert not audit.jinja_macros.root_macros and not audit.jinja_macros.packages
1315+
1316+
# This literal must match the value computed on main before Jinja macros were hashed, so that
1317+
# audits without Jinja macros are not rehashed (and thus not re-versioned) on upgrade.
1318+
assert audit.metadata_hash == "3234547564"
1319+
1320+
1321+
def test_standalone_audit_metadata_hash_ignores_global_objs_without_macros():
1322+
query = "SELECT * FROM db.tbl WHERE id IS NULL"
1323+
no_macros = ""
1324+
1325+
empty = _load_standalone_audit_with_macros(no_macros, query)
1326+
with_globals = _load_standalone_audit_with_macros(
1327+
no_macros, query, global_objs={"refs": {"a": {"name": "a"}}, "vars": {"v": 1}}
1328+
)
1329+
1330+
assert with_globals.jinja_macros.global_objs
1331+
assert not with_globals.jinja_macros.root_macros and not with_globals.jinja_macros.packages
1332+
assert with_globals.metadata_hash == empty.metadata_hash
1333+
# Must match the value computed on main, see the test above.
1334+
assert empty.metadata_hash == "435209792"
1335+
1336+
1337+
def test_standalone_audit_metadata_hash_includes_referenced_package_macros():
1338+
query = "SELECT * FROM db.tbl WHERE id <> {{ pkg.jone() }}"
1339+
1340+
def load(body: str) -> StandaloneAudit:
1341+
return _load_standalone_audit_with_macros(
1342+
"", query, package_macros={"pkg": "{% macro jone() %}" + body + "{% endmacro %}"}
1343+
)
1344+
1345+
audit_1, audit_1_again, audit_2 = load("1"), load("1"), load("2")
1346+
1347+
assert "pkg" in audit_1.jinja_macros.packages
1348+
assert not audit_1.jinja_macros.root_macros
1349+
assert audit_1.metadata_hash == audit_1_again.metadata_hash
1350+
assert audit_1.metadata_hash != audit_2.metadata_hash

0 commit comments

Comments
 (0)