@@ -206,7 +206,7 @@ def build(self) -> Plan:
206206 dag = self ._build_dag ()
207207 directly_modified , indirectly_modified = self ._build_directly_and_indirectly_modified (dag )
208208
209- self ._check_destructive_changes (dag , directly_modified )
209+ self ._check_destructive_changes (directly_modified )
210210 self ._categorize_snapshots (dag , directly_modified , indirectly_modified )
211211 self ._adjust_new_snapshot_intervals ()
212212
@@ -436,63 +436,40 @@ def _adjust_new_snapshot_intervals(self) -> None:
436436 if new .is_forward_only :
437437 new .dev_intervals = new .intervals .copy ()
438438
439- def _check_destructive_changes (
440- self , dag : DAG [SnapshotId ], directly_modified : t .Set [SnapshotId ]
441- ) -> None :
442- def _raise_or_warn (snapshot : Snapshot ) -> None :
443- warning_msg = f"Plan results in a destructive change to forward-only model '{ snapshot .name } 's schema."
444- if snapshot .model .on_destructive_change .is_warn :
445- logger .warning (warning_msg )
446- return
447- raise PlanError (
448- f"{ warning_msg } To allow this, change the model's `on_destructive_change` setting to `warn` or `allow` or include it in the plan's `--allow-destructive-model` option."
449- )
439+ def _check_destructive_changes (self , directly_modified : t .Set [SnapshotId ]) -> None :
440+ for s_id in sorted (directly_modified ):
441+ snapshot = self ._context_diff .snapshots [s_id ]
442+ # should we raise/warn if this snapshot has/inherits a destructive change?
443+ should_raise_or_warn = (
444+ self ._is_forward_only_change (s_id ) or self ._forward_only
445+ ) and snapshot .needs_destructive_check (self ._allow_destructive_models )
450446
451- snapshot_is_destructive = {}
452- for s_id in dag :
453- # Only process snapshots that are modified by the plan
454- if s_id .name in self ._context_diff .modified_snapshots :
455- snapshot = self ._context_diff .snapshots [s_id ]
456-
457- if snapshot and snapshot .is_model :
458- # should we raise/warn if this snapshot has/inherits a destructive change?
459- should_raise_or_warn = (
460- snapshot .model .forward_only or self ._forward_only
461- ) and snapshot .needs_destructive_check (self ._allow_destructive_models )
462-
463- snapshot_is_destructive [s_id ] = False
464-
465- # get parent classifications
466- for parent_id in snapshot .parents :
467- if snapshot_is_destructive .get (parent_id ):
468- snapshot_is_destructive [s_id ] = True
469- if should_raise_or_warn :
470- _raise_or_warn (snapshot )
471- break
472-
473- # examine directly modified snapshots unless we already know the snapshot is destructive
474- if s_id in directly_modified and not snapshot_is_destructive [s_id ]:
475- new , old = self ._context_diff .modified_snapshots [snapshot .name ]
476-
477- # we must know all columns_to_types to determine whether a change is destructive
478- old_columns_to_types = old .model .columns_to_types or {}
479- new_columns_to_types = new .model .columns_to_types or {}
480-
481- if not columns_to_types_all_known (
482- old_columns_to_types
483- ) or not columns_to_types_all_known (new_columns_to_types ):
484- snapshot_is_destructive [s_id ] = False
485- else :
486- schema_diff = self ._engine_schema_differ .compare_columns (
487- new .name ,
488- old_columns_to_types ,
489- new_columns_to_types ,
490- )
447+ if not should_raise_or_warn or not snapshot .is_model :
448+ continue
449+
450+ new , old = self ._context_diff .modified_snapshots [snapshot .name ]
451+
452+ # we must know all columns_to_types to determine whether a change is destructive
453+ old_columns_to_types = old .model .columns_to_types or {}
454+ new_columns_to_types = new .model .columns_to_types or {}
455+
456+ if columns_to_types_all_known (old_columns_to_types ) and columns_to_types_all_known (
457+ new_columns_to_types
458+ ):
459+ schema_diff = self ._engine_schema_differ .compare_columns (
460+ new .name ,
461+ old_columns_to_types ,
462+ new_columns_to_types ,
463+ )
491464
492- has_drop = has_drop_alteration (schema_diff )
493- snapshot_is_destructive [s_id ] = has_drop
494- if has_drop and should_raise_or_warn :
495- _raise_or_warn (snapshot )
465+ if has_drop_alteration (schema_diff ):
466+ warning_msg = f"Plan results in a destructive change to forward-only model '{ snapshot .name } 's schema"
467+ if snapshot .model .on_destructive_change .is_warn :
468+ logger .warning (warning_msg )
469+ else :
470+ raise PlanError (
471+ f"{ warning_msg } . To allow this, change the model's `on_destructive_change` setting to `warn` or `allow` or include it in the plan's `--allow-destructive-model` option."
472+ )
496473
497474 def _categorize_snapshots (
498475 self ,
0 commit comments