Skip to content

Commit a7ceedd

Browse files
authored
Fix: Allow reverting to previous versions (#1163)
* allow reverting to previous versions * use is_new_snapshot for check
1 parent ea7b377 commit a7ceedd

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

sqlmesh/core/plan/definition.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ def set_choice(self, snapshot: Snapshot, choice: SnapshotChangeCategory) -> None
337337

338338
for child in self.indirectly_modified[snapshot.name]:
339339
child_snapshot = self.context_diff.snapshots[child]
340+
# If the snapshot isn't new then we are reverting to a previously existing snapshot
341+
# and therefore we don't want to recategorize it.
342+
if not self.is_new_snapshot(child_snapshot):
343+
continue
340344

341345
if choice in (
342346
SnapshotChangeCategory.BREAKING,

tests/core/test_plan.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,39 @@ def test_disable_restatement(make_snapshot, mocker: MockerFixture):
623623
# Restatements should still be supported when in dev.
624624
plan = Plan(context_diff_mock, is_dev=True, restate_models=["a"])
625625
assert plan.restatements == {"a"}
626+
627+
628+
def test_revert_to_previous_value(make_snapshot, mocker: MockerFixture):
629+
"""
630+
Make sure we can revert to previous snapshots with intervals if it already exists and not modify
631+
it's existing change category
632+
"""
633+
old_snapshot_a = make_snapshot(
634+
SqlModel(name="a", query=parse_one("select 1, ds"), depends_on={})
635+
)
636+
old_snapshot_b = make_snapshot(
637+
SqlModel(name="b", query=parse_one("select 1, ds FROM a"), depends_on={"a"})
638+
)
639+
snapshot_a = make_snapshot(SqlModel(name="a", query=parse_one("select 2, ds"), depends_on={}))
640+
snapshot_b = make_snapshot(
641+
SqlModel(name="b", query=parse_one("select 1, ds FROM a"), depends_on={"a"})
642+
)
643+
snapshot_b.categorize_as(SnapshotChangeCategory.FORWARD_ONLY)
644+
snapshot_b.add_interval("2022-01-01", now())
645+
646+
context_diff_mock = mocker.Mock()
647+
context_diff_mock.snapshots = {"a": snapshot_a, "b": snapshot_b}
648+
context_diff_mock.added = set()
649+
context_diff_mock.removed = set()
650+
context_diff_mock.directly_modified.side_effect = lambda x: x == "a"
651+
context_diff_mock.modified_snapshots = {
652+
"a": (snapshot_a, old_snapshot_a),
653+
"b": (snapshot_b, old_snapshot_b),
654+
}
655+
context_diff_mock.new_snapshots = {snapshot_a.snapshot_id: snapshot_a}
656+
context_diff_mock.added_materialized_models = set()
657+
658+
plan = Plan(context_diff_mock)
659+
plan.set_choice(snapshot_a, SnapshotChangeCategory.BREAKING)
660+
# Make sure it does not get assigned INDIRECT_BREAKING
661+
assert snapshot_b.change_category == SnapshotChangeCategory.FORWARD_ONLY

0 commit comments

Comments
 (0)