Skip to content

Commit 1c5f02c

Browse files
Decouple environment and model difference summary
1 parent e584eaa commit 1c5f02c

4 files changed

Lines changed: 96 additions & 45 deletions

File tree

sqlmesh/core/console.py

Lines changed: 76 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,20 @@ def stop_env_migration_progress(self, success: bool = True) -> None:
282282
"""Stop the environment migration progress."""
283283

284284
@abc.abstractmethod
285-
def show_difference_summary(
285+
def show_environment_difference_summary(
286+
self,
287+
context_diff: ContextDiff,
288+
no_diff: bool = True,
289+
) -> None:
290+
"""Displays a summary of differences for the environment."""
291+
292+
@abc.abstractmethod
293+
def show_model_difference_summary(
286294
self,
287295
context_diff: ContextDiff,
288296
environment_naming_info: EnvironmentNamingInfo,
289297
default_catalog: t.Optional[str],
290298
no_diff: bool = True,
291-
show_environment_statements: bool = True,
292299
) -> None:
293300
"""Displays a summary of differences for the given models."""
294301

@@ -552,14 +559,19 @@ def update_state_import_progress(
552559
def stop_state_import(self, success: bool, input_file: Path) -> None:
553560
pass
554561

555-
def show_difference_summary(
562+
def show_environment_difference_summary(
563+
self,
564+
context_diff: ContextDiff,
565+
no_diff: bool = True,
566+
) -> None:
567+
pass
568+
569+
def show_model_difference_summary(
556570
self,
557571
context_diff: ContextDiff,
558572
environment_naming_info: EnvironmentNamingInfo,
559573
default_catalog: t.Optional[str],
560574
no_diff: bool = True,
561-
show_environment_statements: bool = True,
562-
ignored_snapshot_ids: t.Optional[t.Set[SnapshotId]] = None,
563575
) -> None:
564576
pass
565577

@@ -1312,21 +1324,16 @@ def stop_state_import(self, success: bool, input_file: Path) -> None:
13121324
else:
13131325
self.log_error("State import failed!")
13141326

1315-
def show_difference_summary(
1327+
def show_environment_difference_summary(
13161328
self,
13171329
context_diff: ContextDiff,
1318-
environment_naming_info: EnvironmentNamingInfo,
1319-
default_catalog: t.Optional[str],
13201330
no_diff: bool = True,
1321-
show_environment_statements: bool = True,
13221331
) -> None:
1323-
"""Shows a summary of the differences.
1332+
"""Shows a summary of the environment differences.
13241333
13251334
Args:
13261335
context_diff: The context diff to use to print the summary
1327-
environment_naming_info: The environment naming info to reference when printing model names
1328-
default_catalog: The default catalog to reference when deciding to remove catalog from display names
1329-
no_diff: Hide the actual SQL differences.
1336+
no_diff: Hide the actual environment statement differences.
13301337
"""
13311338
if context_diff.is_new_environment:
13321339
msg = (
@@ -1361,13 +1368,28 @@ def show_difference_summary(
13611368
if context_diff.has_requirement_changes:
13621369
self._print(f"[bold]Requirements:\n{context_diff.requirements_diff()}")
13631370

1364-
if context_diff.has_environment_statements_changes and show_environment_statements:
1371+
if context_diff.has_environment_statements_changes and not no_diff:
13651372
self._print("[bold]Environment statements:\n")
13661373
for type, diff in context_diff.environment_statements_diff(
13671374
include_python_env=not context_diff.is_new_environment
13681375
):
13691376
self._print(Syntax(diff, type, line_numbers=False))
13701377

1378+
def show_model_difference_summary(
1379+
self,
1380+
context_diff: ContextDiff,
1381+
environment_naming_info: EnvironmentNamingInfo,
1382+
default_catalog: t.Optional[str],
1383+
no_diff: bool = True,
1384+
) -> None:
1385+
"""Shows a summary of the model differences.
1386+
1387+
Args:
1388+
context_diff: The context diff to use to print the summary
1389+
environment_naming_info: The environment naming info to reference when printing model names
1390+
default_catalog: The default catalog to reference when deciding to remove catalog from display names
1391+
no_diff: Hide the actual SQL differences.
1392+
"""
13711393
self._show_summary_tree_for(
13721394
context_diff,
13731395
"Models",
@@ -1543,13 +1565,18 @@ def _prompt_categorize(
15431565
"""Get the user's change category for the directly modified models."""
15441566
plan = plan_builder.build()
15451567

1546-
self.show_difference_summary(
1568+
self.show_environment_difference_summary(
15471569
plan.context_diff,
1548-
plan.environment_naming_info,
1549-
default_catalog=default_catalog,
1550-
show_environment_statements=not no_diff,
1570+
no_diff=no_diff,
15511571
)
15521572

1573+
if plan.context_diff.has_changes:
1574+
self.show_model_difference_summary(
1575+
plan.context_diff,
1576+
plan.environment_naming_info,
1577+
default_catalog=default_catalog,
1578+
)
1579+
15531580
if not no_diff:
15541581
self._show_categorized_snapshots(plan, default_catalog)
15551582

@@ -2489,21 +2516,16 @@ class MarkdownConsole(CaptureTerminalConsole):
24892516
def __init__(self, **kwargs: t.Any) -> None:
24902517
super().__init__(**{**kwargs, "console": RichConsole(no_color=True)})
24912518

2492-
def show_difference_summary(
2519+
def show_environment_difference_summary(
24932520
self,
24942521
context_diff: ContextDiff,
2495-
environment_naming_info: EnvironmentNamingInfo,
2496-
default_catalog: t.Optional[str],
24972522
no_diff: bool = True,
2498-
show_environment_statements: bool = True,
24992523
) -> None:
2500-
"""Shows a summary of the differences.
2524+
"""Shows a summary of the environment differences.
25012525
25022526
Args:
25032527
context_diff: The context diff to use to print the summary.
2504-
environment_naming_info: The environment naming info to reference when printing model names
2505-
default_catalog: The default catalog to reference when deciding to remove catalog from display names
2506-
no_diff: Hide the actual SQL differences.
2528+
no_diff: Hide the actual environment statements differences.
25072529
"""
25082530
if context_diff.is_new_environment:
25092531
self._print(
@@ -2521,13 +2543,28 @@ def show_difference_summary(
25212543
if context_diff.has_requirement_changes:
25222544
self._print(f"Requirements:\n{context_diff.requirements_diff()}")
25232545

2524-
if context_diff.has_environment_statements_changes and show_environment_statements:
2546+
if context_diff.has_environment_statements_changes and not no_diff:
25252547
self._print("[bold]Environment statements:\n")
25262548
for _, diff in context_diff.environment_statements_diff(
25272549
include_python_env=not context_diff.is_new_environment
25282550
):
25292551
self._print(diff)
25302552

2553+
def show_model_difference_summary(
2554+
self,
2555+
context_diff: ContextDiff,
2556+
environment_naming_info: EnvironmentNamingInfo,
2557+
default_catalog: t.Optional[str],
2558+
no_diff: bool = True,
2559+
) -> None:
2560+
"""Shows a summary of the model differences.
2561+
2562+
Args:
2563+
context_diff: The context diff to use to print the summary.
2564+
environment_naming_info: The environment naming info to reference when printing model names
2565+
default_catalog: The default catalog to reference when deciding to remove catalog from display names
2566+
no_diff: Hide the actual SQL differences.
2567+
"""
25312568
added_snapshots = {context_diff.snapshots[s_id] for s_id in context_diff.added}
25322569
added_snapshot_models = {s for s in added_snapshots if s.is_model}
25332570
if added_snapshot_models:
@@ -3030,26 +3067,32 @@ def update_env_migration_progress(self, num_tasks: int) -> None:
30303067
def stop_env_migration_progress(self, success: bool = True) -> None:
30313068
self._write(f"Stopping environment migration with success={success}")
30323069

3033-
def show_difference_summary(
3070+
def show_environment_difference_summary(
30343071
self,
30353072
context_diff: ContextDiff,
3036-
environment_naming_info: EnvironmentNamingInfo,
3037-
default_catalog: t.Optional[str],
30383073
no_diff: bool = True,
3039-
show_environment_statements: bool = True,
30403074
) -> None:
3041-
self._write("Model Difference Summary:")
3075+
self._write("Environment Difference Summary:")
30423076

30433077
if context_diff.has_requirement_changes:
30443078
self._write(f"Requirements:\n{context_diff.requirements_diff()}")
30453079

3046-
if context_diff.has_environment_statements_changes and show_environment_statements:
3080+
if context_diff.has_environment_statements_changes and not no_diff:
30473081
self._write("Environment statements:\n")
30483082
for _, diff in context_diff.environment_statements_diff(
30493083
include_python_env=not context_diff.is_new_environment
30503084
):
30513085
self._write(diff)
30523086

3087+
def show_model_difference_summary(
3088+
self,
3089+
context_diff: ContextDiff,
3090+
environment_naming_info: EnvironmentNamingInfo,
3091+
default_catalog: t.Optional[str],
3092+
no_diff: bool = True,
3093+
) -> None:
3094+
self._write("Model Difference Summary:")
3095+
30533096
for added in context_diff.new_snapshots:
30543097
self._write(f" Added: {added}")
30553098
for removed in context_diff.removed_snapshots:

sqlmesh/core/context.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,18 +1533,22 @@ def diff(self, environment: t.Optional[str] = None, detailed: bool = False) -> b
15331533
environment = environment or self.config.default_target_environment
15341534
environment = Environment.sanitize_name(environment)
15351535
context_diff = self._context_diff(environment)
1536-
self.console.show_difference_summary(
1536+
self.console.show_environment_difference_summary(
15371537
context_diff,
1538-
EnvironmentNamingInfo.from_environment_catalog_mapping(
1539-
self.config.environment_catalog_mapping,
1540-
name=environment,
1541-
suffix_target=self.config.environment_suffix_target,
1542-
normalize_name=context_diff.normalize_environment_name,
1543-
),
1544-
self.default_catalog,
15451538
no_diff=not detailed,
1546-
show_environment_statements=detailed,
15471539
)
1540+
if context_diff.has_changes:
1541+
self.console.show_model_difference_summary(
1542+
context_diff,
1543+
EnvironmentNamingInfo.from_environment_catalog_mapping(
1544+
self.config.environment_catalog_mapping,
1545+
name=environment,
1546+
suffix_target=self.config.environment_suffix_target,
1547+
normalize_name=context_diff.normalize_environment_name,
1548+
),
1549+
self.default_catalog,
1550+
no_diff=not detailed,
1551+
)
15481552
return context_diff.has_changes
15491553

15501554
@python_api_analytics

sqlmesh/integrations/github/cicd/controller.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,15 @@ def get_plan_summary(self, plan: Plan) -> str:
457457
try:
458458
# Clear out any output that might exist from prior steps
459459
self._console.clear_captured_outputs()
460-
self._console.show_difference_summary(
460+
self._console.show_environment_difference_summary(
461+
context_diff=plan.context_diff,
462+
no_diff=False,
463+
)
464+
self._console.show_model_difference_summary(
461465
context_diff=plan.context_diff,
462466
environment_naming_info=plan.environment_naming_info,
463467
default_catalog=self._context.default_catalog,
464468
no_diff=False,
465-
show_environment_statements=True,
466469
)
467470
difference_summary = self._console.consume_captured_output()
468471
self._console._show_missing_dates(plan, self._context.default_catalog)

tests/core/test_context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ def test_diff(sushi_context: Context, mocker: MockerFixture):
287287

288288
sushi_context.upsert_model("sushi.customers", query=parse_one("select 1 as customer_id"))
289289
sushi_context.diff("test")
290-
assert mock_console.show_difference_summary.called
290+
assert mock_console.show_environment_difference_summary.called
291+
assert mock_console.show_model_difference_summary.called
291292
assert success
292293

293294

0 commit comments

Comments
 (0)