Skip to content

Commit dc7b0b0

Browse files
authored
Fix: Show diff for metadata changes in the plan output (#3574)
1 parent c1fc1df commit dc7b0b0

2 files changed

Lines changed: 59 additions & 37 deletions

File tree

sqlmesh/core/console.py

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def _show_summary_tree_for(
768768
metadata.add(
769769
f"[metadata]{display_name}"
770770
if no_diff
771-
else Syntax(f"{display_name}", "sql", word_wrap=True)
771+
else Syntax(f"{display_name}\n{context_diff.text_diff(name)}", "sql")
772772
)
773773
if direct.children:
774774
tree.add(direct)
@@ -855,25 +855,31 @@ def _show_categorized_snapshots(self, plan: Plan, default_catalog: t.Optional[st
855855
context_diff = plan.context_diff
856856

857857
for snapshot in plan.categorized:
858-
if not context_diff.directly_modified(snapshot.name):
859-
continue
860-
861-
category_str = SNAPSHOT_CHANGE_CATEGORY_STR[snapshot.change_category]
862-
tree = Tree(
863-
f"[bold][direct]Directly Modified: {snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({category_str})"
864-
)
865-
indirect_tree = None
866-
for child_sid in sorted(plan.indirectly_modified.get(snapshot.snapshot_id, set())):
867-
child_snapshot = context_diff.snapshots[child_sid]
868-
if not indirect_tree:
869-
indirect_tree = Tree("[indirect]Indirectly Modified Children:")
870-
tree.add(indirect_tree)
871-
child_category_str = SNAPSHOT_CHANGE_CATEGORY_STR[child_snapshot.change_category]
872-
indirect_tree.add(
873-
f"[indirect]{child_snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({child_category_str})"
858+
if context_diff.directly_modified(snapshot.name):
859+
category_str = SNAPSHOT_CHANGE_CATEGORY_STR[snapshot.change_category]
860+
tree = Tree(
861+
f"[bold][direct]Directly Modified: {snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({category_str})"
874862
)
875-
if indirect_tree:
876-
indirect_tree = self._limit_model_names(indirect_tree, self.verbose)
863+
indirect_tree = None
864+
for child_sid in sorted(plan.indirectly_modified.get(snapshot.snapshot_id, set())):
865+
child_snapshot = context_diff.snapshots[child_sid]
866+
if not indirect_tree:
867+
indirect_tree = Tree("[indirect]Indirectly Modified Children:")
868+
tree.add(indirect_tree)
869+
child_category_str = SNAPSHOT_CHANGE_CATEGORY_STR[
870+
child_snapshot.change_category
871+
]
872+
indirect_tree.add(
873+
f"[indirect]{child_snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({child_category_str})"
874+
)
875+
if indirect_tree:
876+
indirect_tree = self._limit_model_names(indirect_tree, self.verbose)
877+
elif context_diff.metadata_updated(snapshot.name):
878+
tree = Tree(
879+
f"[bold][metadata]Metadata Updated: {snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)}"
880+
)
881+
else:
882+
continue
877883

878884
self._print(Syntax(context_diff.text_diff(snapshot.name), "sql", word_wrap=True))
879885
self._print(tree)
@@ -1740,25 +1746,32 @@ def _show_missing_dates(self, plan: Plan, default_catalog: t.Optional[str]) -> N
17401746
def _show_categorized_snapshots(self, plan: Plan, default_catalog: t.Optional[str]) -> None:
17411747
context_diff = plan.context_diff
17421748
for snapshot in plan.categorized:
1743-
if not context_diff.directly_modified(snapshot.name):
1749+
if context_diff.directly_modified(snapshot.name):
1750+
category_str = SNAPSHOT_CHANGE_CATEGORY_STR[snapshot.change_category]
1751+
tree = Tree(
1752+
f"[bold][direct]Directly Modified: {snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({category_str})"
1753+
)
1754+
indirect_tree = None
1755+
for child_sid in sorted(plan.indirectly_modified.get(snapshot.snapshot_id, set())):
1756+
child_snapshot = context_diff.snapshots[child_sid]
1757+
if not indirect_tree:
1758+
indirect_tree = Tree("[indirect]Indirectly Modified Children:")
1759+
tree.add(indirect_tree)
1760+
child_category_str = SNAPSHOT_CHANGE_CATEGORY_STR[
1761+
child_snapshot.change_category
1762+
]
1763+
indirect_tree.add(
1764+
f"[indirect]{child_snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({child_category_str})"
1765+
)
1766+
if indirect_tree:
1767+
indirect_tree = self._limit_model_names(indirect_tree, self.verbose)
1768+
elif context_diff.metadata_updated(snapshot.name):
1769+
tree = Tree(
1770+
f"[bold][metadata]Metadata Updated: {snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)}"
1771+
)
1772+
else:
17441773
continue
17451774

1746-
category_str = SNAPSHOT_CHANGE_CATEGORY_STR[snapshot.change_category]
1747-
tree = Tree(
1748-
f"[bold][direct]Directly Modified: {snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({category_str})"
1749-
)
1750-
indirect_tree = None
1751-
for child_sid in sorted(plan.indirectly_modified.get(snapshot.snapshot_id, set())):
1752-
child_snapshot = context_diff.snapshots[child_sid]
1753-
if not indirect_tree:
1754-
indirect_tree = Tree("[indirect]Indirectly Modified Children:")
1755-
tree.add(indirect_tree)
1756-
child_category_str = SNAPSHOT_CHANGE_CATEGORY_STR[child_snapshot.change_category]
1757-
indirect_tree.add(
1758-
f"[indirect]{child_snapshot.display_name(plan.environment_naming_info, default_catalog, dialect=self.dialect)} ({child_category_str})"
1759-
)
1760-
if indirect_tree:
1761-
indirect_tree = self._limit_model_names(indirect_tree, self.verbose)
17621775
self._print(f"```diff\n{context_diff.text_diff(snapshot.name)}\n```\n")
17631776
self._print("```\n")
17641777
self._print(tree)

sqlmesh/core/plan/definition.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def categorized(self) -> t.List[Snapshot]:
108108
"""Returns the already categorized snapshots."""
109109
return [
110110
self.context_diff.snapshots[s_id]
111-
for s_id in sorted(self.directly_modified)
111+
for s_id in sorted({*self.directly_modified, *self.metadata_updated})
112112
if self.context_diff.snapshots[s_id].version
113113
]
114114

@@ -136,6 +136,15 @@ def modified_snapshots(self) -> t.Dict[SnapshotId, t.Union[Snapshot, SnapshotTab
136136
for s_id in sorted(downstream_s_ids)
137137
},
138138
**self.context_diff.removed_snapshots,
139+
**{s_id: self.context_diff.snapshots[s_id] for s_id in sorted(self.metadata_updated)},
140+
}
141+
142+
@cached_property
143+
def metadata_updated(self) -> t.Set[SnapshotId]:
144+
return {
145+
snapshot.snapshot_id
146+
for snapshot, _ in self.context_diff.modified_snapshots.values()
147+
if self.context_diff.metadata_updated(snapshot.name)
139148
}
140149

141150
@property

0 commit comments

Comments
 (0)