Skip to content

Commit f234322

Browse files
committed
PR feedback
1 parent 933bd9e commit f234322

File tree

6 files changed

+58
-16
lines changed

6 files changed

+58
-16
lines changed

sqlmesh/core/snapshot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Node as Node,
55
QualifiedViewName as QualifiedViewName,
66
Snapshot as Snapshot,
7-
MinimalSnapshot as MinimalSnapshot,
7+
SnapshotIdAndVersion as SnapshotIdAndVersion,
88
SnapshotChangeCategory as SnapshotChangeCategory,
99
SnapshotDataVersion as SnapshotDataVersion,
1010
SnapshotFingerprint as SnapshotFingerprint,

sqlmesh/core/snapshot/definition.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def name_version(self) -> SnapshotNameVersion:
588588
return SnapshotNameVersion(name=self.name, version=self.version)
589589

590590

591-
class MinimalSnapshot(PydanticModel):
591+
class SnapshotIdAndVersion(PydanticModel):
592592
"""A stripped down version of a snapshot that is used in situations where we want to fetch the main fields of the snapshots table
593593
without the overhead of parsing the full snapshot payload and fetching intervals.
594594
"""
@@ -597,7 +597,7 @@ class MinimalSnapshot(PydanticModel):
597597
version: str
598598
dev_version_: t.Optional[str] = Field(alias="dev_version")
599599
identifier: str
600-
fingerprint: SnapshotFingerprint
600+
fingerprint_: t.Union[str, SnapshotFingerprint] = Field(alias="fingerprint")
601601

602602
@property
603603
def snapshot_id(self) -> SnapshotId:
@@ -607,6 +607,13 @@ def snapshot_id(self) -> SnapshotId:
607607
def name_version(self) -> SnapshotNameVersion:
608608
return SnapshotNameVersion(name=self.name, version=self.version)
609609

610+
@property
611+
def fingerprint(self) -> SnapshotFingerprint:
612+
value = self.fingerprint_
613+
if isinstance(value, str):
614+
self.fingerprint_ = value = SnapshotFingerprint.parse_raw(value)
615+
return value
616+
610617
@property
611618
def dev_version(self) -> str:
612619
return self.dev_version_ or self.fingerprint.to_version()
@@ -1487,9 +1494,11 @@ class SnapshotTableCleanupTask(PydanticModel):
14871494
dev_table_only: bool
14881495

14891496

1490-
SnapshotIdLike = t.Union[SnapshotId, SnapshotTableInfo, MinimalSnapshot, Snapshot]
1497+
SnapshotIdLike = t.Union[SnapshotId, SnapshotTableInfo, SnapshotIdAndVersion, Snapshot]
14911498
SnapshotInfoLike = t.Union[SnapshotTableInfo, Snapshot]
1492-
SnapshotNameVersionLike = t.Union[SnapshotNameVersion, SnapshotTableInfo, MinimalSnapshot, Snapshot]
1499+
SnapshotNameVersionLike = t.Union[
1500+
SnapshotNameVersion, SnapshotTableInfo, SnapshotIdAndVersion, Snapshot
1501+
]
14931502

14941503

14951504
class DeployabilityIndex(PydanticModel, frozen=True):

