Repository navigation
fix(plan): avoid recreating materialized views on metadata-only changes - #6259
Draft
codewithsruthi wants to merge 1 commit into
Draft
codewithsruthi wants to merge 1 commit into
codewithsruthi wants to merge 1 commit into
Conversation
Signed-off-by: Sruthi <codewithsruthi@users.noreply.github.com>
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #5653
Problem & Root Cause
Snapshot.requires_schema_migration_in_prodreturnsTruefor pausedMETADATAsnapshots (as well as snapshots withphysical_versionorVDE != FULL), andPlanStagesBuilder.build()adds them — along with paused upstream snapshots pulled in bydag.subdag— toMigrateSchemasStage.ViewStrategy.migrate()unconditionally callsadapter.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).existing_environment.snapshotswould be unsafe on retries after a failed forward-only prod plan, becauseEnvironmentRecordUpdateStageruns beforeMigrateSchemasStage.Changes
sqlmesh/core/plan/stages.py:grants_only_snapshot_idsandcreate_only_snapshot_idssets (defaulting to empty) toMigrateSchemasStage.PlanStagesBuilder.build(), resolved finalized prod snapshots safely (existing_environment.snapshotsonly whenfinalized_tsis set and notexpired; otherwiseprevious_finalized_snapshots or []) and checked_is_metadata_only_in_prod(s)against finalized prod (deployabletable_name,not s.is_directly_modified(old),parent_data_hash, and non-symbolic parent deployabletable_names).schema_migration_snapshot_ids, routing physical grants changes (add, update, revoke-allgrants={},grants_target_layerchanges) intogrants_only_snapshot_ids.dag.subdagascreate_only_snapshot_idsso already-existing MVs are not dropped and recreated while downstream view cascades remain intact for genuinely migrated views.sqlmesh/core/plan/evaluator.py:grants_only_snapshot_idsandcreate_only_snapshot_idstoSnapshotEvaluator.migrate(...).sqlmesh/core/plan/explainer.py:RichExplainerConsole.visit_migrate_schemas_stage()to distinguish grants-only syncs and omitcreate_onlyentries from schema migration output.sqlmesh/core/snapshot/evaluator.py:SnapshotEvaluator.migrate()and_migrate_snapshot()so that whentarget_data_object is not None,grants_onlysyncs only physical grants via_apply_grantswithout recreating the MV or running pre/post statements, andcreate_onlyreturns immediately (falling through to_execute_createif the physical object does not exist).Test Plan
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.subdagupstreamcreate_onlyMV, andRichExplainerConsoleoutput)tests/core/test_snapshot_evaluator.py(grants_only_snapshot_idsandcreate_only_snapshot_idsexisting 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 upstreamBREAKINGMV + downstreamforward_onlytable)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
make py-styleand fixed any issuesmake fast-test)git commit -s) per the DCO