Skip to content

Commit 3ebed02

Browse files
committed
Fix: Only selected models need to be promoted when --inlclude-unmodified flag is provided
1 parent 15d2cd0 commit 3ebed02

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

sqlmesh/core/plan/definition.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Plan(PydanticModel, frozen=True):
6363
restatements: t.Dict[SnapshotId, Interval]
6464
"""
6565
All models being restated, which are typically the explicitly selected ones + their downstream dependencies.
66-
66+
6767
Note that dev previews are also considered restatements, so :selected_models_to_restate can be empty
6868
while :restatements is still populated with dev previews
6969
"""
@@ -213,8 +213,8 @@ def environment(self) -> Environment:
213213

214214
snapshots_by_name = self.context_diff.snapshots_by_name
215215
snapshots = [s.table_info for s in self.snapshots.values()]
216-
promoted_snapshot_ids = None
217-
if self.is_dev and not self.include_unmodified:
216+
promotable_snapshot_ids = None
217+
if self.is_dev:
218218
if self.selected_models_to_backfill is not None:
219219
# Only promote models that have been explicitly selected for backfill.
220220
promotable_snapshot_ids = {
@@ -225,12 +225,14 @@ def environment(self) -> Environment:
225225
if m in snapshots_by_name
226226
],
227227
}
228-
else:
228+
elif not self.include_unmodified:
229229
promotable_snapshot_ids = self.context_diff.promotable_snapshot_ids.copy()
230230

231-
promoted_snapshot_ids = [
232-
s.snapshot_id for s in snapshots if s.snapshot_id in promotable_snapshot_ids
233-
]
231+
promoted_snapshot_ids = (
232+
[s.snapshot_id for s in snapshots if s.snapshot_id in promotable_snapshot_ids]
233+
if promotable_snapshot_ids is not None
234+
else None
235+
)
234236

235237
previous_finalized_snapshots = (
236238
self.context_diff.environment_snapshots

tests/core/integration/test_plan_options.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,52 @@ def test_create_environment_no_changes_with_selector(init_and_plan_context: t.Ca
476476

477477
schema_objects = context.engine_adapter.get_data_objects("sushi__dev")
478478
assert {o.name for o in schema_objects} == {"top_waiters"}
479+
480+
481+
@time_machine.travel("2023-01-08 15:00:00 UTC")
482+
def test_include_unmodified(init_and_plan_context: t.Callable):
483+
context, plan = init_and_plan_context("examples/sushi")
484+
context.apply(plan)
485+
486+
plan = context.plan_builder(
487+
"dev",
488+
include_unmodified=True,
489+
skip_tests=True,
490+
).build()
491+
492+
all_snapshots = context.snapshots
493+
494+
assert len(plan.environment.snapshots) == len(all_snapshots)
495+
assert len(plan.environment.promoted_snapshot_ids) == len(all_snapshots)
496+
497+
context.apply(plan)
498+
499+
data_objs = context.engine_adapter.get_data_objects("sushi__dev")
500+
assert len(data_objs) == len(all_snapshots)
501+
502+
503+
@time_machine.travel("2023-01-08 15:00:00 UTC")
504+
def test_select_models_with_include_unmodified(init_and_plan_context: t.Callable):
505+
context, plan = init_and_plan_context("examples/sushi")
506+
context.apply(plan)
507+
508+
plan = context.plan_builder(
509+
"dev",
510+
select_models=["*top_waiters", "*customer_revenue_by_day"],
511+
include_unmodified=True,
512+
skip_tests=True,
513+
).build()
514+
515+
assert len(plan.environment.snapshots) == len(context.snapshots)
516+
517+
promoted_set = {s_id.name for s_id in plan.environment.promoted_snapshot_ids}
518+
assert promoted_set == {
519+
'"memory"."sushi"."customer_revenue_by_day"',
520+
'"memory"."sushi"."top_waiters"',
521+
}
522+
523+
context.apply(plan)
524+
525+
data_objs = context.engine_adapter.get_data_objects("sushi__dev")
526+
assert len(data_objs) == 2
527+
assert {o.name for o in data_objs} == {"customer_revenue_by_day", "top_waiters"}

0 commit comments

Comments
 (0)