Skip to content

Commit 537cc67

Browse files
authored
Fix: Only set effective_from for evaluatable models (#3153)
1 parent c742bcb commit 537cc67

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

sqlmesh/core/model/kind.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ def supports_python_models(self) -> bool:
797797

798798
class EmbeddedKind(_ModelKind):
799799
name: Literal[ModelKindName.EMBEDDED] = ModelKindName.EMBEDDED
800+
disable_restatement: t.Literal[True] = True
800801

801802
@property
802803
def supports_python_models(self) -> bool:
@@ -805,6 +806,7 @@ def supports_python_models(self) -> bool:
805806

806807
class ExternalKind(_ModelKind):
807808
name: Literal[ModelKindName.EXTERNAL] = ModelKindName.EXTERNAL
809+
disable_restatement: t.Literal[True] = True
808810

809811

810812
class CustomKind(_ModelKind):

sqlmesh/core/plan/builder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,11 @@ def _apply_effective_from(self) -> None:
600600
raise PlanError("Effective date cannot be in the future.")
601601

602602
for snapshot in self._context_diff.new_snapshots.values():
603-
if not snapshot.disable_restatement and not snapshot.full_history_restatement_only:
603+
if (
604+
snapshot.evaluatable
605+
and not snapshot.disable_restatement
606+
and not snapshot.full_history_restatement_only
607+
):
604608
snapshot.effective_from = self._effective_from
605609

606610
def _is_forward_only_change(self, s_id: SnapshotId) -> bool:

sqlmesh/core/state_sync/engine_adapter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ def unpause_snapshots(
430430
unpaused_snapshots[snapshot.unpaused_ts].append(snapshot.snapshot_id)
431431
elif not is_target_snapshot:
432432
target_snapshot = target_snapshots_by_version[(snapshot.name, snapshot.version)]
433-
if target_snapshot.normalized_effective_from_ts:
433+
if (
434+
target_snapshot.normalized_effective_from_ts
435+
and not target_snapshot.disable_restatement
436+
):
434437
# Making sure that there are no overlapping intervals.
435438
effective_from_ts = target_snapshot.normalized_effective_from_ts
436439
logger.info(

tests/core/test_plan.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,60 @@ def test_effective_from(make_snapshot, mocker: MockerFixture):
12901290
assert updated_snapshot.effective_from is None
12911291

12921292

1293+
def test_effective_from_non_evaluatble_model(make_snapshot, mocker: MockerFixture):
1294+
snapshot = make_snapshot(
1295+
SqlModel(
1296+
name="a",
1297+
kind="EMBEDDED",
1298+
query=parse_one("select 1, ds FROM b"),
1299+
start="2023-01-01",
1300+
dialect="duckdb",
1301+
)
1302+
)
1303+
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
1304+
1305+
updated_snapshot = make_snapshot(
1306+
SqlModel(
1307+
name="a",
1308+
kind="EMBEDDED",
1309+
query=parse_one("select 2, ds FROM b"),
1310+
start="2023-01-01",
1311+
dialect="duckdb",
1312+
)
1313+
)
1314+
updated_snapshot.previous_versions = snapshot.all_versions
1315+
1316+
context_diff = ContextDiff(
1317+
environment="test_environment",
1318+
is_new_environment=True,
1319+
is_unfinalized_environment=False,
1320+
normalize_environment_name=True,
1321+
create_from="prod",
1322+
added=set(),
1323+
removed_snapshots={},
1324+
modified_snapshots={updated_snapshot.name: (updated_snapshot, snapshot)},
1325+
snapshots={updated_snapshot.snapshot_id: updated_snapshot},
1326+
new_snapshots={updated_snapshot.snapshot_id: updated_snapshot},
1327+
previous_plan_id=None,
1328+
previously_promoted_snapshot_ids=set(),
1329+
previous_finalized_snapshots=None,
1330+
)
1331+
1332+
schema_differ = DuckDBEngineAdapter.SCHEMA_DIFFER
1333+
plan_builder = PlanBuilder(
1334+
context_diff,
1335+
schema_differ,
1336+
forward_only=True,
1337+
start="2023-01-01",
1338+
end="2023-03-01",
1339+
is_dev=True,
1340+
)
1341+
1342+
plan_builder.set_effective_from("2023-02-01")
1343+
assert plan_builder.build().effective_from == "2023-02-01"
1344+
assert not updated_snapshot.effective_from
1345+
1346+
12931347
def test_new_environment_no_changes(make_snapshot, mocker: MockerFixture):
12941348
snapshot = make_snapshot(SqlModel(name="a", query=parse_one("select 1, ds")))
12951349
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)

0 commit comments

Comments
 (0)