diff --git a/docs/concepts/plans.md b/docs/concepts/plans.md index c3e12652ee..75e047d119 100644 --- a/docs/concepts/plans.md +++ b/docs/concepts/plans.md @@ -228,7 +228,7 @@ The `start_end_model` is of kind `INCREMENTAL_BY_UNIQUE_KEY`, which is non-idemp Because the command's `--start` of 2024-09-24 is after `start_end_model`'s start date 2024-09-23, `start_end_model` is marked as preview: -``` bash linenums="1" hl_lines="12-13 20-21" +``` bash linenums="1" hl_lines="12-13 20-22" ❯ sqlmesh plan dev --start 2024-09-24 ====================================================================== Successfully Ran 1 tests against duckdb @@ -248,8 +248,9 @@ Models: Directly Modified: sqlmesh_example__dev.start_end_model (Non-breaking) Models needing backfill (missing dates): -├── sqlmesh_example__dev.incremental_model: 2024-09-24 - 2024-09-26 -└── sqlmesh_example__dev.start_end_model: 2024-09-24 - 2024-09-26 (preview) +└── sqlmesh_example__dev.incremental_model: 2024-09-24 - 2024-09-26 +Models needing preview (data will NOT be reused in production): +└── sqlmesh_example__dev.start_end_model: 2024-09-24 - 2024-09-26 Enter the backfill end date (eg. '1 month ago', '2020-01-01') or blank to backfill up until '2024-09-27 00:00:00': ``` @@ -315,13 +316,14 @@ As mentioned earlier, the data output produced by [forward-only changes](#forwar The same holds true for any subsequent changes that depend on undeployed forward-only changes - data can be previewed but can't be reused in production. -Backfills that are exclusively for preview purposes and will not be reused upon deployment to production are explicitly labeled with `(preview)` in the plan summary: +Backfills that are exclusively for preview purposes and will not be reused upon deployment to production are listed in a separate "Models needing preview" section of the plan summary: ```bash Models needing backfill (missing dates): -├── sushi__dev.customers: 2023-12-22 - 2023-12-28 (preview) ├── sushi__dev.waiter_revenue_by_day: 2023-12-22 - 2023-12-28 -├── sushi__dev.top_waiters: 2023-12-22 - 2023-12-28 -└── sushi__dev.waiter_as_customer_by_day: 2023-12-22 - 2023-12-28 (preview) +└── sushi__dev.top_waiters: 2023-12-22 - 2023-12-28 +Models needing preview (data will NOT be reused in production): +├── sushi__dev.customers: 2023-12-22 - 2023-12-28 +└── sushi__dev.waiter_as_customer_by_day: 2023-12-22 - 2023-12-28 ``` ## Forward-only plans diff --git a/docs/examples/incremental_time_full_walkthrough.md b/docs/examples/incremental_time_full_walkthrough.md index ffa9def911..54d69d1cb9 100644 --- a/docs/examples/incremental_time_full_walkthrough.md +++ b/docs/examples/incremental_time_full_walkthrough.md @@ -724,8 +724,8 @@ Models: Directly Modified: demo__dev.incrementals_demo (Forward-only) Enter the effective date (eg. '1 year', '2020-01-01') to apply forward-only changes retroactively or blank to only apply them going forward once changes are deployed to prod: -Models needing backfill (missing dates): -└── demo__dev.incrementals_demo: 2024-11-07 - 2024-11-07 (preview) +Models needing preview (data will NOT be reused in production): +└── demo__dev.incrementals_demo: 2024-11-07 - 2024-11-07 Enter the preview start date (eg. '1 year', '2020-01-01') or blank to backfill to preview starting from yesterday: 2024-10-27 Enter the preview end date (eg. '1 month ago', '2020-01-01') or blank to preview up until '2024-11-08 00:00:00': Apply - Preview Tables [y/n]: y diff --git a/docs/examples/sqlmesh_cli_crash_course.md b/docs/examples/sqlmesh_cli_crash_course.md index 0bf5780f12..d70fb32b1f 100644 --- a/docs/examples/sqlmesh_cli_crash_course.md +++ b/docs/examples/sqlmesh_cli_crash_course.md @@ -1166,10 +1166,10 @@ If you want to see a full walkthrough, [go here](incremental_time_full_walkthrou └── Indirectly Modified Children: ├── sqlmesh_example__dev.full_model (Forward-only) └── sqlmesh_example__dev.view_model (Forward-only) - Models needing backfill: - ├── sqlmesh_example__dev.full_model: [full refresh] (preview) - ├── sqlmesh_example__dev.incremental_model: [2025-04-17 - 2025-04-17] (preview) - └── sqlmesh_example__dev.view_model: [recreate view] (preview) + Models needing preview (data will NOT be reused in production): + ├── sqlmesh_example__dev.full_model: [full refresh] + ├── sqlmesh_example__dev.incremental_model: [2025-04-17 - 2025-04-17] + └── sqlmesh_example__dev.view_model: [recreate view] Apply - Preview Tables [y/n]: y Updating physical layer ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100.0% • 3/3 • 0:00:00 diff --git a/sqlmesh/core/console.py b/sqlmesh/core/console.py index 7b9264537f..9227dfea7c 100644 --- a/sqlmesh/core/console.py +++ b/sqlmesh/core/console.py @@ -2150,27 +2150,25 @@ def _show_missing_dates(self, plan: Plan, default_catalog: t.Optional[str]) -> N if not missing_intervals: return backfill = Tree("[bold]Models needing backfill:[/bold]") + preview = Tree( + "[bold]Models needing preview ([orange1]data will NOT be reused in production[/orange1]):[/bold]" + ) for missing in missing_intervals: snapshot = plan.context_diff.snapshots[missing.snapshot_id] if not snapshot.is_model: continue - preview_modifier = "" - if not plan.deployability_index.is_deployable(snapshot): - preview_modifier = " ([orange1]preview[/orange1])" - display_name = snapshot.display_name( plan.environment_naming_info, default_catalog if self.verbosity < Verbosity.VERY_VERBOSE else None, dialect=self.dialect, ) - backfill.add( - f"{display_name}: \\[{_format_missing_intervals(snapshot, missing)}]{preview_modifier}" - ) + tree = backfill if plan.deployability_index.is_deployable(snapshot) else preview + tree.add(f"{display_name}: \\[{_format_missing_intervals(snapshot, missing)}]") - if backfill: - backfill = self._limit_model_names(backfill, self.verbosity) - self._print(backfill) + for tree in (backfill, preview): + if tree.children: + self._print(self._limit_model_names(tree, self.verbosity)) def _prompt_effective_from( self, plan_builder: PlanBuilder, auto_apply: bool, default_catalog: t.Optional[str] @@ -3526,37 +3524,41 @@ def _show_missing_dates(self, plan: Plan, default_catalog: t.Optional[str]) -> N missing_intervals = plan.missing_intervals if not missing_intervals: return - self._print("\n**Models needing backfill:**") - snapshots = [] + backfill: t.List[str] = [] + preview: t.List[str] = [] for missing in missing_intervals: snapshot = plan.context_diff.snapshots[missing.snapshot_id] if not snapshot.is_model: continue - preview_modifier = "" - if not plan.deployability_index.is_deployable(snapshot): - preview_modifier = " (**preview**)" - display_name = snapshot.display_name( plan.environment_naming_info, default_catalog if self.verbosity < Verbosity.VERY_VERBOSE else None, dialect=self.dialect, ) - snapshots.append( - f"* `{display_name}`: \\[{_format_missing_intervals(snapshot, missing)}]{preview_modifier}" + entries = backfill if plan.deployability_index.is_deployable(snapshot) else preview + entries.append( + f"* `{display_name}`: \\[{_format_missing_intervals(snapshot, missing)}]" ) - length = len(snapshots) - if ( - self.verbosity < Verbosity.VERY_VERBOSE - and length > self.INDIRECTLY_MODIFIED_DISPLAY_THRESHOLD + for header, snapshots in ( + ("**Models needing backfill:**", backfill), + ("**Models needing preview** (data will NOT be reused in production):", preview), ): - self._print(snapshots[0]) - self._print(f"- `.... {length - 2} more ....`\n") - self._print(snapshots[-1]) - else: - for snap in snapshots: - self._print(snap) + if not snapshots: + continue + self._print(f"\n{header}") + length = len(snapshots) + if ( + self.verbosity < Verbosity.VERY_VERBOSE + and length > self.INDIRECTLY_MODIFIED_DISPLAY_THRESHOLD + ): + self._print(snapshots[0]) + self._print(f"- `.... {length - 2} more ....`\n") + self._print(snapshots[-1]) + else: + for snap in snapshots: + self._print(snap) def _show_categorized_snapshots(self, plan: Plan, default_catalog: t.Optional[str]) -> None: context_diff = plan.context_diff diff --git a/tests/core/test_context.py b/tests/core/test_context.py index 71276b7396..a86a354110 100644 --- a/tests/core/test_context.py +++ b/tests/core/test_context.py @@ -18,7 +18,7 @@ import sqlmesh.core.constants from sqlmesh.cli.project_init import init_example_project from sqlmesh.core.audit import StandaloneAudit -from sqlmesh.core.console import TerminalConsole +from sqlmesh.core.console import MarkdownConsole, TerminalConsole from sqlmesh.core import dialect as d, constants as c from sqlmesh.core.config import ( load_configs, @@ -2925,6 +2925,89 @@ def test_plan_runs_audits_on_dev_previews(sushi_context: Context, capsys, caplog assert "Virtual layer updated" in stdout +@pytest.mark.parametrize( + "console_cls,backfill_header,preview_header", + [ + ( + TerminalConsole, + "Models needing backfill:", + "Models needing preview (data will NOT be reused in production):", + ), + ( + MarkdownConsole, + "**Models needing backfill:**", + "**Models needing preview** (data will NOT be reused in production):", + ), + ], +) +def test_plan_summary_separates_preview_backfills( + sushi_context: Context, capsys, console_cls, backfill_header, preview_header +): + console = console_cls() + sushi_context.console = console + + def forward_only_model(extra_column: str) -> Model: + return load_sql_based_model( + parse( + f""" + MODEL ( + name sushi.test_preview_model, + kind INCREMENTAL_BY_TIME_RANGE (time_column event_date, forward_only true) + ); + + SELECT *{extra_column} FROM sushi.orders + WHERE event_date BETWEEN @start_ts AND @end_ts + """ + ), + default_catalog=sushi_context.default_catalog, + ) + + sushi_context.upsert_model(forward_only_model("")) + sushi_context.plan(auto_apply=True, no_prompts=True) + + # A forward-only change is only previewed in dev, while a new model is backfilled for real + sushi_context.upsert_model(forward_only_model(", 1 AS new_col")) + sushi_context.upsert_model( + load_sql_based_model( + parse("MODEL (name sushi.test_new_model, kind FULL); SELECT 1 AS id"), + default_catalog=sushi_context.default_catalog, + ) + ) + plan = sushi_context.plan_builder("dev", skip_tests=True).build() + + capsys.readouterr() # clear output buffer + console._show_missing_dates(plan, sushi_context.default_catalog) + output = capsys.readouterr().out + + assert output.index(backfill_header) < output.index(preview_header) + + backfill_section, preview_section = output.split(preview_header) + assert "sushi__dev.test_new_model" in backfill_section + assert "test_preview_model" not in backfill_section + assert "sushi__dev.test_preview_model" in preview_section + assert "test_new_model" not in preview_section + assert "(preview)" not in output + + +def test_plan_summary_omits_empty_preview_section(sushi_context: Context, capsys): + console = TerminalConsole() + sushi_context.console = console + sushi_context.upsert_model( + load_sql_based_model( + parse("MODEL (name sushi.test_new_model, kind FULL); SELECT 1 AS id"), + default_catalog=sushi_context.default_catalog, + ) + ) + plan = sushi_context.plan_builder("dev", skip_tests=True).build() + + capsys.readouterr() + console._show_missing_dates(plan, sushi_context.default_catalog) + output = capsys.readouterr().out + + assert "Models needing backfill:" in output + assert "Models needing preview" not in output + + def test_environment_statements(tmp_path: pathlib.Path): models_dir = pathlib.Path("models") macros_dir = pathlib.Path("macros")