Skip to content

Commit 9204459

Browse files
authored
Fix: Use the default start in plan when there are paused forward-only snapshots (#1548)
1 parent 1af4bce commit 9204459

2 files changed

Lines changed: 29 additions & 8 deletions

File tree

sqlmesh/core/plan/definition.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,17 @@ def __init__(
107107
self.categorizer_config = categorizer_config or CategorizerConfig()
108108
self.auto_categorization_enabled = auto_categorization_enabled
109109
self.include_unmodified = include_unmodified
110+
111+
self.__snapshot_mapping: t.Optional[t.Dict[str, Snapshot]] = None
112+
self.__dag: t.Optional[DAG[str]] = None
113+
114+
self._start = start
115+
if not self._start and is_dev and (forward_only or self._has_paused_forward_only()):
116+
self._start = default_start or yesterday_ds()
117+
118+
self._end = end if end or not is_dev else (default_end or now())
110119
self._restate_models = set(restate_models or [])
111120
self._effective_from: t.Optional[TimeLike] = None
112-
self._start = (
113-
start if start or not (is_dev and forward_only) else (default_start or yesterday_ds())
114-
)
115-
self._end = end if end or not is_dev else (default_end or now())
116121
self._execution_time = execution_time or now()
117122
self._apply = apply
118123
self.__missing_intervals: t.Optional[t.Dict[t.Tuple[str, str], Intervals]] = None
@@ -215,7 +220,15 @@ def snapshots(self) -> t.List[Snapshot]:
215220
@property
216221
def _snapshot_mapping(self) -> t.Dict[str, Snapshot]:
217222
"""Gets a mapping of snapshot name to snapshot."""
218-
return self.__snapshot_mapping
223+
return self.__snapshot_mapping or self.context_diff.snapshots
224+
225+
@property
226+
def _dag(self) -> DAG[str]:
227+
if self.__dag is None:
228+
self.__dag = DAG()
229+
for name, snapshot in self._snapshot_mapping.items():
230+
self.__dag.add(name, snapshot.node.depends_on)
231+
return self.__dag
219232

220233
@property
221234
def new_snapshots(self) -> t.List[Snapshot]:
@@ -649,7 +662,7 @@ def _ensure_new_env_with_changes(self) -> None:
649662
def _refresh_dag_and_ignored_snapshots(self) -> None:
650663
self._restatements = {}
651664
(
652-
self._dag,
665+
self.__dag,
653666
self._snapshots,
654667
self._new_snapshots,
655668
self.__snapshot_mapping,
@@ -715,6 +728,12 @@ def _build_snapshots_and_dag(
715728
ignored_snapshot_names,
716729
)
717730

731+
def _has_paused_forward_only(self) -> bool:
732+
for name, snapshot in self._snapshot_mapping.items():
733+
if snapshot.is_paused_forward_only or self._is_forward_only_model(name):
734+
return True
735+
return False
736+
718737
def _is_forward_only_model(self, model_name: str) -> bool:
719738
def _is_forward_only_expected(snapshot: Snapshot) -> bool:
720739
# Returns True if the snapshot is not categorized yet but is expected

tests/core/test_plan.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,9 @@ def test_indirectly_modified_forward_only_model(make_snapshot, mocker: MockerFix
633633
context_diff_mock.previous_plan_id = "previous_plan_id"
634634
context_diff_mock.directly_modified.side_effect = lambda name: name == "a"
635635

636-
plan = Plan(context_diff_mock, is_dev=True)
636+
plan = Plan(context_diff_mock, is_dev=True, default_start="2023-01-01")
637637
assert plan.indirectly_modified == {"a": {"b", "c"}}
638+
assert plan.start == "2023-01-01"
638639

639640
assert len(plan.directly_modified) == 1
640641
assert plan.directly_modified[0].snapshot_id == updated_snapshot_a.snapshot_id
@@ -666,7 +667,8 @@ def test_added_model_with_forward_only_parent(make_snapshot, mocker: MockerFixtu
666667
context_diff_mock.environment = "test_dev"
667668
context_diff_mock.previous_plan_id = "previous_plan_id"
668669

669-
Plan(context_diff_mock)
670+
plan = Plan(context_diff_mock, is_dev=True, default_start="2023-01-01")
671+
assert plan.start == "2023-01-01"
670672
assert snapshot_b.change_category == SnapshotChangeCategory.FORWARD_ONLY
671673

672674

0 commit comments

Comments
 (0)