Skip to content

Commit ca3a1c6

Browse files
Chore: Refine environment statements cli output
1 parent ccd72c6 commit ca3a1c6

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
@@ -288,6 +288,7 @@ def show_model_difference_summary(
288288
environment_naming_info: EnvironmentNamingInfo,
289289
default_catalog: t.Optional[str],
290290
no_diff: bool = True,
291+
show_environment_statements: bool = True,
291292
) -> None:
292293
"""Displays a summary of differences for the given models."""
293294

@@ -557,6 +558,7 @@ def show_model_difference_summary(
557558
environment_naming_info: EnvironmentNamingInfo,
558559
default_catalog: t.Optional[str],
559560
no_diff: bool = True,
561+
show_environment_statements: bool = True,
560562
ignored_snapshot_ids: t.Optional[t.Set[SnapshotId]] = None,
561563
) -> None:
562564
pass
@@ -1316,6 +1318,7 @@ def show_model_difference_summary(
13161318
environment_naming_info: EnvironmentNamingInfo,
13171319
default_catalog: t.Optional[str],
13181320
no_diff: bool = True,
1321+
show_environment_statements: bool = True,
13191322
) -> None:
13201323
"""Shows a summary of the differences.
13211324
@@ -1358,9 +1361,11 @@ def show_model_difference_summary(
13581361
if context_diff.has_requirement_changes:
13591362
self._print(f"[bold]Requirements:\n{context_diff.requirements_diff()}")
13601363

1361-
if context_diff.has_environment_statements_changes:
1364+
if context_diff.has_environment_statements_changes and show_environment_statements:
13621365
self._print("[bold]Environment statements:\n")
1363-
for type, diff in context_diff.environment_statements_diff():
1366+
for type, diff in context_diff.environment_statements_diff(
1367+
include_python_env=not context_diff.is_new_environment
1368+
):
13641369
self._print(Syntax(diff, type, line_numbers=False))
13651370

13661371
self._show_summary_tree_for(
@@ -1542,6 +1547,7 @@ def _prompt_categorize(
15421547
plan.context_diff,
15431548
plan.environment_naming_info,
15441549
default_catalog=default_catalog,
1550+
show_environment_statements=not no_diff,
15451551
)
15461552

15471553
if not no_diff:
@@ -2489,6 +2495,7 @@ def show_model_difference_summary(
24892495
environment_naming_info: EnvironmentNamingInfo,
24902496
default_catalog: t.Optional[str],
24912497
no_diff: bool = True,
2498+
show_environment_statements: bool = True,
24922499
) -> None:
24932500
"""Shows a summary of the differences.
24942501
@@ -2514,9 +2521,11 @@ def show_model_difference_summary(
25142521
if context_diff.has_requirement_changes:
25152522
self._print(f"Requirements:\n{context_diff.requirements_diff()}")
25162523

2517-
if context_diff.has_environment_statements_changes:
2524+
if context_diff.has_environment_statements_changes and show_environment_statements:
25182525
self._print("[bold]Environment statements:\n")
2519-
for _, diff in context_diff.environment_statements_diff():
2526+
for _, diff in context_diff.environment_statements_diff(
2527+
include_python_env=not context_diff.is_new_environment
2528+
):
25202529
self._print(diff)
25212530

25222531
added_snapshots = {context_diff.snapshots[s_id] for s_id in context_diff.added}
@@ -3027,15 +3036,18 @@ def show_model_difference_summary(
30273036
environment_naming_info: EnvironmentNamingInfo,
30283037
default_catalog: t.Optional[str],
30293038
no_diff: bool = True,
3039+
show_environment_statements: bool = True,
30303040
) -> None:
30313041
self._write("Model Difference Summary:")
30323042

30333043
if context_diff.has_requirement_changes:
30343044
self._write(f"Requirements:\n{context_diff.requirements_diff()}")
30353045

3036-
if context_diff.has_environment_statements_changes:
3046+
if context_diff.has_environment_statements_changes and show_environment_statements:
30373047
self._write("Environment statements:\n")
3038-
for _, diff in context_diff.environment_statements_diff():
3048+
for _, diff in context_diff.environment_statements_diff(
3049+
include_python_env=not context_diff.is_new_environment
3050+
):
30393051
self._write(diff)
30403052

30413053
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)