Skip to content

Commit 119b623

Browse files
authored
Fix: Only selected models need to be promoted when --inlclude-unmodified flag is provided (#5561)
1 parent 15d2cd0 commit 119b623

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,54 @@ 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 plan.environment.promoted_snapshot_ids is None
496+
497+
context.apply(plan)
498+
499+
data_objs = context.engine_adapter.get_data_objects("sushi__dev")
500+
assert len(data_objs) == len(
501+
[s for s in all_snapshots.values() if s.is_model and not s.is_symbolic]
502+
)
503+
504+
505+
@time_machine.travel("2023-01-08 15:00:00 UTC")
506+
def test_select_models_with_include_unmodified(init_and_plan_context: t.Callable):
507+
context, plan = init_and_plan_context("examples/sushi")
508+
context.apply(plan)
509+
510+
plan = context.plan_builder(
511+
"dev",
512+
select_models=["*top_waiters", "*customer_revenue_by_day"],
513+
include_unmodified=True,
514+
skip_tests=True,
515+
).build()
516+
517+
assert len(plan.environment.snapshots) == len(context.snapshots)
518+
519+
promoted_set = {s_id.name for s_id in plan.environment.promoted_snapshot_ids}
520+
assert promoted_set == {
521+
'"memory"."sushi"."customer_revenue_by_day"',
522+
'"memory"."sushi"."top_waiters"',
523+
}
524+
525+
context.apply(plan)
526+
527+
data_objs = context.engine_adapter.get_data_objects("sushi__dev")
528+
assert len(data_objs) == 2
529+
assert {o.name for o in data_objs} == {"customer_revenue_by_day", "top_waiters"}

0 commit comments

Comments
 (0)