Skip to content

Fix: Regression that caused view snapshos not to be migrated#5389

Merged
izeigerman merged 3 commits intomainfrom
fix-view-migration-regression
Sep 16, 2025
Merged

Fix: Regression that caused view snapshos not to be migrated#5389
izeigerman merged 3 commits intomainfrom
fix-view-migration-regression

Conversation

@izeigerman
Copy link
Collaborator

Previously, we were incorrectly skipping the migration stage for view snapshots.

@izeigerman izeigerman requested review from a team and Copilot September 16, 2025 17:16
@izeigerman izeigerman force-pushed the fix-view-migration-regression branch from 3e29bdb to 34fab79 Compare September 16, 2025 17:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a regression where view snapshots were incorrectly skipped during the migration stage in forward-only deployments. The fix ensures that view snapshots requiring schema updates are properly included in the migration process.

  • Updated snapshot migration logic to include view snapshots that need schema migration
  • Modified the migration stage to properly handle view creation during schema migration
  • Added comprehensive test coverage for forward-only view migration scenarios

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
sqlmesh/core/snapshot/definition.py Updated migration requirement check to include views (not just materialized models)
sqlmesh/core/snapshot/evaluator.py Modified migration logic to handle all snapshots and create views when needed
sqlmesh/core/plan/stages.py Enhanced stage building to include upstream dependencies for schema migrations
tests/core/test_snapshot_evaluator.py Updated test to verify view creation during migration
tests/core/test_plan_stages.py Adjusted test expectations for additional migration stage
tests/core/test_integration.py Added integration test for forward-only view migration
tests/core/test_context.py Updated test expectation for SQL changes
tests/fixtures/dbt/sushi_test/models/model_with_raw_code.sql Added IF NOT EXISTS clause to fixture

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +495 to +499
snapshots_by_name.values(),
lambda s: self._migrate_snapshot(
s,
snapshots_by_name,
target_data_objects[s.snapshot_id],
target_data_objects.get(s.snapshot_id),
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The migration logic now passes None for snapshots without target data objects, but the downstream code may not handle this case properly. Consider adding validation to ensure the migration logic can handle None target_data_object values.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the downstream method indicates that it supports an optional target_data_object in its signature

deployability_index=deployability_index,
create_render_kwargs=render_kwargs,
rendered_physical_properties=rendered_physical_properties,
dry_run=True,
Copy link

Copilot AI Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dry_run=True parameter suggests this is only a validation step, but for view migration to work properly, the view should actually be created. This may prevent view snapshots from being properly migrated.

Suggested change
dry_run=True,
dry_run=False,

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a bad comment all around

Comment on lines 249 to +252
stored_snapshots = self.state_reader.get_snapshots(plan.environment.snapshots)
snapshots = {**new_snapshots, **stored_snapshots}
snapshots_by_name = {s.name: s for s in snapshots.values()}
dag = snapshots_to_dag(snapshots.values())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reasoning for having this logic outside of EvaluatablePlan? For example should it have properties evaluatable_snapshots and dag which would return this info? Reading this over and I was surprised I couldn't consult the plan itself to get this information.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because EvaluatablePlan instance only contains data which can be (and will be) serialized. We could consider making it a property method of the EvaluatblePlan but currently there isn't really any other consumer for this. Plus we need access to the StateReader to fetch all snapshots which EvalutablePlan doesn't (and shouldn't) posses.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes the state reader. Thanks.

Comment on lines +284 to +286
if snapshots[s_id].is_paused
and snapshots[s_id].is_model
and not snapshots[s_id].is_symbolic
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to help me learn: Why couldn't these if conditions be applied when creating snapshot_ids_with_schema_migration?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see it is part of requires_schema_migration_in_prod. This is making sure the returned snapshots in subdag also have these properties.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct. So the snapshots in the subdag may not return True for requires_schema_migration_in_prod and it's fine, but we still need to perform basic checks.

@izeigerman izeigerman enabled auto-merge (squash) September 16, 2025 17:38
@izeigerman izeigerman merged commit dbc7de6 into main Sep 16, 2025
36 checks passed
@izeigerman izeigerman deleted the fix-view-migration-regression branch September 16, 2025 17:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants