Buckets:

download
raw
55.7 kB
"""Schema diffing and compatibility checking. Implements JSON Schema diffing with compatibility modes borrowed from Kafka Schema Registry (backward, forward, full, none). """ import copy from enum import StrEnum from typing import Any from pydantic import BaseModel, ConfigDict, Field from tessera.models.enums import ( ChangeType, CompatibilityMode, GuaranteeChangeSeverity, GuaranteeMode, ) class ChangeKind(StrEnum): """Types of schema changes.""" PROPERTY_ADDED = "property_added" PROPERTY_REMOVED = "property_removed" PROPERTY_RENAMED = "property_renamed" # Detected heuristically TYPE_CHANGED = "type_changed" TYPE_WIDENED = "type_widened" # e.g., int -> number TYPE_NARROWED = "type_narrowed" # e.g., number -> int REQUIRED_ADDED = "required_added" REQUIRED_REMOVED = "required_removed" ENUM_VALUES_ADDED = "enum_values_added" ENUM_VALUES_REMOVED = "enum_values_removed" CONSTRAINT_TIGHTENED = "constraint_tightened" # e.g., maxLength decreased CONSTRAINT_RELAXED = "constraint_relaxed" # e.g., maxLength increased DEFAULT_ADDED = "default_added" DEFAULT_REMOVED = "default_removed" DEFAULT_CHANGED = "default_changed" NULLABLE_ADDED = "nullable_added" NULLABLE_REMOVED = "nullable_removed" # Which changes are breaking under each compatibility mode BACKWARD_BREAKING = { ChangeKind.PROPERTY_REMOVED, ChangeKind.PROPERTY_RENAMED, ChangeKind.TYPE_CHANGED, ChangeKind.TYPE_NARROWED, ChangeKind.REQUIRED_ADDED, ChangeKind.ENUM_VALUES_REMOVED, ChangeKind.CONSTRAINT_TIGHTENED, ChangeKind.DEFAULT_REMOVED, ChangeKind.NULLABLE_REMOVED, } FORWARD_BREAKING = { ChangeKind.PROPERTY_ADDED, ChangeKind.PROPERTY_RENAMED, ChangeKind.TYPE_CHANGED, ChangeKind.TYPE_WIDENED, ChangeKind.REQUIRED_REMOVED, ChangeKind.ENUM_VALUES_ADDED, ChangeKind.CONSTRAINT_RELAXED, ChangeKind.DEFAULT_ADDED, ChangeKind.NULLABLE_ADDED, } # Full compatibility = intersection of backward and forward breaking FULL_BREAKING = BACKWARD_BREAKING | FORWARD_BREAKING class BreakingChange(BaseModel): """A single breaking change detected in a schema diff.""" model_config = ConfigDict(frozen=True) kind: ChangeKind path: str = Field( ..., description="JSON path to the affected element (e.g., 'properties.email')" ) message: str old_value: bool | int | float | str | list[Any] | dict[str, Any] | None = None new_value: bool | int | float | str | list[Any] | dict[str, Any] | None = None def to_dict(self) -> dict[str, Any]: """Convert to dictionary for JSON serialization. Note: Prefer using model_dump() directly. This method exists for backward compatibility. """ return { "type": str(self.kind), "path": self.path, "message": self.message, "old_value": self.old_value, "new_value": self.new_value, } class SchemaDiffResult(BaseModel): """Result of comparing two schemas.""" model_config = ConfigDict(frozen=True) changes: list[BreakingChange] = Field(default_factory=list) change_type: ChangeType = ChangeType.PATCH @property def has_changes(self) -> bool: return len(self.changes) > 0 def breaking_for_mode(self, mode: CompatibilityMode) -> list[BreakingChange]: """Return only changes that are breaking under the given mode.""" if mode == CompatibilityMode.NONE: return [] breaking_kinds = { CompatibilityMode.BACKWARD: BACKWARD_BREAKING, CompatibilityMode.FORWARD: FORWARD_BREAKING, CompatibilityMode.FULL: FULL_BREAKING, }[mode] return [c for c in self.changes if c.kind in breaking_kinds] def is_compatible(self, mode: CompatibilityMode) -> bool: """Check if the schema change is compatible under the given mode.""" return len(self.breaking_for_mode(mode)) == 0 def resolve_refs(schema: dict[str, Any], max_depth: int = 50) -> dict[str, Any]: """Resolve all $ref pointers in a JSON Schema. This function dereferences all $ref pointers within a schema to enable accurate schema comparison. Without this, two semantically identical schemas with different $ref usage patterns would appear different. Only resolves internal references (within the same schema document). External references (URLs, file paths) are left as-is. Args: schema: The JSON Schema to resolve. max_depth: Maximum recursion depth to prevent infinite loops. Returns: A new schema with all internal $ref pointers resolved. """ if not isinstance(schema, dict): return schema # Build a reference map from $defs / definitions ref_map: dict[str, dict[str, Any]] = {} for defs_key in ("$defs", "definitions"): if defs_key in schema and isinstance(schema[defs_key], dict): for name, definition in schema[defs_key].items(): ref_map[f"#/{defs_key}/{name}"] = definition def _resolve(obj: Any, depth: int, seen_refs: set[str]) -> Any: """Recursively resolve $ref pointers.""" if depth > max_depth: return obj if isinstance(obj, dict): # Handle $ref if "$ref" in obj: ref = obj["$ref"] # Only resolve internal refs (starting with #) if ref.startswith("#"): # Prevent infinite recursion for circular refs if ref in seen_refs: return obj # Keep the $ref as-is for circular references if ref in ref_map: new_seen = seen_refs | {ref} # Merge any additional properties with the resolved ref resolved = _resolve(ref_map[ref], depth + 1, new_seen) # If the $ref object has additional properties, merge them additional = {k: v for k, v in obj.items() if k != "$ref"} if additional: if isinstance(resolved, dict): merged = copy.deepcopy(resolved) else: merged = resolved if isinstance(merged, dict): merged.update(additional) return merged return resolved # External refs or unresolved refs - keep as-is return obj # Recursively resolve all values return {k: _resolve(v, depth + 1, seen_refs) for k, v in obj.items()} if isinstance(obj, list): return [_resolve(item, depth + 1, seen_refs) for item in obj] return obj result = _resolve(schema, 0, set()) # The result for a dict input should always be a dict if isinstance(result, dict): return result # Fallback (should not happen for valid schema input) return schema def _list_diff(old_vals: list[Any], new_vals: list[Any]) -> tuple[list[Any], list[Any]]: """Return (added, removed) between two value lists. Uses set operations when all values are hashable; falls back to O(n²) linear scan when values contain dicts or nested lists (valid in JSON Schema). """ try: old_set = set(old_vals) # type: ignore[arg-type] new_set = set(new_vals) # type: ignore[arg-type] return list(new_set - old_set), list(old_set - new_set) except TypeError: added = [v for v in new_vals if v not in old_vals] removed = [v for v in old_vals if v not in new_vals] return added, removed class SchemaDiff: """Compares two JSON schemas and identifies changes.""" # Type hierarchy for widening/narrowing detection TYPE_HIERARCHY = { "null": 0, "boolean": 1, "integer": 2, "number": 3, # number includes integer "string": 4, "array": 5, "object": 6, } # Types that can be widened to other types TYPE_WIDENING = { ("integer", "number"), # int -> number is widening } # Maximum recursion depth to prevent stack overflow from circular schemas MAX_DEPTH = 50 def __init__(self, old_schema: dict[str, Any], new_schema: dict[str, Any]): self.old = old_schema self.new = new_schema self.changes: list[BreakingChange] = [] def diff(self) -> SchemaDiffResult: """Perform the diff and return results.""" self.changes = [] self._diff_object(self.old, self.new, "", depth=0) return SchemaDiffResult( changes=self.changes, change_type=self._classify_changes(), ) def _classify_changes(self) -> ChangeType: """Classify the overall change type based on detected changes.""" if not self.changes: return ChangeType.PATCH # Any breaking change under backward compatibility = MAJOR backward_breaking = any(c.kind in BACKWARD_BREAKING for c in self.changes) if backward_breaking: return ChangeType.MAJOR # Additions that don't break backward compatibility = MINOR additions = any( c.kind in { ChangeKind.PROPERTY_ADDED, ChangeKind.ENUM_VALUES_ADDED, ChangeKind.NULLABLE_ADDED, ChangeKind.DEFAULT_ADDED, } for c in self.changes ) if additions: return ChangeType.MINOR return ChangeType.PATCH def _diff_object( self, old: dict[str, Any], new: dict[str, Any], path: str, depth: int = 0 ) -> None: """Diff two schema objects recursively. Args: old: The old schema object. new: The new schema object. path: The JSON path to the current location. depth: Current recursion depth (for stack overflow protection). """ # Protect against circular schemas that could cause stack overflow if depth > self.MAX_DEPTH: return # Compare properties old_props = old.get("properties", {}) new_props = new.get("properties", {}) self._diff_properties( old_props, new_props, f"{path}.properties" if path else "properties", depth ) # Compare required fields old_required = set(old.get("required", [])) new_required = set(new.get("required", [])) self._diff_required(old_required, new_required, path) # Compare type self._diff_type(old, new, path) # Compare constraints self._diff_constraints(old, new, path) # Compare enum values self._diff_enum(old, new, path) # Compare default self._diff_default(old, new, path) # Compare nullable (for schemas that use nullable keyword) self._diff_nullable(old, new, path) # Compare additionalProperties self._diff_additional_properties(old, new, path, depth) # Recurse into items for arrays if old.get("type") == "array" and new.get("type") == "array": old_items = old.get("items", {}) new_items = new.get("items", {}) if old_items or new_items: self._diff_object( old_items, new_items, f"{path}.items" if path else "items", depth + 1 ) def _diff_properties( self, old_props: dict[str, Any], new_props: dict[str, Any], path: str, depth: int ) -> None: """Compare properties between schemas.""" old_keys = set(old_props.keys()) new_keys = set(new_props.keys()) # Removed properties for key in old_keys - new_keys: self.changes.append( BreakingChange( kind=ChangeKind.PROPERTY_REMOVED, path=f"{path}.{key}", message=f"Property '{key}' was removed", old_value=old_props[key], new_value=None, ) ) # Added properties for key in new_keys - old_keys: self.changes.append( BreakingChange( kind=ChangeKind.PROPERTY_ADDED, path=f"{path}.{key}", message=f"Property '{key}' was added", old_value=None, new_value=new_props[key], ) ) # Modified properties (recurse) for key in old_keys & new_keys: self._diff_object(old_props[key], new_props[key], f"{path}.{key}", depth + 1) def _diff_required(self, old_req: set[str], new_req: set[str], path: str) -> None: """Compare required fields.""" req_path = f"{path}.required" if path else "required" for field_name in new_req - old_req: self.changes.append( BreakingChange( kind=ChangeKind.REQUIRED_ADDED, path=req_path, message=f"Field '{field_name}' is now required", old_value=list(old_req), new_value=list(new_req), ) ) for field_name in old_req - new_req: self.changes.append( BreakingChange( kind=ChangeKind.REQUIRED_REMOVED, path=req_path, message=f"Field '{field_name}' is no longer required", old_value=list(old_req), new_value=list(new_req), ) ) def _diff_type(self, old: dict[str, Any], new: dict[str, Any], path: str) -> None: """Compare type definitions. JSON Schema allows ``type`` to be a single string or a list of strings. This method normalises lists for order-independent comparison and recognises nullable expansions/contractions expressed via type arrays (e.g. ``"string"`` → ``["string", "null"]``) rather than misfiring a TYPE_CHANGED event. """ old_type = old.get("type") new_type = new.get("type") if old_type is None or new_type is None: return type_path = f"{path}.type" if path else "type" # Check for nullable expansion: "T" → ["T", "null"] if ( isinstance(old_type, str) and isinstance(new_type, list) and sorted(new_type) == sorted([old_type, "null"]) ): self.changes.append( BreakingChange( kind=ChangeKind.NULLABLE_ADDED, path=type_path, message=( f"Field became nullable (type widened from '{old_type}' to {new_type!r})" ), old_value=old_type, new_value=new_type, ) ) return # Check for nullable contraction: ["T", "null"] → "T" if ( isinstance(old_type, list) and isinstance(new_type, str) and sorted(old_type) == sorted([new_type, "null"]) ): self.changes.append( BreakingChange( kind=ChangeKind.NULLABLE_REMOVED, path=type_path, message=( f"Field is no longer nullable" f" (type narrowed from {old_type!r} to '{new_type}')" ), old_value=old_type, new_value=new_type, ) ) return # Normalise lists for order-independent comparison before checking equality old_norm: Any = sorted(old_type) if isinstance(old_type, list) else old_type new_norm: Any = sorted(new_type) if isinstance(new_type, list) else new_type if old_norm == new_norm: return # Widening / narrowing only applies to scalar string types if isinstance(old_norm, str) and isinstance(new_norm, str): if (old_norm, new_norm) in self.TYPE_WIDENING: self.changes.append( BreakingChange( kind=ChangeKind.TYPE_WIDENED, path=type_path, message=f"Type widened from '{old_norm}' to '{new_norm}'", old_value=old_type, new_value=new_type, ) ) return if (new_norm, old_norm) in self.TYPE_WIDENING: self.changes.append( BreakingChange( kind=ChangeKind.TYPE_NARROWED, path=type_path, message=f"Type narrowed from '{old_norm}' to '{new_norm}'", old_value=old_type, new_value=new_type, ) ) return self.changes.append( BreakingChange( kind=ChangeKind.TYPE_CHANGED, path=type_path, message=f"Type changed from {old_type!r} to {new_type!r}", old_value=old_type, new_value=new_type, ) ) def _diff_constraints(self, old: dict[str, Any], new: dict[str, Any], path: str) -> None: """Compare constraints like minLength, maxLength, minimum, maximum, pattern.""" # Constraints that when increased are "relaxed" (less restrictive) relaxing_increase = {"maxLength", "maxItems", "maximum", "exclusiveMaximum"} # Constraints that when decreased are "relaxed" relaxing_decrease = {"minLength", "minItems", "minimum", "exclusiveMinimum"} all_constraints = relaxing_increase | relaxing_decrease | {"pattern"} for constraint in all_constraints: old_val = old.get(constraint) new_val = new.get(constraint) if old_val == new_val: continue constraint_path = f"{path}.{constraint}" if path else constraint # Constraint removed = relaxed if old_val is not None and new_val is None: self.changes.append( BreakingChange( kind=ChangeKind.CONSTRAINT_RELAXED, path=constraint_path, message=f"Constraint '{constraint}' was removed", old_value=old_val, new_value=None, ) ) # Constraint added = tightened elif old_val is None and new_val is not None: self.changes.append( BreakingChange( kind=ChangeKind.CONSTRAINT_TIGHTENED, path=constraint_path, message=f"Constraint '{constraint}' was added with value {new_val}", old_value=None, new_value=new_val, ) ) # Constraint changed (both old_val and new_val are not None at this point) elif constraint in relaxing_increase and old_val is not None and new_val is not None: if new_val > old_val: kind = ChangeKind.CONSTRAINT_RELAXED msg = f"Constraint '{constraint}' relaxed from {old_val} to {new_val}" else: kind = ChangeKind.CONSTRAINT_TIGHTENED msg = f"Constraint '{constraint}' tightened from {old_val} to {new_val}" self.changes.append( BreakingChange( kind=kind, path=constraint_path, message=msg, old_value=old_val, new_value=new_val, ) ) elif constraint in relaxing_decrease and old_val is not None and new_val is not None: if new_val < old_val: kind = ChangeKind.CONSTRAINT_RELAXED msg = f"Constraint '{constraint}' relaxed from {old_val} to {new_val}" else: kind = ChangeKind.CONSTRAINT_TIGHTENED msg = f"Constraint '{constraint}' tightened from {old_val} to {new_val}" self.changes.append( BreakingChange( kind=kind, path=constraint_path, message=msg, old_value=old_val, new_value=new_val, ) ) elif constraint == "pattern": # Pattern changes are always considered tightening (conservative) self.changes.append( BreakingChange( kind=ChangeKind.CONSTRAINT_TIGHTENED, path=constraint_path, message=f"Pattern changed from '{old_val}' to '{new_val}'", old_value=old_val, new_value=new_val, ) ) def _diff_enum(self, old: dict[str, Any], new: dict[str, Any], path: str) -> None: """Compare enum values. Uses ``_list_diff`` to handle enum values that contain unhashable types (dicts, nested lists), which are valid in JSON Schema but crash set(). """ old_enum_list: list[Any] = old.get("enum", []) new_enum_list: list[Any] = new.get("enum", []) if not old_enum_list and not new_enum_list: return enum_path = f"{path}.enum" if path else "enum" added, removed = _list_diff(old_enum_list, new_enum_list) if added: self.changes.append( BreakingChange( kind=ChangeKind.ENUM_VALUES_ADDED, path=enum_path, message=f"Enum values added: {added}", old_value=old_enum_list, new_value=new_enum_list, ) ) if removed: self.changes.append( BreakingChange( kind=ChangeKind.ENUM_VALUES_REMOVED, path=enum_path, message=f"Enum values removed: {removed}", old_value=old_enum_list, new_value=new_enum_list, ) ) def _diff_default(self, old: dict[str, Any], new: dict[str, Any], path: str) -> None: """Compare default values.""" old_default = old.get("default") new_default = new.get("default") has_old = "default" in old has_new = "default" in new if not has_old and not has_new: return default_path = f"{path}.default" if path else "default" if has_old and not has_new: self.changes.append( BreakingChange( kind=ChangeKind.DEFAULT_REMOVED, path=default_path, message=f"Default value removed (was {old_default})", old_value=old_default, new_value=None, ) ) elif not has_old and has_new: self.changes.append( BreakingChange( kind=ChangeKind.DEFAULT_ADDED, path=default_path, message=f"Default value added: {new_default}", old_value=None, new_value=new_default, ) ) elif old_default != new_default: self.changes.append( BreakingChange( kind=ChangeKind.DEFAULT_CHANGED, path=default_path, message=f"Default value changed from {old_default} to {new_default}", old_value=old_default, new_value=new_default, ) ) def _diff_nullable(self, old: dict[str, Any], new: dict[str, Any], path: str) -> None: """Compare nullable flag (common in OpenAPI/JSON Schema extensions).""" old_nullable = old.get("nullable", False) new_nullable = new.get("nullable", False) if old_nullable == new_nullable: return nullable_path = f"{path}.nullable" if path else "nullable" if new_nullable and not old_nullable: self.changes.append( BreakingChange( kind=ChangeKind.NULLABLE_ADDED, path=nullable_path, message="Field is now nullable", old_value=False, new_value=True, ) ) else: self.changes.append( BreakingChange( kind=ChangeKind.NULLABLE_REMOVED, path=nullable_path, message="Field is no longer nullable", old_value=True, new_value=False, ) ) def _diff_additional_properties( self, old: dict[str, Any], new: dict[str, Any], path: str, depth: int, ) -> None: """Compare the ``additionalProperties`` keyword. JSON Schema defaults ``additionalProperties`` to ``true`` when absent, meaning all unknown properties are permitted. Changes to this keyword affect whether consumers can send extra fields: - ``true`` → ``false``: restricts unknown properties (backward-breaking, emitted as CONSTRAINT_TIGHTENED) - ``false`` → ``true``: allows unknown properties (forward-breaking, emitted as CONSTRAINT_RELAXED) - schema → schema: recurse into the sub-schema to detect nested changes """ _missing: Any = object() old_ap = old.get("additionalProperties", _missing) new_ap = new.get("additionalProperties", _missing) # Treat absent as the JSON Schema default (True) old_val: Any = True if old_ap is _missing else old_ap new_val: Any = True if new_ap is _missing else new_ap if old_val == new_val: return ap_path = f"{path}.additionalProperties" if path else "additionalProperties" if isinstance(old_val, bool) and isinstance(new_val, bool): if not new_val: # true → false: restricts unknown properties (backward-breaking) self.changes.append( BreakingChange( kind=ChangeKind.CONSTRAINT_TIGHTENED, path=ap_path, message=( "additionalProperties restricted to false" " (unknown properties no longer permitted)" ), old_value=old_val, new_value=new_val, ) ) else: # false → true: allows unknown properties (forward-breaking) self.changes.append( BreakingChange( kind=ChangeKind.CONSTRAINT_RELAXED, path=ap_path, message=( "additionalProperties relaxed to true" " (unknown properties now permitted)" ), old_value=old_val, new_value=new_val, ) ) elif isinstance(old_val, dict) and isinstance(new_val, dict): # Both are schema objects — recurse for fine-grained change detection self._diff_object(old_val, new_val, ap_path, depth + 1) else: # Boolean ↔ schema mismatch — conservatively flag as tightening self.changes.append( BreakingChange( kind=ChangeKind.CONSTRAINT_TIGHTENED, path=ap_path, message=f"additionalProperties changed from {old_val!r} to {new_val!r}", old_value=None if old_ap is _missing else old_ap, new_value=None if new_ap is _missing else new_ap, ) ) def diff_schemas( old_schema: dict[str, Any], new_schema: dict[str, Any], resolve_references: bool = True, ) -> SchemaDiffResult: """Convenience function to diff two schemas. Args: old_schema: The original schema. new_schema: The new schema to compare. resolve_references: Whether to resolve $ref pointers before diffing. Defaults to True. Set to False only if schemas are known to have no $ref pointers or if you want to compare raw schemas. Returns: SchemaDiffResult containing detected changes. """ if resolve_references: old_schema = resolve_refs(old_schema) new_schema = resolve_refs(new_schema) differ = SchemaDiff(old_schema, new_schema) return differ.diff() def check_compatibility( old_schema: dict[str, Any], new_schema: dict[str, Any], mode: CompatibilityMode, ) -> tuple[bool, list[BreakingChange]]: """Check if a schema change is compatible under the given mode. Returns: Tuple of (is_compatible, list of breaking changes) """ result = diff_schemas(old_schema, new_schema) breaking = result.breaking_for_mode(mode) return len(breaking) == 0, breaking # ============================================================================= # Guarantee Diffing # ============================================================================= class GuaranteeChangeKind(StrEnum): """Types of guarantee changes.""" # Nullability guarantees NOT_NULL_ADDED = "not_null_added" NOT_NULL_REMOVED = "not_null_removed" # Uniqueness guarantees UNIQUE_ADDED = "unique_added" UNIQUE_REMOVED = "unique_removed" # Accepted values guarantees ACCEPTED_VALUES_ADDED = "accepted_values_added" ACCEPTED_VALUES_REMOVED = "accepted_values_removed" ACCEPTED_VALUES_EXPANDED = "accepted_values_expanded" # More values allowed ACCEPTED_VALUES_CONTRACTED = "accepted_values_contracted" # Fewer values # Relationship guarantees RELATIONSHIP_ADDED = "relationship_added" RELATIONSHIP_REMOVED = "relationship_removed" # Expression guarantees (dbt_utils.expression_is_true) EXPRESSION_ADDED = "expression_added" EXPRESSION_REMOVED = "expression_removed" EXPRESSION_CHANGED = "expression_changed" # Freshness guarantees FRESHNESS_ADDED = "freshness_added" FRESHNESS_REMOVED = "freshness_removed" FRESHNESS_RELAXED = "freshness_relaxed" # Longer allowed delay FRESHNESS_TIGHTENED = "freshness_tightened" # Shorter allowed delay # Volume guarantees VOLUME_ADDED = "volume_added" VOLUME_REMOVED = "volume_removed" VOLUME_RELAXED = "volume_relaxed" VOLUME_TIGHTENED = "volume_tightened" # Custom/other guarantees CUSTOM_GUARANTEE_ADDED = "custom_guarantee_added" CUSTOM_GUARANTEE_REMOVED = "custom_guarantee_removed" CUSTOM_GUARANTEE_CHANGED = "custom_guarantee_changed" # Map guarantee change kinds to their default severity GUARANTEE_SEVERITY: dict[GuaranteeChangeKind, GuaranteeChangeSeverity] = { # Adding guarantees = INFO (stricter is safe) GuaranteeChangeKind.NOT_NULL_ADDED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.UNIQUE_ADDED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.ACCEPTED_VALUES_ADDED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.ACCEPTED_VALUES_CONTRACTED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.RELATIONSHIP_ADDED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.EXPRESSION_ADDED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.FRESHNESS_ADDED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.FRESHNESS_TIGHTENED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.VOLUME_ADDED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.VOLUME_TIGHTENED: GuaranteeChangeSeverity.INFO, GuaranteeChangeKind.CUSTOM_GUARANTEE_ADDED: GuaranteeChangeSeverity.INFO, # Removing/relaxing guarantees = WARNING GuaranteeChangeKind.NOT_NULL_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.UNIQUE_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.ACCEPTED_VALUES_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.ACCEPTED_VALUES_EXPANDED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.RELATIONSHIP_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.EXPRESSION_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.EXPRESSION_CHANGED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.FRESHNESS_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.FRESHNESS_RELAXED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.VOLUME_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.VOLUME_RELAXED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.CUSTOM_GUARANTEE_REMOVED: GuaranteeChangeSeverity.WARNING, GuaranteeChangeKind.CUSTOM_GUARANTEE_CHANGED: GuaranteeChangeSeverity.WARNING, } class GuaranteeChange(BaseModel): """A single guarantee change detected in a diff.""" model_config = ConfigDict(frozen=True) kind: GuaranteeChangeKind path: str = Field(..., description="e.g., 'nullability.user_id' or 'accepted_values.status'") message: str severity: GuaranteeChangeSeverity old_value: bool | int | float | str | list[Any] | dict[str, Any] | None = None new_value: bool | int | float | str | list[Any] | dict[str, Any] | None = None def to_dict(self) -> dict[str, Any]: """Convert to dictionary for JSON serialization. Note: Prefer using model_dump() directly. This method exists for backward compatibility. """ return { "type": str(self.kind), "path": self.path, "message": self.message, "severity": str(self.severity), "old_value": self.old_value, "new_value": self.new_value, } class GuaranteeDiffResult(BaseModel): """Result of comparing two guarantee sets.""" model_config = ConfigDict(frozen=True) changes: list[GuaranteeChange] = Field(default_factory=list) @property def has_changes(self) -> bool: return len(self.changes) > 0 def by_severity(self, severity: GuaranteeChangeSeverity) -> list[GuaranteeChange]: """Return changes matching the given severity.""" return [c for c in self.changes if c.severity == severity] @property def info_changes(self) -> list[GuaranteeChange]: """Return INFO severity changes (additions).""" return self.by_severity(GuaranteeChangeSeverity.INFO) @property def warning_changes(self) -> list[GuaranteeChange]: """Return WARNING severity changes (removals/relaxations).""" return self.by_severity(GuaranteeChangeSeverity.WARNING) def is_breaking(self, mode: GuaranteeMode) -> bool: """Check if changes are breaking under the given mode.""" if mode == GuaranteeMode.IGNORE: return False if mode == GuaranteeMode.NOTIFY: return False # Notify only, never blocking if mode == GuaranteeMode.STRICT: # In strict mode, any WARNING is breaking return len(self.warning_changes) > 0 return False def breaking_changes(self, mode: GuaranteeMode) -> list[GuaranteeChange]: """Return changes that are breaking under the given mode.""" if mode != GuaranteeMode.STRICT: return [] return self.warning_changes class GuaranteeDiff: """Compares two guarantee sets and identifies changes.""" def __init__( self, old_guarantees: dict[str, Any] | None, new_guarantees: dict[str, Any] | None, ): # Normalize: replace None values with {} so .get("key", {}).keys() never fails self.old = {k: v or {} for k, v in (old_guarantees or {}).items()} self.new = {k: v or {} for k, v in (new_guarantees or {}).items()} self.changes: list[GuaranteeChange] = [] def diff(self) -> GuaranteeDiffResult: """Perform the diff and return results.""" self.changes = [] # Compare nullability (not_null tests) self._diff_nullability() # Compare uniqueness (unique tests) self._diff_uniqueness() # Compare accepted_values self._diff_accepted_values() # Compare relationships self._diff_relationships() # Compare expressions self._diff_expressions() # Compare freshness self._diff_freshness() # Compare volume self._diff_volume() # Compare custom guarantees self._diff_custom() return GuaranteeDiffResult(changes=self.changes) def _add_change( self, kind: GuaranteeChangeKind, path: str, message: str, old_value: Any = None, new_value: Any = None, ) -> None: """Add a change with its default severity.""" self.changes.append( GuaranteeChange( kind=kind, path=path, message=message, severity=GUARANTEE_SEVERITY[kind], old_value=old_value, new_value=new_value, ) ) def _diff_nullability(self) -> None: """Compare not_null guarantees.""" old_cols = set(self.old.get("nullability", {}).keys()) new_cols = set(self.new.get("nullability", {}).keys()) for col in new_cols - old_cols: self._add_change( GuaranteeChangeKind.NOT_NULL_ADDED, f"nullability.{col}", f"not_null guarantee added for column '{col}'", new_value=col, ) for col in old_cols - new_cols: self._add_change( GuaranteeChangeKind.NOT_NULL_REMOVED, f"nullability.{col}", f"not_null guarantee removed for column '{col}'", old_value=col, ) def _diff_uniqueness(self) -> None: """Compare unique guarantees.""" old_cols = set(self.old.get("uniqueness", {}).keys()) new_cols = set(self.new.get("uniqueness", {}).keys()) for col in new_cols - old_cols: self._add_change( GuaranteeChangeKind.UNIQUE_ADDED, f"uniqueness.{col}", f"unique guarantee added for column '{col}'", new_value=col, ) for col in old_cols - new_cols: self._add_change( GuaranteeChangeKind.UNIQUE_REMOVED, f"uniqueness.{col}", f"unique guarantee removed for column '{col}'", old_value=col, ) def _diff_accepted_values(self) -> None: """Compare accepted_values guarantees.""" old_av = self.old.get("accepted_values", {}) new_av = self.new.get("accepted_values", {}) old_cols = set(old_av.keys()) new_cols = set(new_av.keys()) # Completely new accepted_values constraints for col in new_cols - old_cols: self._add_change( GuaranteeChangeKind.ACCEPTED_VALUES_ADDED, f"accepted_values.{col}", f"accepted_values guarantee added for column '{col}'", new_value=new_av[col], ) # Completely removed accepted_values constraints for col in old_cols - new_cols: self._add_change( GuaranteeChangeKind.ACCEPTED_VALUES_REMOVED, f"accepted_values.{col}", f"accepted_values guarantee removed for column '{col}'", old_value=old_av[col], ) # Modified accepted_values - compare value sets for col in old_cols & new_cols: old_list: list[Any] = old_av[col] if isinstance(old_av[col], list) else [] new_list: list[Any] = new_av[col] if isinstance(new_av[col], list) else [] # Use _list_diff to safely handle unhashable values (dicts, nested lists) added, removed = _list_diff(old_list, new_list) if added or removed: if added and not removed: # Values added = expanded (more permissive) self._add_change( GuaranteeChangeKind.ACCEPTED_VALUES_EXPANDED, f"accepted_values.{col}", f"accepted_values for '{col}' expanded: added {added}", old_value=old_list, new_value=new_list, ) elif removed and not added: # Values removed = contracted (more restrictive) self._add_change( GuaranteeChangeKind.ACCEPTED_VALUES_CONTRACTED, f"accepted_values.{col}", f"accepted_values for '{col}' contracted: removed {removed}", old_value=old_list, new_value=new_list, ) else: # Both added and removed — emit separate changes so each # gets its correct severity (CONTRACTED=INFO, EXPANDED=WARNING). self._add_change( GuaranteeChangeKind.ACCEPTED_VALUES_CONTRACTED, f"accepted_values.{col}", f"accepted_values for '{col}' contracted: removed {removed}", old_value=old_list, new_value=new_list, ) self._add_change( GuaranteeChangeKind.ACCEPTED_VALUES_EXPANDED, f"accepted_values.{col}", f"accepted_values for '{col}' expanded: added {added}", old_value=old_list, new_value=new_list, ) def _diff_relationships(self) -> None: """Compare relationship guarantees.""" old_rels = self.old.get("relationships", {}) new_rels = self.new.get("relationships", {}) old_keys = set(old_rels.keys()) new_keys = set(new_rels.keys()) for key in new_keys - old_keys: self._add_change( GuaranteeChangeKind.RELATIONSHIP_ADDED, f"relationships.{key}", f"relationship guarantee added: {key}", new_value=new_rels[key], ) for key in old_keys - new_keys: self._add_change( GuaranteeChangeKind.RELATIONSHIP_REMOVED, f"relationships.{key}", f"relationship guarantee removed: {key}", old_value=old_rels[key], ) def _diff_expressions(self) -> None: """Compare expression guarantees (dbt_utils.expression_is_true).""" old_exprs = self.old.get("expressions", {}) new_exprs = self.new.get("expressions", {}) old_keys = set(old_exprs.keys()) new_keys = set(new_exprs.keys()) for key in new_keys - old_keys: self._add_change( GuaranteeChangeKind.EXPRESSION_ADDED, f"expressions.{key}", f"expression guarantee added: {key}", new_value=new_exprs[key], ) for key in old_keys - new_keys: self._add_change( GuaranteeChangeKind.EXPRESSION_REMOVED, f"expressions.{key}", f"expression guarantee removed: {key}", old_value=old_exprs[key], ) for key in old_keys & new_keys: if old_exprs[key] != new_exprs[key]: self._add_change( GuaranteeChangeKind.EXPRESSION_CHANGED, f"expressions.{key}", f"expression guarantee changed: {key}", old_value=old_exprs[key], new_value=new_exprs[key], ) @staticmethod def _extract_freshness_duration_seconds(value: Any) -> float | None: """Extract a freshness duration as total seconds from known formats. Supports these formats: - ``{"warn_after": {"days": N, "hours": N, "minutes": N, "seconds": N}}`` - ``{"warn_after": {"count": N, "period": "hour"}}`` (dbt-style) - ``{"max_staleness_minutes": N}`` Returns ``None`` if the format is unrecognised so callers can fall back to a conservative default. """ if not isinstance(value, dict): return None # Format 1: warn_after with day/hour/minute/second components warn_after = value.get("warn_after") if isinstance(warn_after, dict): # Check for direct time-unit keys first time_units = {"days": 86400, "hours": 3600, "minutes": 60, "seconds": 1} if any(u in warn_after for u in time_units): total = 0.0 for unit, multiplier in time_units.items(): total += float(warn_after.get(unit, 0)) * multiplier return total # dbt-style: {"count": N, "period": "hour"} if "count" in warn_after and "period" in warn_after: try: count = float(warn_after["count"]) except (TypeError, ValueError): return None period = str(warn_after["period"]).lower().rstrip("s") period_seconds = {"day": 86400, "hour": 3600, "minute": 60, "second": 1} if period in period_seconds: return count * period_seconds[period] # Format 2: max_staleness_minutes max_staleness = value.get("max_staleness_minutes") if max_staleness is not None: return float(max_staleness) * 60 return None def _diff_freshness(self) -> None: """Compare freshness guarantees.""" old_fresh = self.old.get("freshness") new_fresh = self.new.get("freshness") if old_fresh is None and new_fresh is not None: self._add_change( GuaranteeChangeKind.FRESHNESS_ADDED, "freshness", f"freshness guarantee added: {new_fresh}", new_value=new_fresh, ) elif old_fresh is not None and new_fresh is None: self._add_change( GuaranteeChangeKind.FRESHNESS_REMOVED, "freshness", f"freshness guarantee removed (was {old_fresh})", old_value=old_fresh, ) elif old_fresh is not None and new_fresh is not None and old_fresh != new_fresh: old_seconds = self._extract_freshness_duration_seconds(old_fresh) new_seconds = self._extract_freshness_duration_seconds(new_fresh) if old_seconds is not None and new_seconds is not None: if old_seconds == new_seconds: # Semantically equal despite different representations # (e.g. {"hours": 1} vs {"minutes": 60}) — no change to emit. return elif new_seconds > old_seconds: kind = GuaranteeChangeKind.FRESHNESS_RELAXED else: kind = GuaranteeChangeKind.FRESHNESS_TIGHTENED else: # Unrecognisable format — default to RELAXED (conservative; # triggers WARNING which surfaces the change for review) kind = GuaranteeChangeKind.FRESHNESS_RELAXED self._add_change( kind, "freshness", f"freshness guarantee changed from {old_fresh} to {new_fresh}", old_value=old_fresh, new_value=new_fresh, ) def _compare_volume_fields( self, old_vol: dict[str, Any], new_vol: dict[str, Any], ) -> None: """Emit per-field volume changes with correct directionality. - ``min_rows``: increase = TIGHTENED (harder to meet), decrease = RELAXED - ``max_rows``: increase = RELAXED (more permissive), decrease = TIGHTENED - Unknown numeric fields: default to RELAXED (conservative — triggers WARNING) """ all_keys = set(old_vol.keys()) | set(new_vol.keys()) for key in sorted(all_keys): old_val = old_vol.get(key) new_val = new_vol.get(key) if old_val == new_val: continue # Only compare numeric fields if not (isinstance(old_val, int | float) and isinstance(new_val, int | float)): self._add_change( GuaranteeChangeKind.VOLUME_RELAXED, f"volume.{key}", f"volume.{key} changed from {old_val} to {new_val}", old_value=old_val, new_value=new_val, ) continue if key == "min_rows": # Higher min = tightened (harder to satisfy) kind = ( GuaranteeChangeKind.VOLUME_TIGHTENED if new_val > old_val else GuaranteeChangeKind.VOLUME_RELAXED ) elif key == "max_rows": # Higher max = relaxed (more permissive) kind = ( GuaranteeChangeKind.VOLUME_RELAXED if new_val > old_val else GuaranteeChangeKind.VOLUME_TIGHTENED ) else: # Unknown numeric field — default to RELAXED (conservative) kind = GuaranteeChangeKind.VOLUME_RELAXED self._add_change( kind, f"volume.{key}", f"volume.{key} changed from {old_val} to {new_val}", old_value=old_val, new_value=new_val, ) def _diff_volume(self) -> None: """Compare volume guarantees.""" old_vol = self.old.get("volume") new_vol = self.new.get("volume") if old_vol is None and new_vol is not None: self._add_change( GuaranteeChangeKind.VOLUME_ADDED, "volume", f"volume guarantee added: {new_vol}", new_value=new_vol, ) elif old_vol is not None and new_vol is None: self._add_change( GuaranteeChangeKind.VOLUME_REMOVED, "volume", f"volume guarantee removed (was {old_vol})", old_value=old_vol, ) elif ( old_vol is not None and new_vol is not None and old_vol != new_vol and isinstance(old_vol, dict) and isinstance(new_vol, dict) ): self._compare_volume_fields(old_vol, new_vol) def _diff_custom(self) -> None: """Compare custom guarantees.""" old_custom = self.old.get("custom", {}) new_custom = self.new.get("custom", {}) old_keys = set(old_custom.keys()) new_keys = set(new_custom.keys()) for key in new_keys - old_keys: self._add_change( GuaranteeChangeKind.CUSTOM_GUARANTEE_ADDED, f"custom.{key}", f"custom guarantee added: {key}", new_value=new_custom[key], ) for key in old_keys - new_keys: self._add_change( GuaranteeChangeKind.CUSTOM_GUARANTEE_REMOVED, f"custom.{key}", f"custom guarantee removed: {key}", old_value=old_custom[key], ) for key in old_keys & new_keys: if old_custom[key] != new_custom[key]: self._add_change( GuaranteeChangeKind.CUSTOM_GUARANTEE_CHANGED, f"custom.{key}", f"custom guarantee changed: {key}", old_value=old_custom[key], new_value=new_custom[key], ) def diff_guarantees( old_guarantees: dict[str, Any] | None, new_guarantees: dict[str, Any] | None, ) -> GuaranteeDiffResult: """Convenience function to diff two guarantee sets.""" differ = GuaranteeDiff(old_guarantees, new_guarantees) return differ.diff() def check_guarantee_compatibility( old_guarantees: dict[str, Any] | None, new_guarantees: dict[str, Any] | None, mode: GuaranteeMode, ) -> tuple[bool, list[GuaranteeChange]]: """Check if guarantee changes are compatible under the given mode. Returns: Tuple of (is_compatible, list of breaking changes) """ result = diff_guarantees(old_guarantees, new_guarantees) if mode == GuaranteeMode.IGNORE: return True, [] breaking = result.breaking_changes(mode) return len(breaking) == 0, breaking class ContractDiffResult(BaseModel): """Combined result of schema and guarantee diffs.""" model_config = ConfigDict(frozen=True) schema_diff: SchemaDiffResult guarantee_diff: GuaranteeDiffResult @property def has_changes(self) -> bool: return self.schema_diff.has_changes or self.guarantee_diff.has_changes def is_compatible( self, schema_mode: CompatibilityMode, guarantee_mode: GuaranteeMode = GuaranteeMode.NOTIFY, ) -> bool: """Check if the contract change is compatible.""" schema_ok = self.schema_diff.is_compatible(schema_mode) guarantee_ok = not self.guarantee_diff.is_breaking(guarantee_mode) return schema_ok and guarantee_ok def to_dict(self) -> dict[str, Any]: """Convert to dictionary for JSON serialization. Note: Prefer using model_dump() directly. This method exists for backward compatibility. """ return { "schema_changes": [c.to_dict() for c in self.schema_diff.changes], "schema_change_type": str(self.schema_diff.change_type), "guarantee_changes": [c.to_dict() for c in self.guarantee_diff.changes], "guarantee_warnings": len(self.guarantee_diff.warning_changes), "guarantee_info": len(self.guarantee_diff.info_changes), } def diff_contracts( old_schema: dict[str, Any], new_schema: dict[str, Any], old_guarantees: dict[str, Any] | None = None, new_guarantees: dict[str, Any] | None = None, ) -> ContractDiffResult: """Diff both schema and guarantees for a contract change.""" return ContractDiffResult( schema_diff=diff_schemas(old_schema, new_schema), guarantee_diff=diff_guarantees(old_guarantees, new_guarantees), )
"""Schema diffing and compatibility checking.
Implements JSON Schema diffing with compatibility modes borrowed from
Kafka Schema Registry (backward, forward, full, none).
"""
import copy
from enum import StrEnum
from typing import Any
from pydantic import BaseModel, ConfigDict, Field
from tessera.models.enums import (
ChangeType,
CompatibilityMode,
GuaranteeChangeSeverity,
GuaranteeMode,
)
class ChangeKind(StrEnum):
"""Types of schema changes."""
PROPERTY_ADDED = "property_added"
PROPERTY_REMOVED = "property_removed"
PROPERTY_RENAMED = "property_renamed" # Detected heuristically
TYPE_CHANGED = "type_changed"
TYPE_WIDENED = "type_widened" # e.g., int -> number
TYPE_NARROWED = "type_narrowed" # e.g., number -> int
REQUIRED_ADDED = "required_added"
REQUIRED_REMOVED = "required_removed"
ENUM_VALUES_ADDED = "enum_values_added"
ENUM_VALUES_REMOVED = "enum_values_removed"
CONSTRAINT_TIGHTENED = "constraint_tightened" # e.g., maxLength decreased
CONSTRAINT_RELAXED = "constraint_relaxed" # e.g., maxLength increased
DEFAULT_ADDED = "default_added"
DEFAULT_REMOVED = "default_removed"
DEFAULT_CHANGED = "default_changed"
NULLABLE_ADDED = "nullable_added"
NULLABLE_REMOVED = "nullable_removed"
# Which changes are breaking under each compatibility mode
BACKWARD_BREAKING = {
ChangeKind.PROPERTY_REMOVED,
ChangeKind.PROPERTY_RENAMED,
ChangeKind.TYPE_CHANGED,
ChangeKind.TYPE_NARROWED,
ChangeKind.REQUIRED_ADDED,
ChangeKind.ENUM_VALUES_REMOVED,
ChangeKind.CONSTRAINT_TIGHTENED,
ChangeKind.DEFAULT_REMOVED,
ChangeKind.NULLABLE_REMOVED,
}
FORWARD_BREAKING = {
ChangeKind.PROPERTY_ADDED,
ChangeKind.PROPERTY_RENAMED,
ChangeKind.TYPE_CHANGED,
ChangeKind.TYPE_WIDENED,
ChangeKind.REQUIRED_REMOVED,
ChangeKind.ENUM_VALUES_ADDED,
ChangeKind.CONSTRAINT_RELAXED,
ChangeKind.DEFAULT_ADDED,
ChangeKind.NULLABLE_ADDED,
}
# Full compatibility = intersection of backward and forward breaking
FULL_BREAKING = BACKWARD_BREAKING | FORWARD_BREAKING
class BreakingChange(BaseModel):
"""A single breaking change detected in a schema diff."""
model_config = ConfigDict(frozen=True)
kind: ChangeKind
path: str = Field(
..., description="JSON path to the affected element (e.g., 'properties.email')"
)
message: str
old_value: bool | int | float | str | list[Any] | dict[str, Any] | None = None
new_value: bool | int | float | str | list[Any] | dict[str, Any] | None = None
def to_dict(self) -> dict[str, Any]:
"""Convert to dictionary for JSON serialization.
Note: Prefer using model_dump() directly. This method exists for backward
compatibility.
"""
return {
"type": str(self.kind),
"path": self.path,
"message": self.message,
"old_value": self.old_value,
"new_value": self.new_value,
}
class SchemaDiffResult(BaseModel):

Xet Storage Details

Size:
55.7 kB
·
Xet hash:
8e7b5735b9d9308ff8dde629a45caf7ffa49df17c0e9877ad163d597f6598759

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.