diff --git a/sqlmesh/core/context.py b/sqlmesh/core/context.py index c3eaed4e6b..bca41e9e9e 100644 --- a/sqlmesh/core/context.py +++ b/sqlmesh/core/context.py @@ -1799,10 +1799,9 @@ def plan_builder( expanded_restate_models = None if restate_models is not None: - # Restatements operate entirely against state. Build a dedicated - # selector over the union of local and environment models so that - # both name lookup and upstream expansion (e.g. ``-r '+model'``) - # reach dependencies that only exist in the target environment. + # Restatements operate entirely against state. Models can be selected by name from + # either the project or the target environment, but `+` follows the dependencies of + # the deployed models only. env_models_for_restate = model_selector._load_env_models( environment, fallback_env_name=create_from or c.PROD, @@ -1812,7 +1811,7 @@ def plan_builder( for fqn, model in {**env_models_for_restate, **self._models}.items(): restate_models_combined[fqn] = model restate_dag: DAG[str] = DAG() - for fqn, model in restate_models_combined.items(): + for fqn, model in (env_models_for_restate or self._models).items(): restate_dag.add(fqn, model.depends_on) restate_selector = self._new_selector(models=restate_models_combined, dag=restate_dag) expanded_restate_models = restate_selector.expand_model_selections(restate_models) diff --git a/tests/core/integration/test_restatement.py b/tests/core/integration/test_restatement.py index 245aa9d670..7df9045dd2 100644 --- a/tests/core/integration/test_restatement.py +++ b/tests/core/integration/test_restatement.py @@ -1970,3 +1970,80 @@ def test_restatement_plan_outside_parent_date_range(init_and_plan_context: t.Cal ] context.apply(plan) + + +def _write_full_model(models_dir: Path, name: str, query: str) -> None: + (models_dir / f"{name}.sql").write_text(f"MODEL (name test.{name}, kind FULL);\n{query};\n") + + +def _restatement_context_with_deployed_a_and_b(tmp_path: Path) -> t.Tuple[Context, Path]: + """Deploys `a`, `b` (reads `a`) and an unrelated `other` to prod.""" + models_dir = tmp_path / "models" + models_dir.mkdir() + _write_full_model(models_dir, "a", "SELECT 1 AS x") + _write_full_model(models_dir, "b", "SELECT x FROM test.a") + _write_full_model(models_dir, "other", "SELECT 1 AS x") + + config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")) + context = Context(paths=tmp_path, config=config) + context.plan(auto_apply=True, no_prompts=True) + return context, models_dir + + +@pytest.mark.parametrize( + "selection, expected", + [ + ("+test.a", {'"memory"."test"."a"', '"memory"."test"."b"'}), + ("test.other+", {'"memory"."test"."other"'}), + ], +) +def test_restatement_selection_ignores_local_dependency_cycle( + tmp_path: Path, selection: str, expected: t.Set[str] +): + """ + Locally deleting `b` and making `a` read `b` is cyclic only when local and deployed + definitions are merged. Restatement selection must use the deployed graph, so it neither + crashes on `+a` nor on a downstream selection of an unrelated model. + """ + context, models_dir = _restatement_context_with_deployed_a_and_b(tmp_path) + + (models_dir / "b.sql").unlink() + _write_full_model(models_dir, "a", "SELECT x FROM test.b") + context.load() + + plan = context.plan_builder(restate_models=[selection]).build() + + restated = {snapshot_id.name for snapshot_id in plan.restatements} + assert restated == expected + + +def test_restatement_downstream_selection_excludes_undeployed_local_model(tmp_path: Path): + """ + A new local model that reads a deployed model is not in the target environment, so a + downstream restatement selection (`b+`) must not select it or fail with "Model does not exist". + """ + context, models_dir = _restatement_context_with_deployed_a_and_b(tmp_path) + + _write_full_model(models_dir, "c", "SELECT x FROM test.b") + context.load() + + plan = context.plan_builder(restate_models=["test.b+"]).build() + + restated = {snapshot_id.name for snapshot_id in plan.restatements} + assert restated == {'"memory"."test"."b"'} + + +def test_restatement_upstream_selection_follows_deployed_parent(tmp_path: Path): + """ + Locally `b` no longer reads `a`, but the deployed `b` does. Restating `+b` must still select + the deployed parent `a`; the downstream restatement cascade never adds parents. + """ + context, models_dir = _restatement_context_with_deployed_a_and_b(tmp_path) + + _write_full_model(models_dir, "b", "SELECT 1 AS x") + context.load() + + plan = context.plan_builder(restate_models=["+test.b"]).build() + + restated = {snapshot_id.name for snapshot_id in plan.restatements} + assert restated == {'"memory"."test"."a"', '"memory"."test"."b"'}