sqlmesh/core/state_sync/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
SnapshotTableCleanupTask,
2424
SnapshotTableInfo,
2525
SnapshotNameVersion,
26-
MinimalSnapshot,
26+
SnapshotIdAndVersion,
2727
)
2828
from sqlmesh.core.snapshot.definition import Interval, SnapshotIntervals
2929
from sqlmesh.utils import major_minor
@@ -104,7 +104,7 @@ def get_snapshots_by_names(
104104
snapshot_names: t.Iterable[str],
105105
current_ts: t.Optional[int] = None,
106106
exclude_expired: bool = True,
107-
) -> t.Set[MinimalSnapshot]:
107+
) -> t.Set[SnapshotIdAndVersion]:
108108
"""Return the snapshot records for all versions of the specified snapshot names.
109109
110110
Args:

sqlmesh/core/state_sync/db/facade.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from sqlmesh.core.environment import Environment, EnvironmentStatements, EnvironmentSummary
3030
from sqlmesh.core.snapshot import (
3131
Snapshot,
32-
MinimalSnapshot,
32+
SnapshotIdAndVersion,
3333
SnapshotId,
3434
SnapshotIdLike,
3535
SnapshotInfoLike,
@@ -372,7 +372,7 @@ def get_snapshots_by_names(
372372
snapshot_names: t.Iterable[str],
373373
current_ts: t.Optional[int] = None,
374374
exclude_expired: bool = True,
375-
) -> t.Set[MinimalSnapshot]:
375+
) -> t.Set[SnapshotIdAndVersion]:
376376
return self.snapshot_state.get_snapshots_by_names(
377377
snapshot_names=snapshot_names, current_ts=current_ts, exclude_expired=exclude_expired
378378
)

sqlmesh/core/state_sync/db/snapshot.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
SnapshotNameVersion,
2727
SnapshotInfoLike,
2828
Snapshot,
29-
MinimalSnapshot,
29+
SnapshotIdAndVersion,
3030
SnapshotId,
3131
SnapshotFingerprint,
3232
)
@@ -214,7 +214,7 @@ def _get_expired_snapshots(
214214
for snapshot in environment.snapshots
215215
}
216216

217-
def _is_snapshot_used(snapshot: MinimalSnapshot) -> bool:
217+
def _is_snapshot_used(snapshot: SnapshotIdAndVersion) -> bool:
218218
return (
219219
snapshot.snapshot_id in promoted_snapshot_ids
220220
or snapshot.snapshot_id not in expired_candidates
@@ -312,7 +312,7 @@ def get_snapshots_by_names(
312312
snapshot_names: t.Iterable[str],
313313
current_ts: t.Optional[int] = None,
314314
exclude_expired: bool = True,
315-
) -> t.Set[MinimalSnapshot]:
315+
) -> t.Set[SnapshotIdAndVersion]:
316316
"""Return the snapshot records for all versions of the specified snapshot names.
317317
318318
Args:
@@ -333,12 +333,12 @@ def get_snapshots_by_names(
333333
unexpired_expr = None
334334

335335
return {
336-
MinimalSnapshot(
336+
SnapshotIdAndVersion(
337337
name=name,
338338
identifier=identifier,
339339
version=version,
340340
dev_version=dev_version,
341-
fingerprint=SnapshotFingerprint.parse_raw(fingerprint),
341+
fingerprint=fingerprint,
342342
)
343343
for where in snapshot_name_filter(
344344
snapshot_names=snapshot_names,
@@ -636,7 +636,7 @@ def _get_snapshots_with_same_version(
636636
self,
637637
snapshots: t.Collection[SnapshotNameVersionLike],
638638
lock_for_update: bool = False,
639-
) -> t.List[MinimalSnapshot]:
639+
) -> t.List[SnapshotIdAndVersion]:
640640
"""Fetches all snapshots that share the same version as the snapshots.
641641
642642
The output includes the snapshots with the specified identifiers.
@@ -673,7 +673,7 @@ def _get_snapshots_with_same_version(
673673
snapshot_rows.extend(fetchall(self.engine_adapter, query))
674674

675675
return [
676-
MinimalSnapshot(
676+
SnapshotIdAndVersion(
677677
name=name,
678678
identifier=identifier,
679679
version=version,

tests/core/test_snapshot.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
QualifiedViewName,
4545
Snapshot,
4646
SnapshotId,
47+
SnapshotIdAndVersion,
4748
SnapshotChangeCategory,
4849
SnapshotFingerprint,
4950
SnapshotIntervals,
@@ -3532,3 +3533,35 @@ def test_table_name_virtual_environment_mode(
35323533
assert table_name_result.endswith(snapshot.version)
35333534
else:
35343535
assert table_name_result.endswith(f"{snapshot.dev_version}__dev")
3536+
3537+
3538+
def test_snapshot_id_and_version_fingerprint_lazy_init():
3539+
snapshot = SnapshotIdAndVersion(
3540+
name="a",
3541+
identifier="1234",
3542+
version="2345",
3543+
dev_version=None,
3544+
fingerprint='{"data_hash":"1","metadata_hash":"2","parent_data_hash":"3","parent_metadata_hash":"4"}',
3545+
)
3546+
3547+
# starts off as a string in the private property
3548+
assert isinstance(snapshot.fingerprint_, str)
3549+
3550+
# gets parsed into SnapshotFingerprint on first access of public property
3551+
fingerprint = snapshot.fingerprint
3552+
assert isinstance(fingerprint, SnapshotFingerprint)
3553+
assert isinstance(snapshot.fingerprint_, SnapshotFingerprint)
3554+
3555+
assert fingerprint.data_hash == "1"
3556+
assert fingerprint.metadata_hash == "2"
3557+
assert fingerprint.parent_data_hash == "3"
3558+
assert fingerprint.parent_metadata_hash == "4"
3559+
assert snapshot.dev_version is not None # dev version uses fingerprint
3560+
3561+
# can also be supplied as a SnapshotFingerprint to begin with instead of a str
3562+
snapshot = SnapshotIdAndVersion(
3563+
name="a", identifier="1234", version="2345", dev_version=None, fingerprint=fingerprint
3564+
)
3565+
3566+
assert isinstance(snapshot.fingerprint_, SnapshotFingerprint)
3567+
assert snapshot.fingerprint == fingerprint

0 commit comments

Comments
 (0)