Repository navigation
fix(plan): avoid migrating downstream views when not dropped or backfilled later #6321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,7 +263,6 @@ def build(self, plan: EvaluatablePlan) -> t.List[PlanStage]: | |
| if plan.is_dev: | ||
| before_promote_snapshots = all_selected_for_backfill_snapshots | ||
| after_promote_snapshots = set() | ||
| snapshots_with_schema_migration = [] | ||
| else: | ||
| before_promote_snapshots = { | ||
| s.snapshot_id | ||
|
|
@@ -274,45 +273,55 @@ def build(self, plan: EvaluatablePlan) -> t.List[PlanStage]: | |
| after_promote_snapshots = all_selected_for_backfill_snapshots - before_promote_snapshots | ||
| deployability_index = DeployabilityIndex.all_deployable() | ||
|
|
||
| snapshots_to_intervals = self._missing_intervals( | ||
| plan, snapshots_by_name, deployability_index | ||
| ) | ||
| needs_backfill = ( | ||
| not plan.empty_backfill and not plan.skip_backfill and bool(snapshots_to_intervals) | ||
| ) | ||
| missing_intervals_before_promote: SnapshotToIntervals = {} | ||
| missing_intervals_after_promote: SnapshotToIntervals = {} | ||
| if needs_backfill: | ||
| for snapshot, intervals in snapshots_to_intervals.items(): | ||
| if snapshot.snapshot_id in before_promote_snapshots: | ||
| missing_intervals_before_promote[snapshot] = intervals | ||
| elif snapshot.snapshot_id in after_promote_snapshots: | ||
| missing_intervals_after_promote[snapshot] = intervals | ||
|
|
||
| if plan.is_dev: | ||
| snapshots_with_schema_migration = [] | ||
| else: | ||
| snapshot_ids_with_schema_migration = [ | ||
| s.snapshot_id for s in snapshots.values() if s.requires_schema_migration_in_prod | ||
| ] | ||
| # Include all upstream dependencies of snapshots that require schema migration to make sure | ||
| # the upstream tables are created before the schema updates are applied | ||
| snapshot_ids_to_migrate = set(dag.subdag(*snapshot_ids_with_schema_migration)) | ||
| # Migrating a view replaces it, and engines like Postgres drop its dependent views with CASCADE, | ||
| # so those are migrated too to be recreated | ||
| # Migrating a view replaces it, and Postgres-family engines drop its dependent views with CASCADE, | ||
| # so those are migrated too to be recreated unless they will be created in a subsequent backfill | ||
| cascade_view_ids: t.Set[SnapshotId] = set() | ||
| for s_id in dag: | ||
| snapshot = snapshots[s_id] | ||
| if snapshot.is_view and any( | ||
| p_id in snapshot_ids_to_migrate | ||
| and snapshots[p_id].is_view | ||
| and snapshots[p_id].supports_schema_migration_in_prod | ||
| for p_id in snapshot.parents | ||
| if ( | ||
| snapshot.is_view | ||
| and snapshot not in missing_intervals_after_promote | ||
| and any( | ||
| (p_id in snapshot_ids_to_migrate or p_id in cascade_view_ids) | ||
| and snapshots[p_id].is_view | ||
| and snapshots[p_id].supports_schema_migration_in_prod | ||
| and snapshots[p_id].model.dialect in ("postgres", "risingwave", "redshift") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the execution adapter's cascade behavior, not the query dialect.
With Determine cascade behavior through a capability defined on
|
||
| for p_id in snapshot.parents | ||
| ) | ||
| ): | ||
| snapshot_ids_to_migrate.add(s_id) | ||
| cascade_view_ids.add(s_id) | ||
| snapshot_ids_to_migrate.update(cascade_view_ids) | ||
| snapshots_with_schema_migration = [ | ||
| snapshots[s_id] | ||
| for s_id in dag | ||
| if s_id in snapshot_ids_to_migrate | ||
| and snapshots[s_id].supports_schema_migration_in_prod | ||
| ] | ||
|
|
||
| snapshots_to_intervals = self._missing_intervals( | ||
| plan, snapshots_by_name, deployability_index | ||
| ) | ||
| needs_backfill = ( | ||
| not plan.empty_backfill and not plan.skip_backfill and bool(snapshots_to_intervals) | ||
| ) | ||
| missing_intervals_before_promote: SnapshotToIntervals = {} | ||
| missing_intervals_after_promote: SnapshotToIntervals = {} | ||
| if needs_backfill: | ||
| for snapshot, intervals in snapshots_to_intervals.items(): | ||
| if snapshot.snapshot_id in before_promote_snapshots: | ||
| missing_intervals_before_promote[snapshot] = intervals | ||
| elif snapshot.snapshot_id in after_promote_snapshots: | ||
| missing_intervals_after_promote[snapshot] = intervals | ||
|
|
||
| promoted_snapshots, demoted_snapshots, demoted_environment_naming_info = ( | ||
| self._get_promoted_demoted_snapshots(plan, existing_environment) | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cascade-dropped views can remain absent after the plan.
On Postgres-family engines, replacing a view drops its dependent views. The new guard at
sqlmesh/core/plan/stages.py:307removes any view with after-promotion missing intervals from the cascade set. Its descendants are then not reached, because the walk extends only from the migrate or cascade sets.Consider the chain
A -> B -> C. A requires schema migration. B has after-promotion missing intervals. C has complete intervals and does not require migration on its own. A's replacement drops both B and C. B is recreated by its after-promotion backfill. C is not in any migration or backfill set, so the plan does not recreate it.Before this change, C was added through B. The change removes that path for this chain.
Required behavior: every view that a migration cascade drops must exist when the plan completes. Its recreation must happen after the replacement of each ancestor that it depends on. Replacing B also cascades to C on Postgres, so C cannot be recreated before B's after-promotion backfill. Choose the mechanism that satisfies this order.