Skip to content

Commit ccf7c0d

Browse files
authored
Fix: Dont reuse the dev table version for non-materialized models (#3694)
1 parent dde7f45 commit ccf7c0d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

sqlmesh/core/snapshot/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ def categorize_as(self, category: SnapshotChangeCategory) -> None:
926926
previous_version = self.previous_version
927927
self.version = previous_version.data_version.version
928928
self.physical_schema_ = previous_version.physical_schema
929-
if category.is_indirect_non_breaking or category.is_metadata:
929+
if self.is_materialized and (category.is_indirect_non_breaking or category.is_metadata):
930930
# Reuse the dev table for indirect non-breaking changes.
931931
self.temp_version = (
932932
previous_version.data_version.temp_version

tests/core/test_snapshot.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,8 @@ def test_table_name(snapshot: Snapshot, make_snapshot: t.Callable):
952952
assert snapshot.table_name(is_deployable=True) == "sqlmesh__default.name__3078928823"
953953
assert snapshot.table_name(is_deployable=False) == "sqlmesh__default.name__3078928823__temp"
954954

955+
assert not snapshot.temp_version
956+
955957
# Mimic an indirect non-breaking change.
956958
previous_data_version = snapshot.data_version
957959
assert previous_data_version.physical_schema == "sqlmesh__default"
@@ -963,6 +965,7 @@ def test_table_name(snapshot: Snapshot, make_snapshot: t.Callable):
963965
assert snapshot.table_name(is_deployable=True) == "sqlmesh__default.name__3078928823"
964966
# Indirect non-breaking snapshots reuse the dev table as well.
965967
assert snapshot.table_name(is_deployable=False) == "sqlmesh__default.name__3078928823__temp"
968+
assert snapshot.temp_version
966969

967970
# Mimic a direct forward-only change.
968971
snapshot.fingerprint = SnapshotFingerprint(
@@ -993,6 +996,37 @@ def test_table_name(snapshot: Snapshot, make_snapshot: t.Callable):
993996
)
994997

995998

999+
def test_table_name_view(make_snapshot: t.Callable):
1000+
# Mimic a direct breaking change.
1001+
snapshot = make_snapshot(SqlModel(name="name", query=parse_one("select 1"), kind="VIEW"))
1002+
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
1003+
snapshot.previous_versions = ()
1004+
assert snapshot.table_name(is_deployable=True) == f"sqlmesh__default.name__{snapshot.version}"
1005+
assert (
1006+
snapshot.table_name(is_deployable=False)
1007+
== f"sqlmesh__default.name__{snapshot.temp_version_get_or_generate()}__temp"
1008+
)
1009+
1010+
assert not snapshot.temp_version
1011+
1012+
# Mimic an indirect non-breaking change.
1013+
new_snapshot = make_snapshot(SqlModel(name="name", query=parse_one("select 2"), kind="VIEW"))
1014+
previous_data_version = snapshot.data_version
1015+
new_snapshot.previous_versions = (previous_data_version,)
1016+
new_snapshot.categorize_as(SnapshotChangeCategory.INDIRECT_NON_BREAKING)
1017+
assert (
1018+
new_snapshot.table_name(is_deployable=True) == f"sqlmesh__default.name__{snapshot.version}"
1019+
)
1020+
# Indirect non-breaking view snapshots should not reuse the dev table.
1021+
assert (
1022+
new_snapshot.table_name(is_deployable=False)
1023+
== f"sqlmesh__default.name__{new_snapshot.temp_version_get_or_generate()}__temp"
1024+
)
1025+
assert not new_snapshot.temp_version
1026+
assert new_snapshot.version == snapshot.version
1027+
assert new_snapshot.temp_version_get_or_generate() != snapshot.temp_version_get_or_generate()
1028+
1029+
9961030
def test_categorize_change_sql(make_snapshot):
9971031
old_snapshot = make_snapshot(SqlModel(name="a", query=parse_one("select 1, ds")))
9981032

0 commit comments

Comments
 (0)