Skip to content

Commit 9839223

Browse files
authored
fix: Plan start date inference when targeting the prod environment (#605)
1 parent 90b5a08 commit 9839223

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

sqlmesh/core/plan/definition.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,14 @@ def uncategorized(self) -> t.List[Snapshot]:
129129
@property
130130
def start(self) -> TimeLike:
131131
"""Returns the start of the plan or the earliest date of all snapshots."""
132-
return self._start or scheduler.earliest_start_date(
133-
snapshot
134-
for snapshot in self.snapshots
135-
if snapshot.version_get_or_generate() in self._missing_intervals
132+
return self._start or (
133+
min(
134+
start
135+
for intervals_per_model in self._missing_intervals.values()
136+
for start, _ in intervals_per_model
137+
)
138+
if self._missing_intervals
139+
else yesterday_ds()
136140
)
137141

138142
@start.setter

tests/core/test_plan.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
SnapshotFingerprint,
1313
)
1414
from sqlmesh.utils.dag import DAG
15-
from sqlmesh.utils.date import to_datetime
15+
from sqlmesh.utils.date import to_datetime, to_timestamp
1616
from sqlmesh.utils.errors import PlanError
1717

1818

@@ -326,3 +326,32 @@ def test_forward_only_plan_seed_models(make_snapshot, mocker: MockerFixture):
326326
Plan(context_diff_mock, dag, state_reader_mock, forward_only=True)
327327
assert snapshot_a_updated.version == snapshot_a_updated.fingerprint.to_version()
328328
assert snapshot_a_updated.change_category == SnapshotChangeCategory.NON_BREAKING
329+
330+
331+
def test_start_inference(make_snapshot, mocker: MockerFixture):
332+
snapshot_a = make_snapshot(
333+
SqlModel(name="a", query=parse_one("select 1, ds"), start="2022-01-01")
334+
)
335+
snapshot_a.set_version()
336+
337+
snapshot_b = make_snapshot(SqlModel(name="b", query=parse_one("select 2, ds")))
338+
snapshot_b.set_version()
339+
340+
dag = DAG[str]({"a": set(), "b": set()})
341+
342+
context_diff_mock = mocker.Mock()
343+
context_diff_mock.snapshots = {"a": snapshot_a, "b": snapshot_b}
344+
context_diff_mock.added = set()
345+
context_diff_mock.modified_snapshots = {}
346+
context_diff_mock.new_snapshots = {snapshot_b.snapshot_id: snapshot_b}
347+
348+
state_reader_mock = mocker.Mock()
349+
state_reader_mock.missing_intervals.return_value = {
350+
snapshot_b: [(to_timestamp("2022-01-01"), to_timestamp("2023-01-01"))]
351+
}
352+
353+
plan = Plan(context_diff_mock, dag, state_reader_mock)
354+
assert len(plan._missing_intervals) == 1
355+
assert snapshot_b.version_get_or_generate() in plan._missing_intervals
356+
357+
assert plan.start == to_timestamp("2022-01-01")

0 commit comments

Comments
 (0)