Skip to content

fix(plan): avoid recreating materialized views on metadata-only changes - #6259

Draft
codewithsruthi wants to merge 1 commit into
SQLMesh:mainfrom
codewithsruthi:fix/5653-mv-metadata-downtime
Draft

codewithsruthi wants to merge 1 commit into
SQLMesh:mainfrom
codewithsruthi:fix/5653-mv-metadata-downtime

Conversation

@codewithsruthi

Copy link
Copy Markdown

Description

Fixes #5653

Problem & Root Cause

  1. Snapshot.requires_schema_migration_in_prod returns True for paused METADATA snapshots (as well as snapshots with physical_version or VDE != FULL), and PlanStagesBuilder.build() adds them — along with paused upstream snapshots pulled in by dag.subdag — to MigrateSchemasStage.
  2. ViewStrategy.migrate() unconditionally calls adapter.create_view(..., replace=True, materialized=True), which drops and recreates physical Materialized Views on metadata-only changes (causing prod downtime and destroying materialized data on engines like RisingWave/StarRocks, and unnecessary full rebuilds on Snowflake/BigQuery/Databricks).
  3. Simply comparing against unfinalized existing_environment.snapshots would be unsafe on retries after a failed forward-only prod plan, because EnvironmentRecordUpdateStage runs before MigrateSchemasStage.

Changes

  • sqlmesh/core/plan/stages.py:
    • Added optional grants_only_snapshot_ids and create_only_snapshot_ids sets (defaulting to empty) to MigrateSchemasStage.
    • In PlanStagesBuilder.build(), resolved finalized prod snapshots safely (existing_environment.snapshots only when finalized_ts is set and not expired; otherwise previous_finalized_snapshots or []) and checked _is_metadata_only_in_prod(s) against finalized prod (deployable table_name, not s.is_directly_modified(old), parent_data_hash, and non-symbolic parent deployable table_names).
    • Excluded metadata-only Materialized Views from schema_migration_snapshot_ids, routing physical grants changes (add, update, revoke-all grants={}, grants_target_layer changes) into grants_only_snapshot_ids.
    • Marked paused upstream MVs pulled in solely via dag.subdag as create_only_snapshot_ids so already-existing MVs are not dropped and recreated while downstream view cascades remain intact for genuinely migrated views.
  • sqlmesh/core/plan/evaluator.py:
    • Forwarded grants_only_snapshot_ids and create_only_snapshot_ids to SnapshotEvaluator.migrate(...).
  • sqlmesh/core/plan/explainer.py:
    • Updated RichExplainerConsole.visit_migrate_schemas_stage() to distinguish grants-only syncs and omit create_only entries from schema migration output.
  • sqlmesh/core/snapshot/evaluator.py:
    • Updated SnapshotEvaluator.migrate() and _migrate_snapshot() so that when target_data_object is not None, grants_only syncs only physical grants via _apply_grants without recreating the MV or running pre/post statements, and create_only returns immediately (falling through to _execute_create if the physical object does not exist).

Test Plan

  • Added unit and integration tests in:
    • tests/core/test_plan_stages.py (20 cases covering metadata-only MV changes: tags, owner, SQL comments, @macro(metadata_only=True), unfinalized failed forward-only/metadata prod retries, never-finalized & expired prod environments, stacked dev forward-only + metadata promotion, physical_version, VDE=DEV_ONLY, physical grants add/update/revoke-all/layer-switch/unchanged/virtual-only, dag.subdag upstream create_only MV, and RichExplainerConsole output)
    • tests/core/test_snapshot_evaluator.py (grants_only_snapshot_ids and create_only_snapshot_ids existing vs missing)
    • tests/core/integration/test_forward_only.py (end-to-end DuckDB tests verifying 0 physical MV recreations/drops on metadata-only prod/dev/rollback changes, grants sync, stacked dev forward-only + metadata promotion, and upstream BREAKING MV + downstream forward_only table)
  • Executed checks:
    • make py-style: Passed (ruff, ruff-format, mypy, valid migrations)
    • make fast-test: Passed (3,154 passed, 0 failed across all fast/isolated/fork/registry/dialect suites)
    • pytest tests/core/integration/test_forward_only.py -k "materialized_view": Passed (2 passed)
    • make doc-test: Passed (29 passed)

Checklist

  • I have run make py-style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

Signed-off-by: Sruthi <codewithsruthi@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

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.

Metadata changes to MVs cause downtime in prod

1 participant