Skip to content

Commit 863eb01

Browse files
Chore: Refine environment statements cli output
1 parent 1865bd9 commit 863eb01

5 files changed

Lines changed: 53 additions & 15 deletions

File tree

sqlmesh/core/console.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ def show_model_difference_summary(
282282
environment_naming_info: EnvironmentNamingInfo,
283283
default_catalog: t.Optional[str],
284284
no_diff: bool = True,
285+
show_environment_statements: bool = True,
285286
) -> None:
286287
"""Displays a summary of differences for the given models."""
287288

@@ -555,6 +556,7 @@ def show_model_difference_summary(
555556
environment_naming_info: EnvironmentNamingInfo,
556557
default_catalog: t.Optional[str],
557558
no_diff: bool = True,
559+
show_environment_statements: bool = True,
558560
ignored_snapshot_ids: t.Optional[t.Set[SnapshotId]] = None,
559561
) -> None:
560562
pass
@@ -1270,6 +1272,7 @@ def show_model_difference_summary(
12701272
environment_naming_info: EnvironmentNamingInfo,
12711273
default_catalog: t.Optional[str],
12721274
no_diff: bool = True,
1275+
show_environment_statements: bool = True,
12731276
) -> None:
12741277
"""Shows a summary of the differences.
12751278
@@ -1312,9 +1315,11 @@ def show_model_difference_summary(
13121315
if context_diff.has_requirement_changes:
13131316
self._print(f"[bold]Requirements:\n{context_diff.requirements_diff()}")
13141317

1315-
if context_diff.has_environment_statements_changes:
1318+
if context_diff.has_environment_statements_changes and show_environment_statements:
13161319
self._print("[bold]Environment statements:\n")
1317-
for type, diff in context_diff.environment_statements_diff():
1320+
for type, diff in context_diff.environment_statements_diff(
1321+
include_python_env=not context_diff.is_new_environment
1322+
):
13181323
self._print(Syntax(diff, type, line_numbers=False))
13191324

13201325
self._show_summary_tree_for(
@@ -1496,6 +1501,7 @@ def _prompt_categorize(
14961501
plan.context_diff,
14971502
plan.environment_naming_info,
14981503
default_catalog=default_catalog,
1504+
show_environment_statements=not no_diff,
14991505
)
15001506

15011507
if not no_diff:
@@ -2443,6 +2449,7 @@ def show_model_difference_summary(
24432449
environment_naming_info: EnvironmentNamingInfo,
24442450
default_catalog: t.Optional[str],
24452451
no_diff: bool = True,
2452+
show_environment_statements: bool = True,
24462453
) -> None:
24472454
"""Shows a summary of the differences.
24482455
@@ -2468,9 +2475,11 @@ def show_model_difference_summary(
24682475
if context_diff.has_requirement_changes:
24692476
self._print(f"Requirements:\n{context_diff.requirements_diff()}")
24702477

2471-
if context_diff.has_environment_statements_changes:
2478+
if context_diff.has_environment_statements_changes and show_environment_statements:
24722479
self._print("[bold]Environment statements:\n")
2473-
for _, diff in context_diff.environment_statements_diff():
2480+
for _, diff in context_diff.environment_statements_diff(
2481+
include_python_env=not context_diff.is_new_environment
2482+
):
24742483
self._print(diff)
24752484

24762485
added_snapshots = {context_diff.snapshots[s_id] for s_id in context_diff.added}
@@ -2977,15 +2986,18 @@ def show_model_difference_summary(
29772986
environment_naming_info: EnvironmentNamingInfo,
29782987
default_catalog: t.Optional[str],
29792988
no_diff: bool = True,
2989+
show_environment_statements: bool = True,
29802990
) -> None:
29812991
self._write("Model Difference Summary:")
29822992

29832993
if context_diff.has_requirement_changes:
29842994
self._write(f"Requirements:\n{context_diff.requirements_diff()}")
29852995

2986-
if context_diff.has_environment_statements_changes:
2996+
if context_diff.has_environment_statements_changes and show_environment_statements:
29872997
self._write("Environment statements:\n")
2988-
for _, diff in context_diff.environment_statements_diff():
2998+
for _, diff in context_diff.environment_statements_diff(
2999+
include_python_env=not context_diff.is_new_environment
3000+
):
29893001
self._write(diff)
29903002

29913003
for added in context_diff.new_snapshots:

sqlmesh/core/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,7 @@ def diff(self, environment: t.Optional[str] = None, detailed: bool = False) -> b
15431543
),
15441544
self.default_catalog,
15451545
no_diff=not detailed,
1546+
show_environment_statements=detailed,
15461547
)
15471548
return context_diff.has_changes
15481549

sqlmesh/core/context_diff.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import sys
1616
import typing as t
17-
from difflib import ndiff
17+
from difflib import ndiff, unified_diff
1818
from functools import cached_property
1919
from sqlmesh.core import constants as c
2020
from sqlmesh.core.console import get_console
@@ -341,16 +341,19 @@ def requirements_diff(self) -> str:
341341
)
342342
)
343343

344-
def environment_statements_diff(self) -> t.List[t.Tuple[str, str]]:
344+
def environment_statements_diff(
345+
self, include_python_env: bool = False
346+
) -> t.List[t.Tuple[str, str]]:
345347
def extract_statements(statements: t.List[EnvironmentStatements], attr: str) -> t.List[str]:
346348
return [
347-
expr
349+
string
348350
for statement in statements
349351
for expr in (
350352
sorted_python_env_payloads(statement.python_env)
351353
if attr == "python_env"
352354
else getattr(statement, attr)
353355
)
356+
for string in expr.split("\n")
354357
]
355358

356359
def compute_diff(attribute: str) -> t.Optional[t.Tuple[str, str]]:
@@ -360,21 +363,24 @@ def compute_diff(attribute: str) -> t.Optional[t.Tuple[str, str]]:
360363
if previous == current:
361364
return None
362365

363-
diff_lines = list(ndiff(previous, current))
364366
diff_text = attribute if not attribute == "python_env" else "dependencies"
365367
diff_text += ":\n"
368+
if attribute == "python_env":
369+
diff = list(unified_diff(previous, current))
370+
diff_text += "\n".join(diff[2:] if len(diff) > 1 else diff)
371+
return "python", diff_text + "\n"
366372

373+
diff_lines = list(ndiff(previous, current))
367374
if any(line.startswith(("-", "+")) for line in diff_lines):
368375
diff_text += " " + "\n ".join(diff_lines) + "\n"
369-
370-
return "python" if attribute == "python_env" else "sql", diff_text
376+
return "sql", diff_text
371377

372378
return [
373379
diff
374380
for attribute in [
375381
RuntimeStage.BEFORE_ALL.value,
376382
RuntimeStage.AFTER_ALL.value,
377-
"python_env",
383+
*(["python_env"] if include_python_env else []),
378384
]
379385
if (diff := compute_diff(attribute)) is not None
380386
]

sqlmesh/integrations/github/cicd/controller.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ def get_plan_summary(self, plan: Plan) -> str:
462462
environment_naming_info=plan.environment_naming_info,
463463
default_catalog=self._context.default_catalog,
464464
no_diff=False,
465+
show_environment_statements=True,
465466
)
466467
difference_summary = self._console.consume_captured_output()
467468
self._console._show_missing_dates(plan, self._context.default_catalog)

tests/core/test_plan.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,15 +3023,33 @@ def test_plan_environment_statements_diff(make_snapshot):
30233023
terminal_console._print(diff)
30243024
output = console_output.getvalue()
30253025
stripped = strip_ansi_codes(output)
3026+
3027+
expected_output = (
3028+
"before_all:\n"
3029+
" + CREATE OR REPLACE TABLE table_1 AS SELECT 1\n"
3030+
" + @test_macro()\n\n"
3031+
"after_all:\n"
3032+
" + CREATE OR REPLACE TABLE table_2 AS SELECT 2"
3033+
)
3034+
assert stripped == expected_output
3035+
console_output.close()
3036+
3037+
# Validate with python env included
3038+
console_output, terminal_console = create_test_console()
3039+
for _, diff in context_diff.environment_statements_diff(include_python_env=True):
3040+
terminal_console._print(diff)
3041+
output = console_output.getvalue()
3042+
stripped = strip_ansi_codes(output)
30263043
expected_output = (
30273044
"before_all:\n"
30283045
" + CREATE OR REPLACE TABLE table_1 AS SELECT 1\n"
30293046
" + @test_macro()\n\n"
30303047
"after_all:\n"
30313048
" + CREATE OR REPLACE TABLE table_2 AS SELECT 2\n\n"
30323049
"dependencies:\n"
3033-
" + def test_macro(evaluator):\n"
3034-
" return 'one'"
3050+
"@@ -0,0 +1,2 @@\n\n"
3051+
"+def test_macro(evaluator):\n"
3052+
"+ return 'one'"
30353053
)
30363054
assert stripped == expected_output
30373055
console_output.close()

0 commit comments

Comments
 (0)