77from datetime import datetime
88from enum import IntEnum
99
10- from pydantic import validator
10+ from pydantic import Field , validator
1111from sqlglot import exp
1212from sqlglot .helper import seq_get
1313
@@ -132,10 +132,17 @@ class SnapshotDataVersion(PydanticModel, frozen=True):
132132 version : str
133133 temp_version : t .Optional [str ]
134134 change_category : t .Optional [SnapshotChangeCategory ]
135+ physical_schema_ : t .Optional [str ] = Field (default = None , alias = "physical_schema" )
135136
136137 def snapshot_id (self , name : str ) -> SnapshotId :
137138 return SnapshotId (name = name , identifier = self .fingerprint .to_identifier ())
138139
140+ @property
141+ def physical_schema (self ) -> str :
142+ # The physical schema here is optional to maintain backwards compatibility with
143+ # records stored by previous versions of SQLMesh.
144+ return self .physical_schema_ or c .SQLMESH
145+
139146 @property
140147 def data_version (self ) -> SnapshotDataVersion :
141148 return self
@@ -163,7 +170,7 @@ def for_environment(self, environment: str) -> str:
163170 )
164171
165172 def schema_for_environment (self , environment : str ) -> str :
166- schema = self .schema_name or "default"
173+ schema = self .schema_name or c . DEFAULT_SCHEMA
167174 if environment .lower () != c .PROD :
168175 schema = f"{ schema } __{ environment } "
169176 return schema
@@ -174,7 +181,6 @@ class SnapshotInfoMixin(ModelKindMixin):
174181 temp_version : t .Optional [str ]
175182 change_category : t .Optional [SnapshotChangeCategory ]
176183 fingerprint : SnapshotFingerprint
177- physical_schema : str
178184 previous_versions : t .Tuple [SnapshotDataVersion , ...] = ()
179185
180186 def is_temporary_table (self , is_dev : bool ) -> bool :
@@ -193,7 +199,7 @@ def snapshot_id(self) -> SnapshotId:
193199
194200 @property
195201 def qualified_view_name (self ) -> QualifiedViewName :
196- ( catalog , schema , table ) = parse_model_name (self .name )
202+ catalog , schema , table = parse_model_name (self .name )
197203 return QualifiedViewName (catalog = catalog , schema_name = schema , table = table )
198204
199205 @property
@@ -203,6 +209,10 @@ def previous_version(self) -> t.Optional[SnapshotDataVersion]:
203209 return self .previous_versions [- 1 ]
204210 return None
205211
212+ @property
213+ def physical_schema (self ) -> str :
214+ raise NotImplementedError
215+
206216 @property
207217 def data_version (self ) -> SnapshotDataVersion :
208218 raise NotImplementedError
@@ -262,7 +272,7 @@ class SnapshotTableInfo(PydanticModel, SnapshotInfoMixin, frozen=True):
262272 fingerprint : SnapshotFingerprint
263273 version : str
264274 temp_version : t .Optional [str ]
265- physical_schema : str
275+ physical_schema_ : str = Field ( alias = "physical_schema" )
266276 parents : t .Tuple [SnapshotId , ...]
267277 previous_versions : t .Tuple [SnapshotDataVersion , ...] = ()
268278 change_category : t .Optional [SnapshotChangeCategory ]
@@ -277,6 +287,10 @@ def table_name(self, is_dev: bool = False, for_read: bool = False) -> str:
277287 """
278288 return self ._table_name (self .version , is_dev , for_read )
279289
290+ @property
291+ def physical_schema (self ) -> str :
292+ return self .physical_schema_
293+
280294 @property
281295 def table_info (self ) -> SnapshotTableInfo :
282296 """Helper method to return self."""
@@ -289,6 +303,7 @@ def data_version(self) -> SnapshotDataVersion:
289303 version = self .version ,
290304 temp_version = self .temp_version ,
291305 change_category = self .change_category ,
306+ physical_schema = self .physical_schema ,
292307 )
293308
294309 @property
@@ -342,7 +357,7 @@ class Snapshot(PydanticModel, SnapshotInfoMixin):
342357
343358 name : str
344359 fingerprint : SnapshotFingerprint
345- physical_schema : str
360+ physical_schema_ : t . Optional [ str ] = Field ( default = None , alias = "physical_schema" )
346361 model : Model
347362 parents : t .Tuple [SnapshotId , ...]
348363 audits : t .Tuple [Audit , ...]
@@ -442,7 +457,6 @@ def from_model(
442457 model : Model ,
443458 * ,
444459 models : t .Dict [str , Model ],
445- physical_schema : str = c .SQLMESH ,
446460 ttl : str = c .DEFAULT_SNAPSHOT_TTL ,
447461 project : str = "" ,
448462 version : t .Optional [str ] = None ,
@@ -472,19 +486,16 @@ def from_model(
472486 name = model .name ,
473487 fingerprint = fingerprint_from_model (
474488 model ,
475- physical_schema = physical_schema ,
476489 models = models ,
477490 audits = audits ,
478491 cache = cache ,
479492 ),
480- physical_schema = physical_schema ,
481493 model = model ,
482494 parents = tuple (
483495 SnapshotId (
484496 name = name ,
485497 identifier = fingerprint_from_model (
486498 models [name ],
487- physical_schema = physical_schema ,
488499 models = models ,
489500 audits = audits ,
490501 cache = cache ,
@@ -672,6 +683,7 @@ def categorize_as(self, category: SnapshotChangeCategory) -> None:
672683 )
673684 if is_forward_only and self .previous_version :
674685 self .version = self .previous_version .data_version .version
686+ self .physical_schema_ = self .previous_version .physical_schema
675687 else :
676688 self .version = self .fingerprint .to_version ()
677689
@@ -716,6 +728,15 @@ def version_get_or_generate(self) -> str:
716728 """Helper method to get the version or generate it from the fingerprint."""
717729 return self .version or self .fingerprint .to_version ()
718730
731+ @property
732+ def physical_schema (self ) -> str :
733+ if self .physical_schema_ is not None :
734+ return self .physical_schema_
735+ _ , schema , _ = parse_model_name (self .name )
736+ if schema is None :
737+ schema = c .DEFAULT_SCHEMA
738+ return f"{ c .SQLMESH } __{ schema } "
739+
719740 @property
720741 def table_info (self ) -> SnapshotTableInfo :
721742 """Helper method to get the SnapshotTableInfo from the Snapshot."""
@@ -740,6 +761,7 @@ def data_version(self) -> SnapshotDataVersion:
740761 version = self .version ,
741762 temp_version = self .temp_version ,
742763 change_category = self .change_category ,
764+ physical_schema = self .physical_schema ,
743765 )
744766
745767 @property
@@ -803,7 +825,6 @@ def fingerprint_from_model(
803825 model : Model ,
804826 * ,
805827 models : t .Dict [str , Model ],
806- physical_schema : str = "" ,
807828 audits : t .Optional [t .Dict [str , Audit ]] = None ,
808829 cache : t .Optional [t .Dict [str , SnapshotFingerprint ]] = None ,
809830) -> SnapshotFingerprint :
@@ -815,7 +836,6 @@ def fingerprint_from_model(
815836
816837 Args:
817838 model: Model to fingerprint.
818- physical_schema: The physical_schema of the snapshot which represents where it is stored.
819839 models: Dictionary of all models in the graph to make the fingerprint dependent on parent changes.
820840 If no dictionary is passed in the fingerprint will not be dependent on a model's parents.
821841 audits: Available audits by name.
@@ -831,7 +851,6 @@ def fingerprint_from_model(
831851 fingerprint_from_model (
832852 models [table ],
833853 models = models ,
834- physical_schema = physical_schema ,
835854 audits = audits ,
836855 cache = cache ,
837856 )
@@ -846,7 +865,7 @@ def fingerprint_from_model(
846865 )
847866
848867 cache [model .name ] = SnapshotFingerprint (
849- data_hash = _model_data_hash (model , physical_schema ),
868+ data_hash = _model_data_hash (model ),
850869 metadata_hash = _model_metadata_hash (model , audits or {}),
851870 parent_data_hash = parent_data_hash ,
852871 parent_metadata_hash = parent_metadata_hash ,
@@ -855,7 +874,7 @@ def fingerprint_from_model(
855874 return cache [model .name ]
856875
857876
858- def _model_data_hash (model : Model , physical_schema : str ) -> str :
877+ def _model_data_hash (model : Model ) -> str :
859878 def serialize_hooks (hooks : t .List [HookCall ]) -> t .Iterable [str ]:
860879 serialized = []
861880 for hook in hooks :
@@ -875,7 +894,6 @@ def serialize_hooks(hooks: t.List[HookCall]) -> t.Iterable[str]:
875894 model .cron ,
876895 model .storage_format ,
877896 str (model .lookback ),
878- physical_schema ,
879897 * (model .partitioned_by or []),
880898 * (expression .sql (comments = False ) for expression in model .expressions or []),
881899 * serialize_hooks (model .pre ),
0 commit comments