sqlmesh.core.snapshot.categorizer
1from __future__ import annotations 2 3import typing as t 4 5from sqlmesh.core.config import AutoCategorizationMode, CategorizerConfig 6from sqlmesh.core.snapshot.definition import Snapshot, SnapshotChangeCategory 7from sqlmesh.utils.errors import SQLMeshError 8 9 10def categorize_change( 11 new: Snapshot, old: Snapshot, config: t.Optional[CategorizerConfig] = None 12) -> t.Optional[SnapshotChangeCategory]: 13 """Attempts to automatically categorize a change between two snapshots. 14 15 Presently the implementation only returns the NON_BREAKING category iff 16 a new projections have been added to one or more SELECT statement(s). In 17 all other cases None is returned. 18 19 Args: 20 new: The new snapshot. 21 old: The old snapshot. 22 23 Returns: 24 The change category or None if the category can't be determined automatically. 25 26 """ 27 old_model = old.model 28 new_model = new.model 29 30 config = config or CategorizerConfig() 31 mode = config.dict().get(new_model.source_type, AutoCategorizationMode.OFF) 32 if mode == AutoCategorizationMode.OFF: 33 return None 34 35 default_category = ( 36 SnapshotChangeCategory.BREAKING if mode == AutoCategorizationMode.FULL else None 37 ) 38 39 if type(new_model) != type(old_model): 40 return default_category 41 42 if new.fingerprint.data_hash == old.fingerprint.data_hash: 43 if new.fingerprint.metadata_hash == old.fingerprint.metadata_hash: 44 raise SQLMeshError( 45 f"{new} is unmodified or indirectly modified and should not be categorized" 46 ) 47 if new.fingerprint.parent_data_hash == old.fingerprint.parent_data_hash: 48 return SnapshotChangeCategory.NON_BREAKING 49 return None 50 51 is_breaking_change = new_model.is_breaking_change(old_model) 52 if is_breaking_change is None: 53 return default_category 54 return ( 55 SnapshotChangeCategory.BREAKING 56 if is_breaking_change 57 else SnapshotChangeCategory.NON_BREAKING 58 )
def
categorize_change( new: sqlmesh.core.snapshot.definition.Snapshot, old: sqlmesh.core.snapshot.definition.Snapshot, config: Optional[sqlmesh.core.config.categorizer.CategorizerConfig] = None) -> Optional[sqlmesh.core.snapshot.definition.SnapshotChangeCategory]:
11def categorize_change( 12 new: Snapshot, old: Snapshot, config: t.Optional[CategorizerConfig] = None 13) -> t.Optional[SnapshotChangeCategory]: 14 """Attempts to automatically categorize a change between two snapshots. 15 16 Presently the implementation only returns the NON_BREAKING category iff 17 a new projections have been added to one or more SELECT statement(s). In 18 all other cases None is returned. 19 20 Args: 21 new: The new snapshot. 22 old: The old snapshot. 23 24 Returns: 25 The change category or None if the category can't be determined automatically. 26 27 """ 28 old_model = old.model 29 new_model = new.model 30 31 config = config or CategorizerConfig() 32 mode = config.dict().get(new_model.source_type, AutoCategorizationMode.OFF) 33 if mode == AutoCategorizationMode.OFF: 34 return None 35 36 default_category = ( 37 SnapshotChangeCategory.BREAKING if mode == AutoCategorizationMode.FULL else None 38 ) 39 40 if type(new_model) != type(old_model): 41 return default_category 42 43 if new.fingerprint.data_hash == old.fingerprint.data_hash: 44 if new.fingerprint.metadata_hash == old.fingerprint.metadata_hash: 45 raise SQLMeshError( 46 f"{new} is unmodified or indirectly modified and should not be categorized" 47 ) 48 if new.fingerprint.parent_data_hash == old.fingerprint.parent_data_hash: 49 return SnapshotChangeCategory.NON_BREAKING 50 return None 51 52 is_breaking_change = new_model.is_breaking_change(old_model) 53 if is_breaking_change is None: 54 return default_category 55 return ( 56 SnapshotChangeCategory.BREAKING 57 if is_breaking_change 58 else SnapshotChangeCategory.NON_BREAKING 59 )
Attempts to automatically categorize a change between two snapshots.
Presently the implementation only returns the NON_BREAKING category iff a new projections have been added to one or more SELECT statement(s). In all other cases None is returned.
Arguments:
- new: The new snapshot.
- old: The old snapshot.
Returns:
The change category or None if the category can't be determined automatically.