From 6fe3da3328eb8f524854f21b54ee81b08b316b24 Mon Sep 17 00:00:00 2001 From: lafirm <136463254+lafirm@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:28:29 +0530 Subject: [PATCH] fix: load materializations during cleanup Signed-off-by: lafirm <136463254+lafirm@users.noreply.github.com> --- sqlmesh/core/context.py | 2 ++ tests/core/integration/test_aux_commands.py | 23 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/sqlmesh/core/context.py b/sqlmesh/core/context.py index 39590a0fa3..fb341c4fa6 100644 --- a/sqlmesh/core/context.py +++ b/sqlmesh/core/context.py @@ -3182,6 +3182,8 @@ def _run_janitor( current_ts = now_timestamp() failures: t.List[str] = [] + self._load_materializations() + # Clean up expired environments by removing their views and schemas failures.extend( self._cleanup_environments( diff --git a/tests/core/integration/test_aux_commands.py b/tests/core/integration/test_aux_commands.py index 7de585576d..c91fa7a30c 100644 --- a/tests/core/integration/test_aux_commands.py +++ b/tests/core/integration/test_aux_commands.py @@ -86,6 +86,29 @@ def test_table_name(init_and_plan_context: t.Callable): ) +def test_janitor_loads_materialization(mocker: MockerFixture, tmp_path: Path): + models_dir = tmp_path / "models" + models_dir.mkdir(exist_ok=True) + + test_model = models_dir / "model.sql" + + with open(test_model, "w") as f: + f.write( + "MODEL(name test.model, kind CUSTOM (materialization 'custom_full')); SELECT 1 AS col" + ) + + config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")) + ctx = Context(paths=[tmp_path], config=config) + ctx.plan("dev", no_prompts=True, auto_apply=True) + + load_materializations_spy = mocker.spy(Context, "_load_materializations") + + ctx.invalidate_environment("dev") + ctx.run_janitor(ignore_ttl=True) + + load_materializations_spy.assert_called_once() + + def test_janitor_cleanup_order(mocker: MockerFixture, tmp_path: Path): def setup_scenario(): models_dir = tmp_path / "models"